package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.OpenAccount;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.vo.OpenAccountVO;
import com.ruoyi.system.mapper.IUserinfoMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.IUserinfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class IUserinfoServiceImpl implements IUserinfoService {
    @Autowired
    IUserinfoMapper iUserinfoMapper;
    private final SysUserRoleMapper userRoleMapper;


    @Override
    public R createAdmin(OpenAccountVO openAccountVO) {
        //检查用户名称、Uid是否重复
        String s = checkUIdAndUserName(openAccountVO);
        if (s == null){
            //注册用户
            OpenAccount openAccount = new OpenAccount(openAccountVO);
            iUserinfoMapper.insert(openAccount);
            //设置角色
            for (Integer role : openAccountVO.getRoles()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(openAccount.getUserId());
                sysUserRole.setRoleId(Long.valueOf(role));
                userRoleMapper.insert(sysUserRole);
            }
            return R.ok();
        }
        return R.fail(s);
    }

    @Override
    public R createUser(OpenAccountVO openAccountVO) {
        //检查用户名称、Uid是否重复
        String s = checkUIdAndUserName(openAccountVO);
        if (s == null){
            //注册用户
            OpenAccount openAccount = new OpenAccount(openAccountVO);
            iUserinfoMapper.insert(openAccount);
            //设置角色
            for (Integer role : openAccountVO.getRoles()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(openAccount.getUserId());
                sysUserRole.setRoleId(Long.valueOf(role));
                userRoleMapper.insert(sysUserRole);
            }
            return R.ok();
        }
        return R.fail(s);
    }

    @Override
    public void disableUser(OpenAccountVO openAccountVO) {
        UpdateWrapper<OpenAccount> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("u_id",openAccountVO.getU_id());
        updateWrapper.set("delFlag",2);
        iUserinfoMapper.update(null,updateWrapper);
    }

    /**
     * 判断用户是否存在
     * */
    public String checkUIdAndUserName(OpenAccountVO openAccountVO){
        //判断u_id 和 用户名称是否存在
        QueryWrapper<OpenAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("u_id","user_name");
        queryWrapper.eq("u_id",openAccountVO.getU_id());
        queryWrapper.or();
        queryWrapper.eq("user_name",openAccountVO.getUserName());
        List<OpenAccount> openAccounts = iUserinfoMapper.selectList(queryWrapper);
        if (!openAccounts.isEmpty()){
            if (openAccounts.size() == 2)
                return "u_id、userName已存在";
            else if (openAccounts.get(0).getUId() == openAccountVO.getU_id())
                return "u_id已存在";
            else
                return "userName已存在";
        }
        return null;
    }
}
