package com.ydtech.modules.admin.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.modules.admin.dao.SysDeptInternalAgentMapper; import com.ydtech.modules.admin.model.dto.SysDeptInternalAgentDto; import com.ydtech.modules.admin.model.po.SysDeptInternalAgent; import com.ydtech.modules.admin.model.vo.SysDeptInternalAgentAddVo; import com.ydtech.modules.admin.model.vo.SysUserLinkAreaVo; import com.ydtech.modules.admin.service.SysDeptInternalAgentService; import com.ydtech.modules.order.entity.BasePageResult; import com.ydtech.utils.HeadUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author Administrator * @description 针对表【sys_dept_internal_agent(内部机构管理)】的数据库操作Service实现 * @createDate 2024-06-26 17:28:34 */ @Service public class SysDeptInternalAgentServiceImpl extends ServiceImpl implements SysDeptInternalAgentService { @Autowired private SysDeptInternalAgentMapper sysDeptInternalAgentMapper; /** * @version * @author: hxl * @Date: 2024/6/26 18:53 * @Description: 分页 */ @Override public BasePageResult queryPage(SysUserLinkAreaVo sysUserLinkAreaVo) { return null; } /** * @version * @author: hxl * @Date: 2024/6/26 18:53 * @Description: 添加或修改 */ @Override public void addOrUpdate(SysDeptInternalAgentAddVo sysDeptInternalAgentAddVo) { String parentId = sysDeptInternalAgentAddVo.getParentId(); String id = sysDeptInternalAgentAddVo.getId(); SysDeptInternalAgent sysDept = new SysDeptInternalAgent(); BeanUtils.copyProperties(sysDeptInternalAgentAddVo, sysDept); String uuid = HeadUtils.get16UUID(); SysDeptInternalAgent sysDeptInternalAgentParent = sysDeptInternalAgentMapper.selectById(parentId); if(StringUtils.isNotBlank(id)){ //更新操作 sysDeptInternalAgentMapper.updateById(sysDept); }else{ //插入操作 sysDept.setId(uuid); sysDept.setParentName(sysDeptInternalAgentParent.getName()); sysDept.setParentIds(sysDeptInternalAgentParent.getParentIds()+","+uuid); sysDept.setComlevel(sysDeptInternalAgentParent.getComlevel()+1); sysDeptInternalAgentMapper.insert(sysDept); } } /** * @version * @author: hxl * @Date: 2024/6/26 18:53 * @Description: 删除 */ @Override public void deleteByIds(List ids) { sysDeptInternalAgentMapper.deleteBatchIds(ids); } /** * @version * @author: hxl * @Date: 2024/6/26 18:54 * @Description: 查询对象 */ @Override public SysDeptInternalAgent findSysDeptInternalAgentById(String id) { return sysDeptInternalAgentMapper.selectById(id); } @Override public List findTree(String deptId, String comtype) { String parentIds =null; if(StringUtils.isNotBlank(deptId)){ SysDeptInternalAgent sysDeptInternalAgent = sysDeptInternalAgentMapper.selectById(deptId); parentIds=sysDeptInternalAgent.getParentIds(); } List sysDeptAll =sysDeptInternalAgentMapper.queryDeptList(parentIds,comtype); List resultList =new ArrayList(); List sysDeptTop = getTopDeptList(sysDeptAll); for (SysDeptInternalAgent dept : sysDeptTop) { if(StringUtils.isNotBlank(comtype)){ resultList.add(buildDeptTree(dept,sysDeptAll)); }else{ resultList.add(buildDeptTreeOne(dept,sysDeptAll)); } } return resultList; } /** * @version * @author: hxl * @Date: 2024/6/28 19:19 * @Description: 处理上下级关系 */ private SysDeptInternalAgent buildDeptTreeOne(SysDeptInternalAgent dept, List deptList){ for (SysDeptInternalAgent d : deptList) { if (dept.getId().equals(d.getParentId())) { if (null == dept.getChildren()) { dept.setChildren(new ArrayList()); } //递归调用 dept.getChildren().add(buildDeptTreeOne(d, deptList)); } } return dept; } /** * @version * @author: hxl * @Date: 2024/6/28 18:17 * @Description: 获取顶级机构 */ private static List getTopDeptList(List sysDeptAll) { Integer min; List levelList =new ArrayList<>(); List sysDeptTop=new ArrayList<>(); if(sysDeptAll !=null && sysDeptAll.size()>0){ for(SysDeptInternalAgent sysDeptInternalAgent: sysDeptAll){ levelList.add(Integer.parseInt(sysDeptInternalAgent.getComlevel())); } min = Collections.min(levelList); for(SysDeptInternalAgent sysDeptInternalAgent: sysDeptAll){ if(Integer.parseInt(sysDeptInternalAgent.getComlevel())==min){ sysDeptTop.add(sysDeptInternalAgent); } } } return sysDeptTop; } /** * @version * @author: hxl * @Date: 2024/6/27 8:42 * @Description: 处理爷孙关系 */ private SysDeptInternalAgent buildDeptTree(SysDeptInternalAgent dept, List deptList){ for (SysDeptInternalAgent d : deptList) { if (d.getParentIds().contains(dept.getParentIds()) && !d.getParentIds().equals(dept.getParentIds()) ) { if (null == dept.getChildren()) { dept.setChildren(new ArrayList()); } //递归调用 dept.getChildren().add(buildDeptTree(d, deptList)); } } return dept; } }