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.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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class IUserinfoServiceImpl implements IUserinfoService {

    private final IUserinfoMapper iUserinfoMapper;
    private final SysUserRoleMapper userRoleMapper;


    @Override
    public void createAdmin(OpenAccountVO openAccountVO) {
        //注册用户
        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);
        }

    }

    @Override
    public void createUser(OpenAccountVO openAccountVO) {
        OpenAccount openAccount = new OpenAccount(openAccountVO);
        iUserinfoMapper.insert(openAccount);
    }

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