package com.gkhy.safePlatform.account.service.impl; import com.alibaba.fastjson.JSONArray; import com.gkhy.safePlatform.account.enums.DepartmentLevelEnum; import com.gkhy.safePlatform.account.model.dto.req.DepartmentAddReqDTO; import com.gkhy.safePlatform.account.model.dto.req.DepartmentModReqDTO; import com.gkhy.safePlatform.account.model.dto.resp.DepRespDTO; import com.gkhy.safePlatform.account.model.dto.resp.DepartmentRespDTO; import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfo; import com.gkhy.safePlatform.account.entity.enterprise.DepartmentInfoDO; import com.gkhy.safePlatform.account.enums.DepartmentStatusEnum; import com.gkhy.safePlatform.account.service.DepartmentService; import com.gkhy.safePlatform.account.service.RedisService; import com.gkhy.safePlatform.account.service.baseService.DepartmentInfoService; import com.gkhy.safePlatform.account.service.baseService.GroupInfoService; import com.gkhy.safePlatform.account.service.baseService.UserInfoService; import com.gkhy.safePlatform.account.utils.DepartmentUtil; import com.gkhy.safePlatform.account.utils.TokenUtil; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.enums.E; import com.gkhy.safePlatform.commons.enums.RedisKeyEnum; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.AusinessException; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service("departmentService") public class DepartmentServiceImpl implements DepartmentService { @Autowired private DepartmentInfoService departmentInfoService; @Autowired private RedisService redisService; @Autowired private TokenUtil tokenConfig; @Autowired private UserInfoService userInfoService; @Autowired private GroupInfoService groupInfoService; @Override public List getDepartmentEnableList() { // 获取所有部门 List allDeps = this.getCacheDepartment(); // 部门生成树 return DepartmentUtil.generateMenuTree(allDeps); } /** * @Description: 新增部门 */ @Override public void addDepartment(ContextCacheUser currentUser, DepartmentAddReqDTO departmentAddReqDTO) { if (StringUtils.isBlank(departmentAddReqDTO.getDepName())) { throw new AusinessException(E.DATA_PARAM_NULL,"部门名称为空"); } if (departmentAddReqDTO.getDepLevel() == null) { throw new AusinessException(E.DATA_PARAM_NULL,"部门等级为空"); } DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(departmentAddReqDTO.getDepLevel()); if (levelEnum == null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "部门等级入参非法"); } if (StringUtils.isBlank(departmentAddReqDTO.getDepCode())) { throw new AusinessException(E.DATA_PARAM_NULL,"部门编号为空"); } // 部门编号不能超过16位长度 String depCode = departmentAddReqDTO.getDepCode().trim(); if (depCode.length() > 16) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "部门编号长度不宜超过16位"); } // 部门编号是否重复 DepartmentInfo departmentInfoByCode= departmentInfoService.getDepartmentInfoByCode(depCode); if (departmentInfoByCode != null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "部门编号已存在"); } String name = departmentAddReqDTO.getDepName().trim(); // 根部门等级只能是公司级 if (departmentAddReqDTO.getParentDepId() == null && levelEnum != DepartmentLevelEnum.COMPANY) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "根部门等级只能是公司级"); } // 传入父菜单判断是否存在 if (departmentAddReqDTO.getParentDepId() != null) { DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoById(departmentAddReqDTO.getParentDepId()); if (departmentInfo == null) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } if (!departmentInfo.getStatus().equals(DepartmentStatusEnum.ENABLED.getCode())) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } // 子部门不可以是公司级部门 if (levelEnum == DepartmentLevelEnum.COMPANY) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "无法创建公司级部门"); } // 父部门如果level是车间 子部门就必须是车间, 否则 子部门的Level 比父部门level 小(大) if (departmentInfo.getLevel().equals(DepartmentLevelEnum.WORKSHOP.code)) { levelEnum = DepartmentLevelEnum.WORKSHOP; } else if (levelEnum.code < departmentInfo.getLevel()) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "子部门等级需要在父部门等级之下"); } } // 获取同级目录 List sameLevelDeps = departmentInfoService.getDepartmentByParentId(departmentAddReqDTO.getParentDepId()); if (sameLevelDeps.size() > 0) { for (DepartmentInfoDO department : sameLevelDeps) { // 不能同名 if (department.getName().equals(name)) { throw new BusinessException(ResultCodes.CLIENT_DEP_NAME_EXIST); } } } DepartmentInfo departmentInfoDO = departmentInfoService.getDepartmentInfoByName(name); if (departmentInfoDO != null) { throw new AusinessException(E.DATA_DATABASE_EXIST, "部门名称已经重复,请更改名称方案"); } DepartmentInfo departmentInfo = new DepartmentInfo(); departmentInfo.setInfo(departmentAddReqDTO.getDepInfo()); departmentInfo.setName(name); departmentInfo.setParentId(departmentAddReqDTO.getParentDepId()); departmentInfo.setGmtCreate(new Date()); departmentInfo.setStatus(DepartmentStatusEnum.ENABLED.getCode()); departmentInfo.setLevel(levelEnum.code); departmentInfo.setCode(depCode); departmentInfoService.saveDepartmentInfo(departmentInfo); // redis刷新部门 List allDeps = departmentInfoService.getDepartmentByStatus(DepartmentStatusEnum.ENABLED); redisService.setCacheDepAndExpireTime(RedisKeyEnum.AUTH_DEP.getKey(), allDeps, tokenConfig.getExpiration()); } @Override public void modDepartment(ContextCacheUser currentUser, DepartmentModReqDTO departmentModReqDTO) { if (departmentModReqDTO.getDepId() == null) { throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } if (StringUtils.isBlank(departmentModReqDTO.getDepName())) { throw new AusinessException(E.DATA_PARAM_NULL,"部门名称为空"); } if (departmentModReqDTO.getDepLevel() == null) { throw new AusinessException(E.DATA_PARAM_NULL,"部门等级为空"); } DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(departmentModReqDTO.getDepLevel()); if (levelEnum == null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "部门等级入参非法"); } if (StringUtils.isBlank(departmentModReqDTO.getDepCode())) { throw new AusinessException(E.DATA_PARAM_NULL,"部门编号为空"); } String depCode = departmentModReqDTO.getDepCode().trim(); // 部门编号不能超过16位长度 if (depCode.length() > 16) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "部门编号长度不宜超过16位"); } String name = departmentModReqDTO.getDepName().trim(); // 查找的就是有效的部门 DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoById(departmentModReqDTO.getDepId()); // 部门是否存在 if (departmentInfo == null) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } // 部门转移判断 // 公司级别部门无法转移 // 存在子部门的部门无法转移 this.departmentTransferCheck(departmentInfo,departmentModReqDTO); // 部门修改判断 if (departmentModReqDTO.getParentDepId() == null && levelEnum != DepartmentLevelEnum.COMPANY) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "根部门等级只能是公司级"); } if (departmentModReqDTO.getParentDepId() != null) { // 父部门是否存在 DepartmentInfo department = departmentInfoService.getDepartmentInfoById(departmentModReqDTO.getParentDepId()); if (department == null) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } if (!department.getStatus().equals(DepartmentStatusEnum.ENABLED.getCode())) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } if (levelEnum == DepartmentLevelEnum.COMPANY) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "无法修改为公司级部门"); } // 父部门如果level是车间 子部门就必须是车间, 否则 子部门的Level 比父部门level 小(大) if (departmentInfo.getLevel().equals(DepartmentLevelEnum.WORKSHOP.code)) { levelEnum = DepartmentLevelEnum.WORKSHOP; } else if (levelEnum.code < departmentInfo.getLevel()) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "子部门等级需要在父部门等级之下"); } } DepartmentInfo departmentInfoDO = departmentInfoService.getDepartmentInfoByName(name); if (departmentInfoDO != null && !departmentInfoDO.getId().equals(departmentModReqDTO.getDepId())) { throw new AusinessException(E.DATA_DATABASE_EXIST, "部门名称已经重复,请更改名称方案"); } departmentInfoDO = departmentInfoService.getDepartmentInfoByCode(depCode); if (departmentInfoDO != null && !departmentInfoDO.getId().equals(departmentModReqDTO.getDepId())) { throw new AusinessException(E.DATA_DATABASE_EXIST, "部门编号已经重复,请更改编号方案"); } // 获取同级部门名称 List sameLevelDeps = departmentInfoService.getDepartmentByParentId(departmentInfo.getParentId()); // 判断是否重名 if (sameLevelDeps.size() > 0) { for (DepartmentInfoDO dep : sameLevelDeps) { // 不是自己的重名 if (dep.getName().equals(name) && !dep.getId().equals(departmentModReqDTO.getDepId())) { throw new BusinessException(ResultCodes.CLIENT_DEP_NAME_EXIST); } } } departmentInfo.setGmtModified(new Date()); departmentInfo.setName(name); departmentInfo.setInfo(departmentModReqDTO.getDepInfo()); departmentInfo.setParentId(departmentModReqDTO.getParentDepId()); departmentInfo.setLevel(levelEnum.code); departmentInfo.setCode(depCode); departmentInfoService.updateDepartmentInfo(departmentInfo); // redis 刷新部门 List allDeps = departmentInfoService.getDepartmentByStatus(DepartmentStatusEnum.ENABLED); redisService.setCacheDepAndExpireTime(RedisKeyEnum.AUTH_DEP.getKey(), allDeps, tokenConfig.getExpiration()); } /** * @Description: 判断是否能够部门转移 * @dbOriginalDep 原始部门数据 * @targetDep 目标部门修改数据 */ private void departmentTransferCheck(DepartmentInfo dbOriginalDep,DepartmentModReqDTO targetDep) { if (dbOriginalDep.getParentId() == null && targetDep.getParentDepId() != null) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "公司级根部门无法进行部门转移"); } // 查询当前修改部门子部门 List subDepartments = departmentInfoService.getDepartmentByParentId(dbOriginalDep.getId()); // 子部门如果存在 不能转移 if (subDepartments.size() > 0) { throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "当前部门下存在子部门 无法进行部门转移"); } } @Override public void delDepartment(ContextCacheUser currentUser, Long depId) { ; if (depId == null) { throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } DepartmentInfo department = departmentInfoService.getDepartmentInfoById(depId); if (department == null) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } assert department.getStatus() != null; DepartmentStatusEnum status = DepartmentStatusEnum.parse(department.getStatus()); if (status != DepartmentStatusEnum.ENABLED) { throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } // 1.判断部门下是否有人员 long countPerson = userInfoService.countByDepId(depId); if (countPerson > 0L) { throw new AusinessException(E.NOT_DELETE, "部门下存在人员"); } // 2.判断部门下是否有子部门 long countDepartment = departmentInfoService.countByParentDepId(depId); if (countDepartment > 0L) { throw new AusinessException(E.NOT_DELETE, "部门存在子部门"); } // 3.判断部门下是否有班组关系 boolean existGroup = groupInfoService.isExistGroupMountedByDepartment(depId); if (existGroup) { throw new AusinessException(E.NOT_DELETE, "部门下存在班组"); } // 4.删除部门 DepartmentInfo departmentInfo = new DepartmentInfo(); departmentInfo.setId(department.getId()); departmentInfo.setStatus(DepartmentStatusEnum.ABANDONED.getCode()); departmentInfo.setGmtModified(new Date()); departmentInfoService.updateDepartmentInfo(departmentInfo); // 5.redis 刷新部门 List allDeps = departmentInfoService.getDepartmentByStatus(DepartmentStatusEnum.ENABLED); redisService.setCacheDepAndExpireTime(RedisKeyEnum.AUTH_DEP.getKey(), allDeps, tokenConfig.getExpiration()); } @Override public DepRespDTO getEnableDepartmentInfoByDepId(Long userId, Long depId) { ; if (depId == null) { throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } DepRespDTO depRespDTO; DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoById(depId); if (departmentInfo == null) { depRespDTO = null; }else{ depRespDTO = new DepRespDTO(); depRespDTO.setDepId(departmentInfo.getId()); depRespDTO.setDepName(departmentInfo.getName()); depRespDTO.setDepInfo(departmentInfo.getInfo()); depRespDTO.setStatus(departmentInfo.getStatus()); depRespDTO.setDepLevel(departmentInfo.getLevel()); DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(departmentInfo.getLevel()); if (levelEnum != null) { depRespDTO.setDepLevelDesc(levelEnum.value); } DepartmentStatusEnum status = DepartmentStatusEnum.parse(departmentInfo.getStatus()); if (status != null) { depRespDTO.setStatusDesc(status.getValue()); } } return depRespDTO; } @Override public DepRespDTO getDepartmentInfoByDepId(Long depId) { if (depId == null) { throw new BusinessException(ResultCodes.SERVER_PARAM_NULL); } DepRespDTO depRespDTO = null; DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoById(depId); if (departmentInfo != null) { depRespDTO = new DepRespDTO(); depRespDTO.setDepId(departmentInfo.getId()); depRespDTO.setDepName(departmentInfo.getName()); depRespDTO.setDepInfo(departmentInfo.getInfo()); depRespDTO.setStatus(departmentInfo.getStatus()); depRespDTO.setDepLevel(departmentInfo.getLevel()); depRespDTO.setDepCode(departmentInfo.getCode()); depRespDTO.setParentDepId(departmentInfo.getParentId()); DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(departmentInfo.getLevel()); if (levelEnum != null) { depRespDTO.setDepLevelDesc(levelEnum.value); } DepartmentStatusEnum status = DepartmentStatusEnum.parse(departmentInfo.getStatus()); if (status != null) { depRespDTO.setStatusDesc(status.getValue()); } } return depRespDTO; } @Override public DepRespDTO getParentDepInfoByDepId(Long depId) { DepartmentInfoDO depDO = departmentInfoService.getParentDepartmentInfoDOByDepId(depId); DepRespDTO depRespDTO = null; if (depDO != null) { depRespDTO = new DepRespDTO(); depRespDTO.setDepId(depDO.getId()); depRespDTO.setDepName(depDO.getName()); depRespDTO.setDepInfo(depDO.getInfo()); depRespDTO.setDepCode(depDO.getCode()); depRespDTO.setStatus(depDO.getStatus()); depRespDTO.setDepLevel(depDO.getLevel()); DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(depDO.getLevel()); if (levelEnum != null) { depRespDTO.setDepLevelDesc(levelEnum.value); } DepartmentStatusEnum status = DepartmentStatusEnum.parse(depDO.getStatus()); if (status != null) { depRespDTO.setStatusDesc(status.getValue()); } } return depRespDTO; } @Override public boolean isSelfOrSubDep(Long depId, Long depId1) { boolean result; if (depId.equals(depId1)) { result = true; }else{ List allDeps = this.getCacheDepartment(); result = DepartmentUtil.isUnderDepartment(depId,depId1,allDeps); } return result; } @Override public DepRespDTO getDepartmentInfoByDepName(String depName) { DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoByName(depName); DepRespDTO result = null; if (departmentInfo != null) { result = new DepRespDTO(); result.setDepId(departmentInfo.getId()); result.setDepName(departmentInfo.getName()); result.setDepInfo(departmentInfo.getInfo()); result.setStatus(departmentInfo.getStatus()); result.setDepLevel(departmentInfo.getLevel()); DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(departmentInfo.getLevel()); if (levelEnum != null) { result.setDepLevelDesc(levelEnum.value); } DepartmentStatusEnum statusEnum = DepartmentStatusEnum.parse(departmentInfo.getStatus()); if (statusEnum != null) { result.setStatusDesc(statusEnum.getValue()); } result.setParentDepId(departmentInfo.getParentId()); } return result; } @Override public List listDepAndSubDepIds(Long depId) { List allDeps = getCacheDepartment(); return DepartmentUtil.generateDepIdListByDepId(depId,allDeps); } @Override public List listDepAndSubByDepId(Long depId) { // 获取所有部门 List allDeps = this.getCacheDepartment(); List deps = DepartmentUtil.generateDepListByDepId(depId, allDeps); // 转换 List result = new ArrayList<>(deps.size()); DepRespDTO respDTO; if (deps.size() > 0) { for (DepartmentInfoDO dep : deps) { respDTO = new DepRespDTO(); respDTO.setDepId(dep.getId()); respDTO.setDepName(dep.getName()); respDTO.setDepInfo(dep.getInfo()); respDTO.setStatus(dep.getStatus()); DepartmentStatusEnum statusEnum = DepartmentStatusEnum.parse(dep.getStatus()); if (statusEnum != null) { respDTO.setStatusDesc(statusEnum.getValue()); } respDTO.setParentDepId(dep.getParentId()); respDTO.setDepLevel(dep.getLevel()); DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(dep.getLevel()); if (levelEnum != null) { respDTO.setDepLevelDesc(levelEnum.value); } result.add(respDTO); } } return result; } @Override public List listDepByDepIds(List depIds) { List result; if (depIds != null && depIds.size() > 0) { List allDeps = getCacheDepartment(); result = new ArrayList<>(depIds.size()); for (DepartmentInfoDO dep : allDeps) { if (depIds.contains(dep.getId())) { DepRespDTO respDTO = new DepRespDTO(); respDTO.setDepId(dep.getId()); respDTO.setDepName(dep.getName()); respDTO.setDepInfo(dep.getInfo()); respDTO.setStatus(dep.getStatus()); DepartmentStatusEnum statusEnum = DepartmentStatusEnum.parse(dep.getStatus()); if (statusEnum != null) { respDTO.setStatusDesc(statusEnum.getValue()); } respDTO.setParentDepId(dep.getParentId()); respDTO.setDepLevel(dep.getLevel()); DepartmentLevelEnum levelEnum = DepartmentLevelEnum.parse(dep.getLevel()); if (levelEnum != null) { respDTO.setDepLevelDesc(levelEnum.value); } result.add(respDTO); } } }else{ result = Collections.emptyList(); } return result; } @Override public List listSubDepsByDepId(Long depId) { List result = new ArrayList<>(); List deps = getCacheDepartment(); if (deps.size() > 0 && depId != null) { DepRespDTO depRespDTO; DepartmentLevelEnum levelEnum; DepartmentStatusEnum statusEnum; for (DepartmentInfoDO depDo : deps) { if (depId.equals(depDo.getParentId())) { depRespDTO = new DepRespDTO(); depRespDTO.setDepId(depDo.getId()); depRespDTO.setDepName(depDo.getName()); depRespDTO.setDepInfo(depDo.getInfo()); depRespDTO.setStatus(depDo.getStatus()); statusEnum = DepartmentStatusEnum.parse(depDo.getStatus()); if (statusEnum != null) { depRespDTO.setStatusDesc(statusEnum.getValue()); } depRespDTO.setParentDepId(depDo.getParentId()); depRespDTO.setDepLevel(depDo.getLevel()); levelEnum = DepartmentLevelEnum.parse(depDo.getLevel()); if (levelEnum != null) { depRespDTO.setDepLevelDesc(levelEnum.value); } result.add(depRespDTO); } } } return result; } /** * @Description: 统一调用 缓存获取 */ private List getCacheDepartment(){ List allDeps; // 1.redis获取部门 String oo = redisService.getCacheDepByKey(RedisKeyEnum.AUTH_DEP.getKey()); if (oo != null) { allDeps = JSONArray.parseArray(oo, DepartmentInfoDO.class); }else{ // 2.mysql获取 && 存入 redis // 获取启用部门 allDeps = departmentInfoService.getDepartmentByStatus(DepartmentStatusEnum.ENABLED); redisService.setCacheDepAndExpireTime(RedisKeyEnum.AUTH_DEP.getKey(), allDeps, tokenConfig.getExpiration()); } return allDeps; } }