package com.gkhy.labRiskManage.domain.account.service.impl; import cn.hutool.core.util.ObjectUtil; import com.gkhy.labRiskManage.commons.enums.ResultCode; import com.gkhy.labRiskManage.commons.exception.BusinessException; import com.gkhy.labRiskManage.domain.account.converter.SysDeparmentConverter; import com.gkhy.labRiskManage.domain.account.entity.SysDepartment; import com.gkhy.labRiskManage.domain.account.model.bo.SysDepartmentBO; import com.gkhy.labRiskManage.domain.account.model.dto.SysDepartmentDomainDTO; import com.gkhy.labRiskManage.domain.account.repository.jpa.SysDeparmentRepository; import com.gkhy.labRiskManage.domain.account.service.SysDepartmentDomainService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * @email 1603559716@qq.com * @author: zf * @date: 2023/3/7 * @time: 17:18 */ @Service public class SysDepartmentDomainServiceImpl implements SysDepartmentDomainService { @Autowired private SysDeparmentRepository repository; @Autowired private SysDeparmentConverter converter; @Override public SysDepartmentDomainDTO save(SysDepartmentBO sysDepartmentBO,Long currentUserId) { if (ObjectUtil.isEmpty(sysDepartmentBO.getDepName())) { throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"请填写部门名称"); } byte depLevel = 0; if(ObjectUtil.isEmpty(sysDepartmentBO.getParentId())){ depLevel = 1; } if (!ObjectUtil.isEmpty(sysDepartmentBO.getParentId())) { //验证父级部门 SysDepartment parentDep = repository.findByIdAndDeleteStatus(sysDepartmentBO.getParentId()); if(ObjectUtil.isEmpty(parentDep)){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"父级部门不存在!"); } depLevel = (byte)(parentDep.getLevel()+1); } //复制 SysDepartment sysDepartment = new SysDepartment(); BeanUtils.copyProperties(sysDepartmentBO,sysDepartment); sysDepartment.setDeleteStatus((byte)0); sysDepartment.setLevel(depLevel); sysDepartment.setCreateByUserId(currentUserId); sysDepartment.setUpdateByUserId(currentUserId); SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(repository.save(sysDepartment)); return sysDepartmentDomainDTO; } @Override public SysDepartmentDomainDTO update(SysDepartmentBO sysDepartmentBO,Long currentUserId) { if (ObjectUtil.isEmpty(sysDepartmentBO.getId())) { throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"主键不可为空!"); } if (ObjectUtil.isEmpty(sysDepartmentBO.getDepName())) { throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"请填写部门名称"); } SysDepartment sysDepartment = repository.findByIdAndDeleteStatus(sysDepartmentBO.getId()); if(sysDepartment == null){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"该部门不存在,不可修改!"); } //复制 sysDepartment.setDepName(sysDepartmentBO.getDepName()); sysDepartment.setUpdateByUserId(currentUserId); sysDepartment.setInfo(sysDepartmentBO.getInfo()); SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(repository.save(sysDepartment)); return sysDepartmentDomainDTO; } @Override public SysDepartmentDomainDTO delete(Long id, Long currentUserId) { SysDepartment sysDepartment = repository.findByIdAndDeleteStatus(id); if(sysDepartment == null){ throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_DATA_NOT_EXISIST.getCode(),"该部门不存在,不可修改!"); } //验证该部门下子部门 if (!checkDepChildren(id)) { throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"该部门存在子部门,请先删除子部门!"); } sysDepartment.setDeleteStatus((byte)1); SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(repository.save(sysDepartment)); return sysDepartmentDomainDTO; } @Override public SysDepartmentDomainDTO findById(Long id){ if(ObjectUtil.isEmpty(id)){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(),"主键不可为空!"); } SysDepartment sysDepartment = repository.findByIdAndDeleteStatus(id); SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(sysDepartment); return sysDepartmentDomainDTO; } @Override public List list() { List list = repository.findByDeleteStatus(); List parentList = list.stream().filter(item -> item.getParentId() == null).collect(Collectors.toList()); List childList = list.stream().filter(item -> item.getParentId() != null).collect(Collectors.toList()); List departmentDomainDTOList = new ArrayList<>(); if(parentList.size() > 0){ for(SysDepartment sysDepartment : parentList){ SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(sysDepartment); sysDepartmentDomainDTO.setChildren(recursion(sysDepartmentDomainDTO.getId(), childList)); departmentDomainDTOList.add(sysDepartmentDomainDTO); } } return departmentDomainDTOList; } private List recursion(Long parentId,List list){ List childList = list.stream().filter(item -> item.getParentId().equals(parentId)).collect(Collectors.toList()); List departmentDomainDTOList = new ArrayList<>(); if(childList.size() > 0){ for(SysDepartment sysDepartment : childList){ SysDepartmentDomainDTO sysDepartmentDomainDTO = converter.sysDepDomainDTOConverter(sysDepartment); sysDepartmentDomainDTO.setChildren(recursion(sysDepartmentDomainDTO.getId(), list)); departmentDomainDTOList.add(sysDepartmentDomainDTO); } } return departmentDomainDTOList; } //验证部门 private boolean checkDep(Long id){ if(ObjectUtil.isEmpty(id)){ return false; } SysDepartment dep = repository.findByIdAndDeleteStatus(id); if(ObjectUtil.isEmpty(dep)){ return false; } return true; } //验证部门是否存在子部门 private boolean checkDepChildren(Long parentId){ if (ObjectUtil.isEmpty(parentId)) { return false; } List list = repository.findByParentIdAndDeleteStatus(parentId); if(!CollectionUtils.isEmpty(list)){ return false; } return true; } }