package easydo.technology.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import easydo.technology.system.domain.SysDept;
import easydo.technology.system.mapper.RoleMapper;
import easydo.technology.system.mapper.SysDeptMapper;
import easydo.technology.system.mapper.UserMapper;
import easydo.technology.system.service.ISysDeptService;
import easydo.technology.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 部门 服务实现类
 * </p>
 *
 * @author fr
 * @since 2022-01-26
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {
    @Resource
    UserMapper userMapper;
    @Resource
    RoleMapper roleMapper;

    @Override
    @Transactional
    public SysDept saveDept(SysDept dept) {
        String username = SecurityUtils.getCurrentUsername();
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateBy(username);
        dept.setUpdateBy(username);
        dept.setCreateTime(now);
        dept.setUpdateTime(now);
        baseMapper.insert(dept);
        if (dept.getPid() != null) {
            updateSubCount(dept.getPid());
        }
        return dept;
    }

    @Override
    @Transactional
    public SysDept updateDept(SysDept dept) throws Exception {
        if (dept.getPid() != null && dept.getDeptId().equals(dept.getPid())) {
            throw new Exception("上级不能为自己");
        }
        Long pidOld = baseMapper.selectById(dept.getDeptId()).getPid();

        String username = SecurityUtils.getCurrentUsername();
        LocalDateTime now = LocalDateTime.now();
        dept.setUpdateBy(username);
        dept.setUpdateTime(now);
        baseMapper.updateById(dept);
        updateSubCount(pidOld);
        updateSubCount(dept.getPid());
        return dept;
    }

    @Override
    public void verification(Set<Long> deptIds) throws Exception {
        if (userMapper.countByDepts(deptIds) > 0) {
            throw new Exception("所选部门存在用户关联，请解除后再试！");
        }
        if (roleMapper.countByDepts(deptIds) > 0) {
            throw new Exception("所选部门存在角色关联，请解除后再试！");
        }
        for (Long id : deptIds) {
            QueryWrapper<SysDept> query = new QueryWrapper<>();
            query.lambda().eq(SysDept::getPid, id);
            int count = baseMapper.selectCount(query);
            if (count > 0) {
                throw new Exception("所选部门存在下级部门，无法删除！");
            }
        }

    }


    private void updateSubCount(Long deptId) {
        if (deptId == null) {
            return;
        }
        QueryWrapper<SysDept> query = new QueryWrapper<>();
        query.lambda().eq(SysDept::getPid, deptId);
        int count = baseMapper.selectCount(query);

        UpdateWrapper<SysDept> update = new UpdateWrapper<>();
        update.lambda().eq(SysDept::getDeptId, deptId);
        SysDept dept = new SysDept();
        dept.setSubCount(count);
        baseMapper.update(dept, update);
    }
}
