龚焕茏
2024-07-03 3ec909b27b3eba956aa9d00cc7a94c179bd04bbf
src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java
@@ -1,10 +1,8 @@
package com.mindskip.xzs.service.impl;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.Department;
import com.mindskip.xzs.domain.UserDepartment;
import com.mindskip.xzs.domain.enums.DeptAdminEnum;
import com.mindskip.xzs.domain.vo.BaseSelect;
import com.mindskip.xzs.domain.vo.UpdateDeptAdminVO;
@@ -18,9 +16,11 @@
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
@@ -62,17 +62,15 @@
        if (Objects.isNull(entity)) {
            throw new RuntimeException("该部门不存在");
        }
        if (Objects.nonNull(department.getAdminId())) {
            Integer adminNum = departmentMapper.countByAdminId(department.getAdminId(), department.getId());
            if (adminNum > 0) {
                throw new RuntimeException("一个用户只能是一个单位的管理员");
            }
            if (! Objects.equals(entity.getAdminId(), department.getAdminId())) {
                // 取消原先的管理员标识
                userMapper.updateDeptAdmin(entity.getAdminId(), DeptAdminEnum.NO.getValue());
        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());
            userMapper.updateDeptAdmin(department.getAdminId(), DeptAdminEnum.YES.getValue(), entity.getId());
        }
        Integer update = departmentMapper.update(department);
        return update;
@@ -91,10 +89,7 @@
    }
    @Override
    public List<Department> gets(Integer deptId) {
        if (deptId == -1) {
            deptId = null;
        }
    public List<Department> gets(List<Integer> deptId) {
        return departmentMapper.gets(deptId);
    }
@@ -114,24 +109,25 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAdmin(UpdateDeptAdminVO form) {
        Department entity = departmentMapper.getById(form.getId());
        if (Objects.isNull(form)) {
        if (Objects.isNull(entity)) {
            throw new RuntimeException("该部门不存在");
        }
        if (Objects.nonNull(form.getAdminId())) {
            Integer adminNum = departmentMapper.countByAdminId(form.getAdminId(), form.getId());
            if (adminNum > 0) {
                throw new RuntimeException("一个用户只能是一个单位的管理员");
            }
            if (! Objects.equals(entity.getAdminId(), form.getAdminId())) {
                // 取消原先的管理员标识
                userMapper.updateDeptAdmin(entity.getAdminId(), DeptAdminEnum.NO.getValue());
        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.getAdminId(), DeptAdminEnum.YES.getValue());
            userMapper.updateDeptAdmin(form.getAdminIds(), DeptAdminEnum.YES.getValue(), entity.getId());
        }
        entity.setAdminId(form.getAdminId());
        departmentMapper.update(entity);
        // 修改被选择的用户为部门管理员
        userMapper.updateUserDeptAdmin(form);
        // 修改没被选择的用户并且不是别的部门管理员的用户角色为1
        userMapper.cancelUserDeptAdmin(form);
    }
}