¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.TreeSelect; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.system.service.ISysDeptService; |
| | | |
| | | /** |
| | | * é¨é¨ç®¡ç æå¡å®ç° |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Service |
| | | public class SysDeptServiceImpl implements ISysDeptService |
| | | { |
| | | @Autowired |
| | | private SysDeptMapper deptMapper; |
| | | |
| | | /** |
| | | * æ¥è¯¢é¨é¨ç®¡çæ°æ® |
| | | * |
| | | * @param dept é¨é¨ä¿¡æ¯ |
| | | * @return é¨é¨ä¿¡æ¯éå |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public List<SysDept> selectDeptList(SysDept dept) |
| | | { |
| | | return deptMapper.selectDeptList(dept); |
| | | } |
| | | |
| | | /** |
| | | * æå»ºå端æéè¦æ ç»æ |
| | | * |
| | | * @param depts é¨é¨å表 |
| | | * @return æ ç»æå表 |
| | | */ |
| | | @Override |
| | | public List<SysDept> buildDeptTree(List<SysDept> depts) |
| | | { |
| | | List<SysDept> returnList = new ArrayList<SysDept>(); |
| | | List<Long> tempList = new ArrayList<Long>(); |
| | | for (SysDept dept : depts) |
| | | { |
| | | tempList.add(dept.getDeptId()); |
| | | } |
| | | for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext();) |
| | | { |
| | | SysDept dept = (SysDept) iterator.next(); |
| | | // 妿æ¯é¡¶çº§èç¹, éå该ç¶èç¹çææåèç¹ |
| | | if (!tempList.contains(dept.getParentId())) |
| | | { |
| | | recursionFn(depts, dept); |
| | | returnList.add(dept); |
| | | } |
| | | } |
| | | if (returnList.isEmpty()) |
| | | { |
| | | returnList = depts; |
| | | } |
| | | return returnList; |
| | | } |
| | | |
| | | /** |
| | | * æå»ºå端æéè¦ä¸ææ ç»æ |
| | | * |
| | | * @param depts é¨é¨å表 |
| | | * @return 䏿æ ç»æå表 |
| | | */ |
| | | @Override |
| | | public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) |
| | | { |
| | | List<SysDept> deptTrees = buildDeptTree(depts); |
| | | return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®è§è²IDæ¥è¯¢é¨é¨æ ä¿¡æ¯ |
| | | * |
| | | * @param roleId è§è²ID |
| | | * @return éä¸é¨é¨å表 |
| | | */ |
| | | @Override |
| | | public List<Integer> selectDeptListByRoleId(Long roleId) |
| | | { |
| | | return deptMapper.selectDeptListByRoleId(roleId); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®é¨é¨IDæ¥è¯¢ä¿¡æ¯ |
| | | * |
| | | * @param deptId é¨é¨ID |
| | | * @return é¨é¨ä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | public SysDept selectDeptById(Long deptId) |
| | | { |
| | | return deptMapper.selectDeptById(deptId); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®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 ? true : false; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢é¨é¨æ¯å¦åå¨ç¨æ· |
| | | * |
| | | * @param deptId é¨é¨ID |
| | | * @return ç»æ true åå¨ false ä¸åå¨ |
| | | */ |
| | | @Override |
| | | public boolean checkDeptExistUser(Long deptId) |
| | | { |
| | | int result = deptMapper.checkDeptExistUser(deptId); |
| | | return result > 0 ? true : false; |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªé¨é¨åç§°æ¯å¦å¯ä¸ |
| | | * |
| | | * @param dept é¨é¨ä¿¡æ¯ |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | public String checkDeptNameUnique(SysDept dept) |
| | | { |
| | | Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); |
| | | SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId()); |
| | | if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) |
| | | { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢ä¿åé¨é¨ä¿¡æ¯ |
| | | * |
| | | * @param dept é¨é¨ä¿¡æ¯ |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | public int insertDept(SysDept dept) |
| | | { |
| | | SysDept info = deptMapper.selectDeptById(dept.getParentId()); |
| | | // 妿ç¶èç¹ä¸ä¸ºæ£å¸¸ç¶æ,åä¸å
许æ°å¢åèç¹ |
| | | if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) |
| | | { |
| | | throw new CustomException("é¨é¨åç¨ï¼ä¸å
许æ°å¢"); |
| | | } |
| | | dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); |
| | | return deptMapper.insertDept(dept); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹ä¿åé¨é¨ä¿¡æ¯ |
| | | * |
| | | * @param dept é¨é¨ä¿¡æ¯ |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | public int updateDept(SysDept dept) |
| | | { |
| | | SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); |
| | | SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); |
| | | if (StringUtils.isNotNull(newParentDept) && StringUtils.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 (UserConstants.DEPT_NORMAL.equals(dept.getStatus())) |
| | | { |
| | | // å¦æè¯¥é¨é¨æ¯å¯ç¨ç¶æï¼åå¯ç¨è¯¥é¨é¨çææä¸çº§é¨é¨ |
| | | updateParentDeptStatus(dept); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹è¯¥é¨é¨çç¶çº§é¨é¨ç¶æ |
| | | * |
| | | * @param dept å½åé¨é¨ |
| | | */ |
| | | private void updateParentDeptStatus(SysDept dept) |
| | | { |
| | | String updateBy = dept.getUpdateBy(); |
| | | dept = deptMapper.selectDeptById(dept.getDeptId()); |
| | | dept.setUpdateBy(updateBy); |
| | | deptMapper.updateDeptStatus(dept); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹åå
ç´ å
³ç³» |
| | | * |
| | | * @param deptId 被修æ¹çé¨é¨ID |
| | | * @param newAncestors æ°çç¶IDéå |
| | | * @param oldAncestors æ§çç¶IDéå |
| | | */ |
| | | public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) |
| | | { |
| | | List<SysDept> children = deptMapper.selectChildrenDeptById(deptId); |
| | | for (SysDept child : children) |
| | | { |
| | | child.setAncestors(child.getAncestors().replace(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<SysDept> list, SysDept t) |
| | | { |
| | | // å¾å°åèç¹å表 |
| | | List<SysDept> childList = getChildList(list, t); |
| | | t.setChildren(childList); |
| | | for (SysDept tChild : childList) |
| | | { |
| | | if (hasChild(list, tChild)) |
| | | { |
| | | // 夿æ¯å¦æåèç¹ |
| | | Iterator<SysDept> it = childList.iterator(); |
| | | while (it.hasNext()) |
| | | { |
| | | SysDept n = (SysDept) it.next(); |
| | | recursionFn(list, n); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¾å°åèç¹å表 |
| | | */ |
| | | private List<SysDept> getChildList(List<SysDept> list, SysDept t) |
| | | { |
| | | List<SysDept> tlist = new ArrayList<SysDept>(); |
| | | Iterator<SysDept> it = list.iterator(); |
| | | while (it.hasNext()) |
| | | { |
| | | SysDept n = (SysDept) it.next(); |
| | | if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) |
| | | { |
| | | tlist.add(n); |
| | | } |
| | | } |
| | | return tlist; |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦æåèç¹ |
| | | */ |
| | | private boolean hasChild(List<SysDept> list, SysDept t) |
| | | { |
| | | return getChildList(list, t).size() > 0 ? true : false; |
| | | } |
| | | } |