package com.jy.system.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.framework.utils.AssertUtil;
import com.jy.system.dao.SysUserDao;
import com.jy.system.dao.SysUserRoleDao;
import com.jy.system.model.dto.ResetPwdDto;
import com.jy.system.model.dto.SysUserDto;
import com.jy.system.model.dto.SysUserSelectDto;
import com.jy.system.model.vo.SysUserInfoVo;
import com.jy.system.model.vo.SysUserVo;
import com.jy.system.service.SysUserRoleService;
import com.jy.system.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author
 * @since 2024-06-20
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private SysUserRoleDao sysUserRoleDao;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Override
    public List<SysUserInfoVo> getList(SysUserSelectDto dto) {
        return sysUserDao.getList(dto);
    }

    @Override
    public Page<SysUserVo> getPage(SysUserSelectDto dto) {
        // 角色id查询
        if (ObjectUtil.isNotNull(dto.getRoleId())) {
            List<Long> inUserIdList = sysUserRoleDao.getUserIdListByRoleId(dto.getRoleId());
            if (inUserIdList.isEmpty()) {
                return dto.getPage();
            }

            dto.setInUserIdList(inUserIdList);
        }

        // 排除角色id查询
        if (ObjectUtil.isNotNull(dto.getNeRoleId())) {
            List<Long> notInUserIdList = sysUserRoleDao.getUserIdListByRoleId(dto.getNeRoleId());
            dto.setNotInUserIdList(notInUserIdList);
        }

        return sysUserDao.getPage(dto);
    }

    @Override
    public SysUserVo getDetail(Long id) {
        SysUserVo vo = sysUserDao.getDetail(id);
        AssertUtil.notNull(vo, "未知数据");

        // 赋值用户拥有角色id
        List<Long> roleIdList = sysUserRoleDao.getRoleIdListByUserId(id);
        vo.setRoleIdList(roleIdList);

        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SysUserDto dto) {
        // 判断账号是否已存在
        boolean exists = sysUserDao.usernameExist(dto.getUsername());
        AssertUtil.eqFalse(exists, "账号已存在");

        // 密码加密
        dto.setPassword(BCrypt.hashpw(dto.getPassword()));

        // 保存用户
        sysUserDao.save(dto);

        // 添加用户角色关联
        sysUserRoleService.add(dto.getId(), dto.getRoleIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysUserDto dto) {
        // 不修改用户名与密码
        dto.setUsername(null);
        dto.setPassword(null);

        // 编辑用户
        sysUserDao.updateById(dto);

        // 编辑用户角色关联
        sysUserRoleService.edit(dto.getId(), dto.getRoleIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Long> idList) {
        // 删除用户角色关联
        sysUserRoleDao.removeByUserId(idList);

        // 删除用户
        sysUserDao.removeBatchByIds(idList);
    }

    @Override
    public void resetPwd(ResetPwdDto dto) {
        sysUserDao.updatePassword(dto.getUserId(), dto.getPassword());
    }

}
