/**sysDeptcontroller*/ @Controller @RequestMapping("/sys/dept") @Slf4j public class SysDeptController { @Resource private SysDeptService sysDeptService; @Resource private SysTreeService sysTreeService; @RequestMapping(value = "/save.json") @ResponseBody public JsonData saveDept(DeptParam param){ sysDeptService.save(param); return JsonData.success(); } }
/** SysDeptService */ package com.permission.service; import com.google.common.base.Preconditions; import com.permission.Exception.ParamException; import com.permission.dao.SysDeptMapper; import com.permission.model.SysDept; import com.permission.param.DeptParam; import com.permission.utils.BeanValidator; import com.permission.utils.LevelUtil; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Date; import java.util.List; @Service public class SysDeptService { @Resource private SysDeptMapper sysDeptMapper; public void save(DeptParam param){ BeanValidator.check(param); if (checkExist(param.getParentId(), param.getName(), param.getId())) { throw new ParamException("同一层级下存在相同名称的部门"); } SysDept dept = SysDept.builder() .name(param.getName()) .parentId(param.getParentId()) .seq(param.getSeq()) .remark(param.getRemark()) .build(); dept.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()),param.getParentId())); dept.setOperator("SYSTEM-SAVE"); dept.setOperateIp("127.0.0.1"); dept.setOperateTime(new Date()); sysDeptMapper.insertSelective(dept); } public void update(DeptParam param){ BeanValidator.check(param); if (checkExist(param.getParentId(), param.getName(), param.getId())) { throw new ParamException("同一层级下存在相同名称的部门"); } SysDept before = sysDeptMapper.selectByPrimaryKey(param.getId()); Preconditions.checkNotNull(before,"待更新的部门不存在"); if (checkExist(param.getParentId(), param.getName(), param.getId())) { throw new ParamException("同一层级下存在相同名称的部门"); } SysDept after = SysDept.builder() .id(param.getId()) .name(param.getName()) .parentId(param.getParentId()) .seq(param.getSeq()) .remark(param.getRemark()) .build(); after.setLevel(LevelUtil.calculateLevel(getLevel(param.getParentId()),param.getParentId())); after.setOperator("SYSTEM-UPDATE");// TODO: after.setOperateIp("127.0.0.1");// TODO: after.setOperateTime(new Date()); updateWithChild(before, after); } @Transactional public void updateWithChild(SysDept before, SysDept after){ String newLevelPrefix = after.getLevel(); String oldLevelPrefix = before.getLevel(); if (!after.getLevel().equals(before.getLevel())){ List<SysDept> deptList = sysDeptMapper.getChildDeptListByLevel(before.getLevel()); if (CollectionUtils.isNotEmpty(deptList)){ for (SysDept dept : deptList){ String level = dept.getLevel(); if(level.indexOf(oldLevelPrefix) == 0){ level = newLevelPrefix + level.substring(oldLevelPrefix.length()); dept.setLevel(level); } } sysDeptMapper.batchUpdateLevel(deptList); } } sysDeptMapper.updateByPrimaryKey(after); } private boolean checkExist(Integer parentId,String deptName,Integer deptId){ return sysDeptMapper.countByNameAndParentId(parentId,deptName,deptId) > 0; } private String getLevel(Integer deptId){ SysDept dept = sysDeptMapper.selectByPrimaryKey(deptId); if (dept == null){ return null; } return dept.getLevel(); } }
/** SysTreeService */ package com.permission.service; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.permission.Dto.DeptLevelDto; import com.permission.dao.SysDeptMapper; import com.permission.model.SysDept; import com.permission.utils.LevelUtil; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Collections; import java.util.Comparator; import java.util.List; @Service public class SysTreeService { @Resource private SysDeptMapper sysDeptMapper; public List<DeptLevelDto> deptTree() { List<SysDept> deptList = sysDeptMapper.getAllDept(); List<DeptLevelDto> dtoList = Lists.newArrayList(); for (SysDept dept : deptList) { DeptLevelDto dto = DeptLevelDto.adept(dept); dtoList.add(dto); } return deptListToTree(dtoList); } public List<DeptLevelDto> deptListToTree(List<DeptLevelDto> deptLevelList) { if (CollectionUtils.isEmpty(deptLevelList)) { return Lists.newArrayList(); } // level -> [dept1, dept2, ...] Map<String, List<Object>> Multimap<String, DeptLevelDto> levelDeptMap = ArrayListMultimap.create(); List<DeptLevelDto> rootList = Lists.newArrayList(); for (DeptLevelDto dto : deptLevelList) { levelDeptMap.put(dto.getLevel(), dto); if (LevelUtil.ROOT.equals(dto.getLevel())) { rootList.add(dto); } } // 按照seq从小到大排序 Collections.sort(rootList, new Comparator<DeptLevelDto>() { public int compare(DeptLevelDto o1, DeptLevelDto o2) { return o1.getSeq() - o2.getSeq(); } }); // 递归生成树 transformDeptTree(rootList, LevelUtil.ROOT, levelDeptMap); return rootList; } // level:0, 0, all 0->0.1,0.2 // level:0.1 // level:0.2 public void transformDeptTree(List<DeptLevelDto> deptLevelList, String level, Multimap<String, DeptLevelDto> levelDeptMap) { for (int i = 0; i < deptLevelList.size(); i++) { // 遍历该层的每个元素 DeptLevelDto deptLevelDto = deptLevelList.get(i); // 处理当前层级的数据 String nextLevel = LevelUtil.calculateLevel(level, deptLevelDto.getId()); // 处理下一层 List<DeptLevelDto> tempDeptList = (List<DeptLevelDto>) levelDeptMap.get(nextLevel); if (CollectionUtils.isNotEmpty(tempDeptList)) { // 排序 Collections.sort(tempDeptList, deptSeqComparator); // 设置下一层部门 deptLevelDto.setDeptLevelList(tempDeptList); // 进入到下一层处理 transformDeptTree(tempDeptList, nextLevel, levelDeptMap); } } } public Comparator<DeptLevelDto> deptSeqComparator = new Comparator<DeptLevelDto>() { public int compare(DeptLevelDto o1, DeptLevelDto o2) { return o1.getSeq() - o2.getSeq(); } }; }
/** SysDeptMapper.xml新增方法 */ <select id="getAllDept" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from sys_dept </select> <select id="getChildDeptListByLevel" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from sys_dept WHERE LEVEL LIKE #{level} || '.%' -- level like '0.1.%' </select> <update id="batchUpdateLevel" parameterType="map"> <foreach collection="sysDeptList" item="sysDept" separator=";"> UPDATE sys_dept SET level = #{sysDept.level} WHERE id = #{sysDept.id} </foreach> </update> <select id="countByNameAndParentId" parameterType="map" resultType="int"> SELECT count(1) FROM sys_dept WHERE name = #{name} <if test="parentId != null"> AND parent_id = #{parentId} </if> <if test="id != null"> AND id != #{id} </if> </select>
新增部门方法level没有改变