xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java
@@ -4,6 +4,7 @@
import com.mindskip.xzs.domain.enums.DeptAdminEnum;
import com.mindskip.xzs.domain.vo.BaseSelect;
import com.mindskip.xzs.domain.vo.CascaderDataVO;
import com.mindskip.xzs.domain.vo.DepartmentEditVO;
import com.mindskip.xzs.domain.vo.UpdateDeptAdminVO;
import com.mindskip.xzs.repository.BaseMapper;
import com.mindskip.xzs.repository.DepartmentMapper;
@@ -11,8 +12,10 @@
import com.mindskip.xzs.repository.UserMapper;
import com.mindskip.xzs.service.DepartmentService;
import com.mindskip.xzs.utility.ModelMapperSingle;
import com.mindskip.xzs.utility.TreeUtils;
import com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@@ -39,7 +42,45 @@
    @Override
    public Integer add(Department model) {
        model.setDeleted("0");
        if (Objects.nonNull(model.getParentId())) {
            // 查出父级的层级
            List<Integer> fatherList = departmentMapper.getFather(model.getParentId());
            Collections.reverse(fatherList);
            model.setLevel(fatherList.size() + 1);
        }
        return departmentMapper.add(model);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer update(Department department) {
        Department entity = departmentMapper.getById(department.getId());
        if (Objects.isNull(entity)) {
            throw new RuntimeException("该部门不存在");
        }
        // 先清除之前的管理员标识
        List<BaseSelect> deptUserList = userDepartmentMapper.getDeptUserList(entity.getId());
        List<Integer> userIds = deptUserList.stream().map(BaseSelect::getId).collect(Collectors.toList());
        if (! CollectionUtils.isEmpty(userIds)) {
            userMapper.clearDeptAdmin(userIds, entity.getId());
            userMapper.cancelUserDeptAdmin(userIds);
        }
        // 设置部门管理员
        if (! CollectionUtils.isEmpty(department.getAdminId())) {
            userMapper.updateDeptAdmin(department.getAdminId(), DeptAdminEnum.YES.getValue(), entity.getId());
            UpdateDeptAdminVO updateDeptAdminVO = new UpdateDeptAdminVO();
            updateDeptAdminVO.setAdminIds(department.getAdminId());
            userMapper.updateUserDeptAdmin(updateDeptAdminVO);
        }
        if (Objects.nonNull(department.getParentId())) {
            // 查出父级的层级
            List<Integer> fatherList = departmentMapper.getFather(department.getParentId());
            department.setLevel(fatherList.size() + 1);
        } else {
            department.setLevel(1);
        }
        Integer update = departmentMapper.update(department);
        return update;
    }
    @Override
@@ -48,28 +89,6 @@
        department.setId(id);
        department.setDeleted("1");
        return departmentMapper.update(department);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer update(Department department) {
//        userDepartmentMapper.removeByDepartmentId(department.getId());
        Department entity = departmentMapper.getById(department.getId());
        if (Objects.isNull(entity)) {
            throw new RuntimeException("该部门不存在");
        }
        if (! CollectionUtils.isEmpty(department.getAdminId())) {
            List<BaseSelect> deptUserList = userDepartmentMapper.getDeptUserList(entity.getId());
            List<Integer> userIds = deptUserList.stream().map(BaseSelect::getId).collect(Collectors.toList());
            if (! CollectionUtils.isEmpty(userIds)) {
                // 先清除之前的管理员标识
                userMapper.clearDeptAdmin(userIds, entity.getId());
            }
            // 标识部门管理员
            userMapper.updateDeptAdmin(department.getAdminId(), DeptAdminEnum.YES.getValue(), entity.getId());
        }
        Integer update = departmentMapper.update(department);
        return update;
    }
    @Override
@@ -108,8 +127,23 @@
    }
    @Override
    public DepartmentEditVO getByIdVO(Integer id) {
        Department dept = departmentMapper.getById(id);
        DepartmentEditVO vo = new DepartmentEditVO();
        BeanUtils.copyProperties(dept, vo);
        if (Objects.nonNull(dept.getParentId())) {
            // 查出完整的父级ID
            List<Integer> fatherList = departmentMapper.getFather(dept.getParentId());
            Collections.reverse(fatherList);
            vo.setParentIds(fatherList);
        }
        vo.setAdminId(userDepartmentMapper.selectDeptAdmin(id));
        return vo;
    }
    @Override
    public List<Department> gets(List<Integer> deptId) {
        return departmentMapper.gets(deptId);
        return TreeUtils.build(departmentMapper.gets(deptId));
    }
    @Override
@@ -134,20 +168,20 @@
        if (Objects.isNull(entity)) {
            throw new RuntimeException("该部门不存在");
        }
        List<BaseSelect> deptUserList = userDepartmentMapper.getDeptUserList(entity.getId());
        List<Integer> userIds = deptUserList.stream().map(BaseSelect::getId).collect(Collectors.toList());
        if (! CollectionUtils.isEmpty(userIds)) {
            // 先清除之前的管理员标识
            userMapper.clearDeptAdmin(userIds, entity.getId());
            // 修改没被选择的用户并且不是别的部门管理员的用户角色为1
            userMapper.cancelUserDeptAdmin(userIds);
        }
        if (! CollectionUtils.isEmpty(form.getAdminIds())) {
            List<BaseSelect> deptUserList = userDepartmentMapper.getDeptUserList(entity.getId());
            List<Integer> userIds = deptUserList.stream().map(BaseSelect::getId).collect(Collectors.toList());
            if (! CollectionUtils.isEmpty(userIds)) {
                // 先清除之前的管理员标识
                userMapper.clearDeptAdmin(userIds, entity.getId());
            }
            // 标识部门管理员
            userMapper.updateDeptAdmin(form.getAdminIds(), DeptAdminEnum.YES.getValue(), entity.getId());
            // 修改被选择的用户为部门管理员
            userMapper.updateUserDeptAdmin(form);
        }
        // 修改被选择的用户为部门管理员
        userMapper.updateUserDeptAdmin(form);
        // 修改没被选择的用户并且不是别的部门管理员的用户角色为1
        userMapper.cancelUserDeptAdmin(form);
    }
    @Override