package com.doumee.biz.system.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.doumee.biz.system.SystemDepartmentBiz; import com.doumee.core.exception.BusinessException; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.model.ApiResponse; import com.doumee.core.utils.ID; import com.doumee.dao.system.SystemUserJoinMapper; import com.doumee.dao.system.SystemUserMapper; import com.doumee.dao.system.model.SystemDepartment; import com.doumee.dao.system.model.SystemDepartmentUser; import com.doumee.dao.system.model.SystemUser; import com.doumee.dao.system.vo.SystemDepartmentListVO; import com.doumee.service.aware.DepartmentDataPermissionAware; import com.doumee.service.system.SystemDepartmentService; import com.doumee.service.system.SystemDepartmentUserService; import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @Service public class SystemDepartmentBizImpl implements SystemDepartmentBiz { @Autowired private SystemDepartmentService systemDepartmentService; @Autowired private SystemDepartmentUserService systemDepartmentUserService; @Autowired private DepartmentDataPermissionAware departmentDataPermissionAware; @Autowired private SystemUserJoinMapper systemUserJoinMapper; @Override public Integer create(SystemDepartment department) { // 验证部门编码 if (Objects.nonNull(department.getParentId())){ SystemDepartment systemDepartment = systemDepartmentService.findById(department.getParentId()); if (systemDepartment == null) { throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "上级部门不存在"); } } SystemDepartment query = new SystemDepartment(); query.setName(department.getName()); query.setDeleted(false); SystemDepartment one = systemDepartmentService.findOne(query); if (one != null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "部门名称已经存在"); } SystemDepartment insert = new SystemDepartment(); insert.setParentId(department.getParentId()); insert.setCode(department.getCode()); insert.setName(department.getName()); insert.setPhone(department.getPhone()); insert.setEmail(department.getEmail()); insert.setCreateUser(department.getCreateUser()); insert.setUpdateUser(department.getCreateUser()); return systemDepartmentService.create(insert); } @Override public void updateById(SystemDepartment department) { if (Objects.isNull(department.getParentId())){ SystemDepartment update = new SystemDepartment(); update.setId(department.getId()); update.setName(department.getName()); update.setUpdateUser(department.getUpdateUser()); update.setUpdateTime(new Date()); systemDepartmentService.updateById(update); return; } SystemDepartment systemDepartment = Optional.ofNullable(department) .map(s -> systemDepartmentService.findById(s.getParentId())) .orElseThrow(()->new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"上级部门不存在")); SystemDepartment update = new SystemDepartment(); update.setId(department.getId()); update.setParentId(department.getParentId()); update.setName(department.getName()); update.setUpdateUser(department.getUpdateUser()); update.setUpdateTime(new Date()); systemDepartmentService.updateById(update); } @Override public List findTree() { return departmentDataPermissionAware.execute(); } @Override public SystemDepartmentListVO findTreeUser(Integer parentId) { SystemDepartment parent = null; if (Objects.nonNull(parentId)){ parent = systemDepartmentService.findById(parentId); }else { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().isNull(SystemDepartment::getParentId).last("limit 1"); parent = systemDepartmentService.findOne(wrapper); } if (Objects.isNull(parent)){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"部门不存在"); } QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(SystemDepartment::getParentId,parent.getId()); List list = systemDepartmentService.findList(wrapper); MPJLambdaWrapper userWrapper = new MPJLambdaWrapper<>(); userWrapper.selectAll(SystemUser.class) .rightJoin(SystemDepartmentUser.class,SystemDepartmentUser::getUserId,SystemUser::getId) .eq(SystemDepartmentUser::getDepartmentId,parentId); List systemUsers = systemUserJoinMapper.selectJoinList(SystemUser.class, userWrapper); SystemDepartmentListVO systemDepartmentListVO = new SystemDepartmentListVO(); BeanUtils.copyProperties(parent,systemDepartmentListVO); systemDepartmentListVO.setUserList(systemUsers); if (!CollectionUtils.isEmpty(list)){ List collect = list.stream().map(s -> { SystemDepartmentListVO children = new SystemDepartmentListVO(); BeanUtils.copyProperties(s, children); return children; }).collect(Collectors.toList()); systemDepartmentListVO.setChildren(collect); } return systemDepartmentListVO; } @Override public void deleteById(Integer id) { List ids = systemDepartmentService.findChildren(id); if (!CollectionUtils.isEmpty(ids)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "存在子节点。请删除子节点"); } SystemDepartmentUser systemDepartmentUser = new SystemDepartmentUser(); systemDepartmentUser.setDepartmentId(id); systemDepartmentUser.setDeleted(false); List list = systemDepartmentUserService.findList(systemDepartmentUser); if (!CollectionUtils.isEmpty(list)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "部门存在人员,请处理人员"); } ids.add(id); systemDepartmentService.deleteByIdInBatch(ids); } @Override @Transactional public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } for (Integer id : ids) { this.deleteById(id); } } }