package org.springblade.mdm.program.service; import com.baomidou.mybatisplus.core.metadata.IPage; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.mp.base.BizServiceImpl; import org.springblade.core.mp.support.Condition; import org.springblade.core.tool.utils.Func; import org.springblade.mdm.basesetting.machine.vo.MachineVO; import org.springblade.mdm.program.entity.NcNode; import org.springblade.mdm.program.mapper.NcNodeMapper; import org.springblade.mdm.program.vo.*; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Objects; /** * 程序节点 * * @author yangys */ @Slf4j @Service @AllArgsConstructor public class NcNodeService extends BizServiceImpl { public void saveNcCode(NcNodeVO vo) { NcNode ncNode = new NcNode(); BeanUtils.copyProperties(vo, ncNode); ncNode.setId(null); ncNode.setParentIds(buildParentIds(vo.getParentId())); this.save(ncNode); } /** * 构建父id节点 * @param nodeId 节点id * @return */ String buildParentIds(long nodeId){ if(nodeId == 0L){ return "0"; } NcNode pNode = this.baseMapper.selectById(nodeId); return pNode.getParentIds()+","+pNode.getId(); } /** * 更新节点 * @param vo */ public void updateNcNode(NcNodeVO vo) { NcNode ncNode = this.getById(vo.getId()); ncNode.setName(vo.getName()); ncNode.setNodeType(vo.getNodeType()); ncNode.setDescription(vo.getDescription()); ncNode.setRemark(vo.getRemark()); ncNode.setMachineCode(vo.getMachineCode()); ncNode.setParentIds(buildParentIds(vo.getParentId())); this.updateById(ncNode); } /** * 懒加载列表 * @param parentId 父节点ID * @return */ public List lazyList(Long parentId) { // 判断点击搜索但是没有查询条件的情况 if (Func.isEmpty(parentId)) { parentId = 0L; } return baseMapper.lazyList(parentId); } /** * 查询现有固化的程序,暂定条件:零组件号相同,且是同一机床组 * * @param name 程序名称 * @param name * @return */ public NcNode getLastProgramNode(String name) { return this.getBaseMapper().getLastProgramNode(name); } /** * 首页树查询 * @param queryVO * @return */ public List searchList(NcNodeQueryVO queryVO) { //1.根据关进字和节点类型查询初始列表,查询的原始列表,需要hasChild字段,所以使用mapper.xml List oriList = this.getBaseMapper().searchList(queryVO); //List list = this.lambdaQuery().eq(NcNode::getNodeType, queryVO.getNodeType()).like(NcNode::getName, queryVO.getName()).list(); if ("10".equals(queryVO.getNodeType())) { //搜索的根级别,直接返回 return oriList; } List allVos = new ArrayList();//已经加入过的节点,用于去重 List rootVos = new ArrayList(); //List rootNodes = new ArrayList<>(); for(NcNodeVO vo : oriList){ allVos.add(vo); //上级各级的节点 List parents = this.getBaseMapper().searchListInIds(Func.toLongList(vo.getParentIds())); for(NcNodeVO pvo : parents){ if(pvo.getParentId() == 0L){ rootVos.add(pvo); } allVos.add(pvo); } } for(NcNodeVO root : rootVos){ addNodeChildren(root, allVos); } return rootVos; } void addNodeChildren(NcNodeVO node, List allNodes) { for(NcNodeVO vo : allNodes){ if(Objects.equals(vo.getParentId(), node.getId())){ node.addChildren(vo); } } if(node.getChildren()!=null && !node.getChildren().isEmpty()){ for(NcNodeVO child : node.getChildren()){ addNodeChildren(child, allNodes); } } } NcNodeVO toNodeVO(NcNode node){ NcNodeVO vo = new NcNodeVO(); BeanUtils.copyProperties(node, vo); return vo; } /** * 获取程序包名的数据 * @param programName 程序名称(程序包名) * @return 程序报包名的节点 */ public NcNode getProgramPackageByName(String programName) { List pkgs = this.lambdaQuery().eq(NcNode::getName, programName).eq(NcNode::getNodeType,NcNode.TYPE_PROGRAM_PACKAGE).list(); if(pkgs.isEmpty()){ return null; }else{ return pkgs.get(0); } } @Transactional(readOnly = true) public List historyByNodeId(Long id) { NcNode node = this.getById(id); return this.baseMapper.historyByParentIdAndName(node.getParentId(),node.getName()); } }