package com.gkhy.exam.system.service.impl; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.gkhy.exam.common.api.CommonPage; import com.gkhy.exam.common.constant.UserConstant; import com.gkhy.exam.common.domain.TreeSelect; import com.gkhy.exam.common.domain.entity.SysDept; import com.gkhy.exam.common.domain.entity.SysRole; import com.gkhy.exam.common.exception.ApiException; import com.gkhy.exam.common.utils.NaturalOrderComparator; import com.gkhy.exam.common.utils.PageUtils; import com.gkhy.exam.common.utils.SecurityUtils; import com.gkhy.exam.common.utils.SpringUtils; import com.gkhy.exam.system.domain.SysDeptManage; import com.gkhy.exam.system.domain.SysDeptResponsibility; import com.gkhy.exam.system.domain.SysFunctionalDistribution; import com.gkhy.exam.system.domain.SysFunctionalRemark; import com.gkhy.exam.system.domain.req.SysDeptPageReq; import com.gkhy.exam.system.domain.vo.*; import com.gkhy.exam.system.mapper.*; import com.gkhy.exam.system.service.ISysDeptService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * 部门管理 服务实现 * * @author expert */ @Service public class SysDeptServiceImpl implements ISysDeptService { @Autowired private SysDeptMapper deptMapper; @Autowired private SysRoleMapper roleMapper; @Autowired private SysDeptResponsibilityMapper deptResponsibilityMapper; @Autowired private SysFunctionalDistributionMapper sysFunctionalDistributionMapper; @Autowired private SysFunctionalRemarkMapper sysFunctionalRemarkMapper; public static final String[] DEPT_ROOT = {"4","4.1", "4.2", "4.3", "4.4","5", "5.1", "5.2", "5.3","6","6.1", "6.2", "6.3" ,"7","7.1.1", "7.1.2", "7.1.3", "7.1.4", "7.1.5", "7.1.6","7.2", "7.3", "7.4", "7.5", "7.6","8","8.1","8.2","8.2.1","8.2.2", "8.2.3", "8.2.4", "8.3", "8.3.1", "8.3.2", "8.3.3", "8.3.4", "8.3.5", "8.3.6", "8.3.7", "8.3.8", "8.4", "8.4.1", "8.4.2", "8.4.3", "8.5", "8.5.1", "8.5.2", "8.5.3", "8.5.4", "8.5.5", "8.5.6", "8.5.7", "8.6","8.7","9","9.1","9.1.1","9.1.2","9.1.3","9.2", "9.3","10","10.1" ,"10.2", "10.3"}; @Override public CommonPage selectDeptPageList(SysDeptPageReq req) { PageUtils.startPage(); List deptVos = deptMapper.selectDeptPageList(req); if (ObjectUtil.isNotEmpty(deptVos)){ for (DeptVo deptVo : deptVos) { List caluseVO1List = deptVo.getCaluseVO1List(); if (ObjectUtil.isNotEmpty(caluseVO1List)){ caluseVO1List.sort(Comparator.comparing(CaluseVO1::getClauseNum, new NaturalOrderComparator())); } } } CommonPage deptVoCommonPage = CommonPage.restPage(deptVos); return deptVoCommonPage; } /** * 查询部门管理数据 * * @param dept 部门信息 * @return 部门信息集合 */ @Override public List selectDeptList(SysDept dept) { if (dept.getCompanyId() == null){ dept.setCompanyId(SecurityUtils.getCompanyId()); } List deptVos = deptMapper.selectDeptList(dept); if (ObjectUtil.isNotEmpty(deptVos)){ for (DeptVo deptVo : deptVos) { List caluseVO1List = deptVo.getCaluseVO1List(); if (ObjectUtil.isNotEmpty(caluseVO1List)){ caluseVO1List.sort(Comparator.comparing(CaluseVO1::getClauseNum, new NaturalOrderComparator())); } } } return deptVos; } @Override public List getOutDeptList(SysDept dept) { return deptMapper.getOutDeptList(dept); } /** * 查询部门树结构信息 * * @param dept 部门信息 * @return 部门树信息集合 */ @Override public List selectDeptTreeList(SysDept dept) { List depts = SpringUtils.getAopProxy(this).getOutDeptList(dept); return buildDeptTreeSelect(depts); } @Override public List getParentInfo(Long deptId) { return deptMapper.getParentInfo(deptId); } /** * 构建前端所需要树结构 * * @param depts 部门列表 * @return 树结构列表 */ @Override public List buildDeptTree(List depts) { List returnList = new ArrayList(); List tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList()); for (SysDept dept : depts) { // 如果是顶级节点, 遍历该父节点的所有子节点 if (!tempList.contains(dept.getParentId())) { recursionFn(depts, dept); returnList.add(dept); } } if (returnList.isEmpty()) { returnList = depts; } return returnList; } /** * 构建前端所需要下拉树结构 * * @param depts 部门列表 * @return 下拉树结构列表 */ @Override public List buildDeptTreeSelect(List depts) { List deptTrees = buildDeptTree(depts); return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); } /** * 根据角色ID查询部门树信息 * * @param roleId 角色ID * @return 选中部门列表 */ @Override public List selectDeptListByRoleId(Long roleId) { SysRole role = roleMapper.selectRoleById(roleId); return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly()); } /** * 根据部门ID查询信息 * * @param deptId 部门ID * @return 部门信息 */ @Override public DeptDetialVo selectDeptById(Long deptId) { SysDept sysDept = deptMapper.selectDeptById(deptId); DeptDetialVo deptDetialVo = new DeptDetialVo(); BeanUtils.copyProperties(sysDept, deptDetialVo); LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery() .eq(SysDeptResponsibility::getDeptId, deptId) .eq(SysDeptResponsibility::getDelFlag, UserConstant.ENABLE) .orderByAsc(SysDeptResponsibility::getClauseNum); List sysDeptResponsibilities = deptResponsibilityMapper.selectList(lambdaQueryWrapper); if (ObjectUtil.isNotEmpty(sysDeptResponsibilities)){ sysDeptResponsibilities.sort(Comparator.comparing(SysDeptResponsibility::getClauseNum, new NaturalOrderComparator())); } deptDetialVo.setSysDeptResponsibilitys(sysDeptResponsibilities); return deptDetialVo ; } /** * 根据ID查询所有子部门(正常状态) * * @param deptId 部门ID * @return 子部门数 */ @Override public int selectNormalChildrenDeptById(Long deptId) { return deptMapper.selectNormalChildrenDeptById(deptId); } /** * 是否存在子节点 * * @param deptId 部门ID * @return 结果 */ @Override public boolean hasChildByDeptId(Long deptId) { int result = deptMapper.hasChildByDeptId(deptId); return result > 0; } /** * 查询部门是否存在用户 * * @param deptId 部门ID * @return 结果 true 存在 false 不存在 */ @Override public boolean checkDeptExistUser(Long deptId) { int result = deptMapper.checkDeptExistUser(deptId); return result > 0; } /** * 校验部门名称是否唯一 * * @param dept 部门信息 * @return 结果 */ @Override public boolean checkDeptNameUnique(SysDept dept) { Long deptId = ObjectUtil.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); Long companyId = SecurityUtils.getCompanyId(); SysDept info = deptMapper.checkDeptNameUnique(companyId,dept.getDeptName(), dept.getParentId()); if (ObjectUtil.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) { return UserConstant.NOT_UNIQUE; } return UserConstant.UNIQUE; } /** * 校验部门是否有数据权限 * * @param deptId 部门id */ @Override public void checkDeptDataScope(Long deptId) { if (!SecurityUtils.adminUser() && ObjectUtil.isNotNull(deptId)) { SysDept dept = new SysDept(); dept.setDeptId(deptId); List depts = SpringUtils.getAopProxy(this).getOutDeptList(dept); if (ObjectUtil.isEmpty(depts)) { throw new ApiException("没有权限访问部门数据!"); } } } /** * 新增保存部门信息 * * @param dept 部门信息 * @return 结果 */ @Override public int insertDept(SysDept dept) { SysDept info = deptMapper.selectDeptById(dept.getParentId()); // 如果父节点不为正常状态,则不允许新增子节点 if (!UserConstant.DEPT_NORMAL.equals(info.getStatus())) { throw new ApiException("部门停用,不允许新增"); } dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); return deptMapper.insertDept(dept); } @Override @Transactional(rollbackFor = RuntimeException.class) public int saveDept(SysDeptSaveDTOReq dept) { // Long companyId = SecurityUtils.getCompanyId(); // if (!companyId.equals(dept.getCompanyId())){ // throw new ApiException("无权操作!"); // } SysDept sysDept = new SysDept(); BeanUtils.copyProperties(dept, sysDept); boolean b = checkDeptNameUnique(sysDept); if (!b){ throw new ApiException("部门名称已存在!"); } SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); int i = 0; if (sysDept.getDeptId() == null){ if (ObjectUtil.isNotNull(newParentDept)){ String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); dept.setAncestors(newAncestors); } sysDept.setCreateBy(SecurityUtils.getUsername()); i = deptMapper.insertDept(sysDept); }else { if (sysDept.getParentId().equals(sysDept.getDeptId())){ throw new ApiException("上级部门不能是自己!"); } SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) { String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); String oldAncestors = oldDept.getAncestors(); dept.setAncestors(newAncestors); updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); } sysDept.setUpdateBy(SecurityUtils.getUsername()); sysDept.setUpdateTime(LocalDateTime.now()); i = deptMapper.updateDept(sysDept); } if (i > 0){ List delCaluseIds = dept.getDelCaluseIds(); if (delCaluseIds != null && !delCaluseIds.isEmpty()) { delCaluse(sysDept.getDeptId(), delCaluseIds); } //处理条款 batchSaveCaluse(sysDept.getDeptId(), dept.getCompanyId(), dept.getCaluseVO1List()); } return sysDept.getDeptId().byteValue(); } private void bacthInsertManager(Long deptId, List managerIds,Long companyId) { deptMapper.deleteMangeBatch(deptId); if (managerIds != null && !managerIds.isEmpty()) { List sysDeptManagers = new ArrayList<>(); for (Long managerId : managerIds) { SysDeptManage sysDeptManage = new SysDeptManage(); sysDeptManage.setDeptId(deptId); sysDeptManage.setSubDeptId(managerId); sysDeptManage.setCompanyId(companyId); sysDeptManagers.add(sysDeptManage); } deptMapper.insetMangeBatch(sysDeptManagers); } } @Override @Transactional(rollbackFor = RuntimeException.class) public int saveDeptResponsibility(SysDeptResponsibilityReqVo reqVo) { // Long companyId = SecurityUtils.getCompanyId(); // if (!companyId.equals(reqVo.getCompanyId())){ // throw new ApiException("无权操作!"); // } SysDept sysDept = deptMapper.selectDeptById(reqVo.getDeptId()); if (null == sysDept){ throw new ApiException("部门不存在!"); } if (!reqVo.getCompanyId().equals(sysDept.getCompanyId())){ throw new ApiException("无权操作!"); } SysDept dept = new SysDept(); dept.setDeptId(reqVo.getDeptId()); dept.setPersonNum(reqVo.getPersonNum()); dept.setInternalAuditors(reqVo.getInternalAuditors()); dept.setResponsibilities(reqVo.getResponsibilities()); dept.setUpdateBy(SecurityUtils.getUsername()); dept.setUpdateTime(LocalDateTime.now()); int i = deptMapper.updateDept(dept); if (i < 1){ throw new ApiException("编辑失败!"); } batchSaveRespon(reqVo); return 1; } @Override public List getFunctionalDistributionList(Long companyId) { return sysFunctionalDistributionMapper.selectListVo(companyId); } @Override @Transactional(rollbackFor = RuntimeException.class) public int initFunctionalDistribution(Long companyId) { sysFunctionalDistributionMapper.delByCompanyId(companyId); sysFunctionalRemarkMapper.delByCompanyId(companyId); SysDept sysDept = new SysDept(); sysDept.setCompanyId(companyId); sysDept.setResponsType("1"); List deptVos = deptMapper.selectDeptList(sysDept); if (deptVos != null && !deptVos.isEmpty()){ List sysFunctionalDistributions = new ArrayList<>(); Map clauseNumMap = new HashMap<>(); Map clauseNumSetNum = new HashMap<>(); Set clauseNumSet = new HashSet<>(); for (DeptVo deptVo : deptVos) { List caluseVO1List = deptVo.getCaluseVO1List(); if (ObjectUtil.isNotEmpty(caluseVO1List)){ // 转成 Map,key 是 clauseNum clauseNumMap = caluseVO1List.stream() .collect(Collectors.toMap( CaluseVO1::getClauseNum, // key Function.identity(), // value (existing, replacement) -> existing // 如果有重复 key,保留现有的值 )); } //加入职能 for (String s : DEPT_ROOT) { SysFunctionalDistribution sysFunctionalDistribution = new SysFunctionalDistribution(); sysFunctionalDistribution.setDeptId(deptVo.getDeptId()); sysFunctionalDistribution.setCompanyId(companyId); sysFunctionalDistribution.setCreateBy(SecurityUtils.getUsername()); sysFunctionalDistribution.setCreateTime(LocalDateTime.now()); sysFunctionalDistribution.setClauseNum(s); if (clauseNumMap.isEmpty()){ sysFunctionalDistribution.setChooseLab(0); }else if (clauseNumMap.containsKey(s)){ sysFunctionalDistribution.setChooseLab(1); if (deptVo.getDeptType().equals("0")){ Integer i = clauseNumSetNum.get(s); if (i == null){ i = 0; clauseNumSetNum.put(s, ++i); }else { clauseNumSetNum.put(s, ++i); clauseNumSet.add(s); } } }else { sysFunctionalDistribution.setChooseLab(0); } sysFunctionalDistributions.add(sysFunctionalDistribution); } } int i = sysFunctionalDistributionMapper.batchInsert(sysFunctionalDistributions); if (clauseNumSet.size() > 0){ List sysFunctionalRemarks = new ArrayList<>(); for (String clauseNum : clauseNumSet) { SysFunctionalRemark sysFunctionalRemark = new SysFunctionalRemark(); sysFunctionalRemark.setCompanyId(companyId); sysFunctionalRemark.setClauseNum(clauseNum); sysFunctionalRemark.setRemark(" "); sysFunctionalRemark.setCreateBy(SecurityUtils.getUsername()); sysFunctionalRemark.setCreateTime(LocalDateTime.now()); sysFunctionalRemarks.add(sysFunctionalRemark); } int j = sysFunctionalRemarkMapper.batchInsert(sysFunctionalRemarks); } if (i < 1){ throw new ApiException("初始化失败!"); } } return 1; } @Override @Transactional(rollbackFor = RuntimeException.class) public int saveFunctionalDistribution(FunctionalDistributionVo reqVo) { // Long companyId = SecurityUtils.getCompanyId(); // if (!companyId.equals(reqVo.getCompanyId())){ // throw new ApiException("无权操作!"); // } List list = reqVo.getList(); if (list != null && !list.isEmpty()) { list.forEach(e ->{ e.setCompanyId(reqVo.getCompanyId()); e.setUpdateBy(SecurityUtils.getUsername()); e.setUpdateTime(LocalDateTime.now()); }); int i = sysFunctionalDistributionMapper.batchUpdate(list); } List remarks = reqVo.getRemarks(); if (remarks != null && !remarks.isEmpty()) { sysFunctionalRemarkMapper.delByCompanyId(reqVo.getCompanyId()); remarks.forEach(e ->{ e.setCompanyId(reqVo.getCompanyId()); e.setCreateBy(SecurityUtils.getUsername()); e.setCreateTime(LocalDateTime.now()); }); int i1 = sysFunctionalRemarkMapper.batchInsert(remarks); } return 1; } @Override public List getFunctionalRemarkList(Long companyId) { return sysFunctionalRemarkMapper.selectListVo(companyId); } private void batchSaveRespon(SysDeptResponsibilityReqVo reqVo) { List delData = reqVo.getDelData(); if (delData != null && !delData.isEmpty()) { deptResponsibilityMapper .update(new SysDeptResponsibility(), new LambdaUpdateWrapper().set(SysDeptResponsibility::getDelFlag, UserConstant.DEPT_DISABLE) .set(SysDeptResponsibility::getUpdateTime, LocalDateTime.now()).set(SysDeptResponsibility::getUpdateBy, SecurityUtils.getUsername()) .in(SysDeptResponsibility::getId, delData) ); } List sysDeptResponsibilityList = reqVo.getSysDeptResponsibilityList(); List emptyIdClauseNums = sysDeptResponsibilityList.stream() .map(SysDeptResponsibility::getClauseNum) .collect(Collectors.toList()); Set emptyIdClauseNumSet = new HashSet<>(emptyIdClauseNums); if (emptyIdClauseNumSet.size() != emptyIdClauseNums.size()){ throw new ApiException("部门条款编码重复!"); } List emptyIdClauseIds = sysDeptResponsibilityList.stream() .filter(item -> item.getId() != null) .map(SysDeptResponsibility::getId) .collect(Collectors.toList()); if (!emptyIdClauseNums.isEmpty()){ List sysDeptResponsibilities = deptResponsibilityMapper.selectList(new LambdaQueryWrapper() .eq(SysDeptResponsibility::getDeptId, reqVo.getDeptId()) .in(SysDeptResponsibility::getClauseNum, emptyIdClauseNums) .notIn(SysDeptResponsibility::getId, emptyIdClauseIds).eq(SysDeptResponsibility::getDelFlag, "0")); if (!sysDeptResponsibilities.isEmpty()){ throw new ApiException("部门条款编码重复!"); } } for (SysDeptResponsibility sysDeptResponsibility : sysDeptResponsibilityList) { if (sysDeptResponsibility.getId() == null){ sysDeptResponsibility.setCreateBy(SecurityUtils.getUsername()); sysDeptResponsibility.setCreateTime(LocalDateTime.now()); sysDeptResponsibility.setDeptId(reqVo.getDeptId()); sysDeptResponsibility.setCompanyId(reqVo.getCompanyId()); deptResponsibilityMapper.insert(sysDeptResponsibility); }else { sysDeptResponsibility.setUpdateBy(SecurityUtils.getUsername()); sysDeptResponsibility.setUpdateTime(LocalDateTime.now()); deptResponsibilityMapper.updateById(sysDeptResponsibility); } } } private void delCaluse(Long deptId, List delCaluseIds) { deptResponsibilityMapper .update(new SysDeptResponsibility(), new LambdaUpdateWrapper().set(SysDeptResponsibility::getDelFlag, UserConstant.DEPT_DISABLE) .set(SysDeptResponsibility::getUpdateTime, LocalDateTime.now()).set(SysDeptResponsibility::getUpdateBy, SecurityUtils.getUsername()) .eq(SysDeptResponsibility::getDeptId, deptId).in(SysDeptResponsibility::getId, delCaluseIds)); } private void batchSaveCaluse(Long deptId,Long companyId, List caluseVO1List) { List emptyIdClauseNums = caluseVO1List.stream() .filter(item -> item.getId() == null) .map(CaluseVO1::getClauseNum) .collect(Collectors.toList()); if (!emptyIdClauseNums.isEmpty()){ List sysDeptResponsibilities = deptResponsibilityMapper.selectList(new LambdaQueryWrapper() .eq(SysDeptResponsibility::getDeptId, deptId).in(SysDeptResponsibility::getClauseNum, emptyIdClauseNums) .eq(SysDeptResponsibility::getCompanyId, companyId).eq(SysDeptResponsibility::getDelFlag, "0")); if (!sysDeptResponsibilities.isEmpty()){ throw new ApiException("部门条款编码重复!"); } } for (CaluseVO1 caluseVO1 : caluseVO1List) { SysDeptResponsibility sysDeptResponsibility = new SysDeptResponsibility(); BeanUtils.copyProperties(caluseVO1, sysDeptResponsibility); sysDeptResponsibility.setCompanyId(companyId); sysDeptResponsibility.setDeptId(deptId); if (sysDeptResponsibility.getId() == null){ sysDeptResponsibility.setCreateBy(SecurityUtils.getUsername()); sysDeptResponsibility.setCreateTime(LocalDateTime.now()); sysDeptResponsibility.setDataType("2"); deptResponsibilityMapper.insert(sysDeptResponsibility); }else { sysDeptResponsibility.setUpdateBy(SecurityUtils.getUsername()); sysDeptResponsibility.setUpdateTime(LocalDateTime.now()); deptResponsibilityMapper.updateById(sysDeptResponsibility); } } } /** * 修改保存部门信息 * * @param dept 部门信息 * @return 结果 */ @Override public int updateDept(SysDept dept) { SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) { String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); String oldAncestors = oldDept.getAncestors(); dept.setAncestors(newAncestors); updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); } int result = deptMapper.updateDept(dept); if (UserConstant.DEPT_NORMAL.equals(dept.getStatus()) && ObjectUtil.isNotEmpty(dept.getAncestors()) && !ObjectUtil.equals("0", dept.getAncestors())) { // 如果该部门是启用状态,则启用该部门的所有上级部门 updateParentDeptStatusNormal(dept); } return result; } /** * 修改该部门的父级部门状态 * * @param dept 当前部门 */ private void updateParentDeptStatusNormal(SysDept dept) { String ancestors = dept.getAncestors(); Long[] deptIds = Convert.toLongArray(ancestors); deptMapper.updateDeptStatusNormal(deptIds); } /** * 修改子元素关系 * * @param deptId 被修改的部门ID * @param newAncestors 新的父ID集合 * @param oldAncestors 旧的父ID集合 */ public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) { List children = deptMapper.selectChildrenDeptById(deptId); for (SysDept child : children) { child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); } if (children.size() > 0) { deptMapper.updateDeptChildren(children); } } /** * 删除部门管理信息 * * @param deptId 部门ID * @return 结果 */ @Override public int deleteDeptById(Long deptId) { return deptMapper.deleteDeptById(deptId); } /** * 递归列表 */ private void recursionFn(List list, SysDept t) { // 得到子节点列表 List childList = getChildList(list, t); t.setChildren(childList); for (SysDept tChild : childList) { if (hasChild(list, tChild)) { recursionFn(list, tChild); } } } /** * 得到子节点列表 */ private List getChildList(List list, SysDept t) { List tlist = new ArrayList(); Iterator it = list.iterator(); while (it.hasNext()) { SysDept n = (SysDept) it.next(); if (ObjectUtil.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) { tlist.add(n); } } return tlist; } /** * 判断是否有子节点 */ private boolean hasChild(List list, SysDept t) { return getChildList(list, t).size() > 0; } }