xiangpei
2024-05-15 09135e31580c89fa86ba760904dca6d88f98c040
src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java
@@ -18,9 +18,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 +64,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;
@@ -114,24 +114,21 @@
    }
    @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);
    }
}