package com.ydtech.modules.admin.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.core.page.PageRequest; import com.ydtech.core.page.PageResult; import com.ydtech.exception.SystemException; import com.ydtech.modules.admin.dao.SysDeptMapper; import com.ydtech.modules.admin.model.SysDept; import com.ydtech.modules.admin.model.vo.SysDeptVo; import com.ydtech.modules.admin.model.vo.TreeSysDeptVO; import com.ydtech.modules.admin.service.SysDeptService; import com.ydtech.modules.protocol.utils.AssertionUtils; import com.ydtech.utils.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Service public class SysDeptServiceImpl extends ServiceImpl implements SysDeptService { @Override public List findTree(String deptId,String managementSource) { if(null == deptId || "".equals(deptId) || "undefined".equals(deptId)){ deptId = "9"; } SysDept sysDept = getById(deptId); LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(SysDept::getDelFlag, 0); lqw.likeRight(SysDept::getId, deptId); lqw.orderByDesc(SysDept::getOrderNum); List sysDeptList = list(lqw); //判断机构来源条件 进行遍历 if(null != managementSource && !"undefined".equals(managementSource) && !"".equals(managementSource)){ lqw.eq(SysDept::getManagementSource,managementSource); //条件 List conditionList = list(lqw); Map> conditionMap = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId)); Map> map = conditionList.stream().collect(Collectors.groupingBy(SysDept::getParentId)); //先查第6层 conditionList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>())) ); List result = new ArrayList<>(); //再查前4层 int level = 5; while(level > 0){ List resultTmp = recursion(sysDeptList, conditionMap); if(resultTmp.size() == 0){ break; }else{ result.clear(); result.addAll(resultTmp); } conditionMap = result.stream().collect(Collectors.groupingBy(SysDept::getParentId)); level--; } sysDeptList = result; }else{ Map> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId)); sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>()))); sysDeptList = map.get(sysDept.getParentId()); } return sysDeptList; } private List recursion(List sysDeptList, Map> conditionMap) { List result = new ArrayList<>(); sysDeptList.forEach(i -> conditionMap.forEach((key,value) -> { if (i.getId().equals(key)) { if(i.getChildren() == null){ i.setChildren(new ArrayList<>()); } i.getChildren().addAll(value); result.add(i); } }) ); return result; } @Override public List getUserList(String deptId) { SysDept sysDept = getById(deptId); LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(SysDept::getDelFlag, 0); lqw.likeRight(SysDept::getId, deptId); List sysDeptList = list(lqw); Map> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId)); return map.get(sysDept.getParentId()); } @Override public void addDept(SysDeptVo sysDeptVo) { SysDept sysDept = new SysDept(); BeanUtils.copyProperties(sysDeptVo, sysDept); String comlevel = ObjectUtils.isEmpty(sysDept.getComlevel()) ? "" : sysDept.getComlevel(); String parentId = sysDept.getParentId(); String number; if (comlevel.equals("2")) { AssertionUtils.isStringEmpty(sysDept.getProvince(), "二级机构份必填"); number = sysDept.getProvince().substring(0, 2); } else if (comlevel.equals("3")) { AssertionUtils.isStringEmpty(sysDept.getCity(), "三级机构市必填"); number = sysDept.getCity().substring(0, 4); } else if (comlevel.equals("4")) { AssertionUtils.isStringEmpty(sysDept.getArea(), "四级机构区必填"); number = sysDept.getArea().substring(0, 6); } else { if (comlevel.equals("5")) { sysDeptVo.setDeptType("M"); } String maxId = baseMapper.getMaxId(sysDept.getParentId()); if (!maxId.equals("0")) { String substring = maxId.substring(sysDept.getParentId().length() + 1); int i = Integer.parseInt("1" + substring) + 1; number = String.valueOf(i); } else { number = "101"; } } String substring = number.substring(number.length() - 2); String serialNumber; if ("M,D".contains(sysDeptVo.getDeptType())) { serialNumber = parentId + sysDeptVo.getDeptType() + substring; } else { serialNumber = parentId + substring; } sysDept.setId(serialNumber); SysDept dept = getById(sysDept.getId()); if (!ObjectUtils.isEmpty(dept)) { throw new SystemException("机构编号为:" + dept.getId() + " " + dept.getName() + "已被占用"); } SysDept parentDept = baseMapper.selectById(sysDept.getParentId()); if (parentDept.getComlevel().equals("5")){ sysDept.setManagementSource(parentDept.getManagementSource()); } boolean b = save(sysDept); AssertionUtils.isSucceed(b, "删除失败"); } @Override public void deleteDept(String deptId) { SysDept sysDept = getById(deptId); List list = lambdaQuery().eq(SysDept::getParentId, sysDept.getId()).list(); if (list.isEmpty()) { boolean b = removeById(deptId); AssertionUtils.isSucceed(b, "删除失败"); } else { throw new SystemException(sysDept.getName() + "下还有其他隶属机构"); } } @Override public PageResult findSubDepts(PageRequest pageRequest) { String id = pageRequest.getColumnFilterValue(pageRequest, "id"); String managementSource = pageRequest.getColumnFilterValue(pageRequest, "managementSource"); Page p = PageRequest.buildPageRequest(pageRequest); LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(StringUtils.isNotEmpty(id), SysDept::getParentId, id); lqw.eq(StringUtils.isNotEmpty(managementSource), SysDept::getManagementSource, managementSource); IPage pResult = page(p, lqw); //传入查询条件 return PageResult.buildPageResult(pResult); } //查找自已有权限的部门树 public List selectListTreeSysDeptVO(String id) { if (id == null) { id = ""; } LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.like(StringUtils.isNotEmpty(id), SysDept::getId, id); List deptList = list(lqw); List recordList = new ArrayList<>(); deptList.forEach(a -> { TreeSysDeptVO model = new TreeSysDeptVO(); model.setId(a.getId()); model.setName(a.getName()); model.setParentId(a.getParentId()); model.setDeptCode(a.getDeptCode()); model.setProvince(a.getProvince()); model.setCity(a.getCity()); model.setArea(a.getArea()); model.setComlevel(a.getComlevel()); recordList.add(model); }); return recordList; } public String findMaxDeptId(String deptId) { if (deptId == null) return ""; String result = "00"; QueryWrapper qw = new QueryWrapper<>(); qw.likeRight(StringUtils.isNotEmpty(deptId), "id", deptId + "D%"); qw.select("IFNULL(MAX(id),0) maxid"); Map m = getMap(qw); String maxId = m.get("maxid").toString(); if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) { result = maxId.substring(deptId.length() + 1); } return result; } public String findMaxHouseId(String deptId) { if (deptId == null) { return ""; } String result = "00"; QueryWrapper qw = new QueryWrapper<>(); qw.like(StringUtils.isNotEmpty(deptId), "id", deptId + "M%"); qw.select("IFNULL(MAX(id),0) maxid"); Map m = getMap(qw); String maxId = m.get("maxid").toString(); if (!maxId.equalsIgnoreCase("0") && !maxId.equalsIgnoreCase(deptId)) { result = maxId.substring(deptId.length() + 1); } return result; } // @Override public List selectListWhere(String where, Object[] paramValues) { String sql = "select t1.id, t1.name, t1.parent_id, t1.dept_code, t1.province, t1.city, t1.area, t1.comlevel from sys_dept t1 " + "where del_flag='0' " + where; QueryWrapper qw = new QueryWrapper<>(); qw.select(sql); List sysDeptList = list(qw); List recordList = new ArrayList(); sysDeptList.forEach(a -> { TreeSysDeptVO model = new TreeSysDeptVO(); model.setId(a.getId()); model.setName(a.getName()); model.setParentId(a.getParentId()); model.setDeptCode(a.getDeptCode()); model.setProvince(a.getProvince()); model.setCity(a.getCity()); model.setArea(a.getArea()); model.setComlevel(a.getComlevel()); recordList.add(model); }); return recordList; } /** * 获取机构树(不包含团队 dept_type=C) */ @Override public List findCompanyTree(String deptId) { SysDept sysDept = getById(deptId); LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(SysDept::getDelFlag, 0); lqw.likeRight(SysDept::getId, deptId); lqw.and((wrapper)->{wrapper.eq(SysDept::getDeptType,'C').or().eq(SysDept::getId,'9');}); List sysDeptList = list(lqw); Map> map = sysDeptList.stream().collect(Collectors.groupingBy(SysDept::getParentId)); sysDeptList.forEach(i -> i.setChildren(map.getOrDefault(i.getId(), new ArrayList<>()))); return map.get(sysDept.getParentId()); } /** * 根据传入机构,查询归属所有团队 */ @Override public List findTeamList(String deptId) { SysDept sysDept = getById(deptId); LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(SysDept::getDelFlag, 0); lqw.likeRight(SysDept::getId, deptId); lqw.eq(SysDept::getDeptType,'D'); lqw.apply(" exists (SELECT 1 FROM `sys_dept` b WHERE sys_dept.parent_id = b.`id` AND b.`comlevel`='5') "); List sysDeptList = list(lqw); return sysDeptList; } }