yangys
2025-08-06 053a438acf9ec33e182b25211ce8108f0367feac
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcNodeService.java
@@ -10,13 +10,14 @@
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.NcNodeProgramQueryVO;
import org.springblade.mdm.program.vo.NcNodeProgramVO;
import org.springblade.mdm.program.vo.NcNodeVO;
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;
/**
 * 程序节点
@@ -82,14 +83,6 @@
      return baseMapper.lazyList(parentId);
   }
   /**
    * 程序节点
    * @param query
    * @return
    */
   public IPage<NcNodeProgramVO> programPageQuery(NcNodeProgramQueryVO query) {
      return this.getBaseMapper().programPageQuery(Condition.getPage(query),query);
   }
   /**
    * 查询现有固化的程序,暂定条件:零组件号相同,且是同一机床组
@@ -102,4 +95,132 @@
   }
   /**
    * 首页树查询
    * @param queryVO
    * @return
    */
   public List<NcNodeVO> searchList(NcNodeQueryVO queryVO) {
      //1.根据关进字和节点类型查询初始列表,查询的原始列表,需要hasChild字段,所以使用mapper.xml
      List<NcNodeVO> oriList = this.getBaseMapper().searchList(queryVO);
      //List<NcNode> list = this.lambdaQuery().eq(NcNode::getNodeType, queryVO.getNodeType()).like(NcNode::getName, queryVO.getName()).list();
      if ("10".equals(queryVO.getNodeType())) {
         //搜索的根级别,直接返回
         return oriList;
      }
      List<NcNodeVO> allVos = new ArrayList<NcNodeVO>();//已经加入过的节点,用于去重
      List<NcNodeVO> rootVos = new ArrayList<NcNodeVO>();
      //List<NcNode> rootNodes = new ArrayList<>();
      for(NcNodeVO vo : oriList){
         allVos.add(vo);
         //上级各级的节点
         List<NcNodeVO> 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<NcNodeVO> 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<NcNode> 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);
      }
   }
   /**
    * 查询节点的历史列表(节点的上级节点”程序包名“因为存在多个版本,所以历史记录也需要根据不同版本程序包名进行查询)
    * @param id
    * @return
    */
   @Transactional(readOnly = true)
    public List<NcNodeVO> historyByNodeId(Long id) {
      NcNode node = this.getById(id);
      NcNode parentNode = this.getById(node.getParentId());
      return this.baseMapper.historyByParentIdAndName(node.getName(),parentNode);
    }
   /**
    * 获取“程序包名”的最新版本
    * @param name 节点名称
    * @return 最新版本程序报名节点
    */
   public NcNode getLastEditionProgramPackage(String name){
      List<NcNode> pkgList = lambdaQuery().eq(NcNode::getNodeType,NcNode.TYPE_PROGRAM_PACKAGE)
         .eq(NcNode::getName, name).eq(NcNode::getIsLastEdition,1).orderByDesc(NcNode::getCreateTime).list();
      if(pkgList.isEmpty()){
         return null;
      }else{
         return pkgList.get(0);
      }
   }
   /**
    * 根据父节点和名称,查询文件界节点列表
    * @param name 节点/文件名称
    * @param parentId 父节点id
    */
    public NcNode getLastEditionProgramFile(String name,Long parentId) {
      List<NcNode> nodes = lambdaQuery().eq(NcNode::getNodeType,NcNode.TYPE_PROGRAM_FILE)
         .eq(NcNode::getName, name).eq(NcNode::getParentId,parentId)
         .eq(NcNode::getIsLastEdition,1).list();
      if(nodes.isEmpty()){
         return null;
      }else {
         return nodes.get(0);
      }
   }
   /**
    * 获取node的历史列表(同一个父节点下,同名的所有节点)
    * @param pkgNode node
    * @return 历史列表,报错节点本身
    */
    public List<NcNode> getNodeHistorys(NcNode pkgNode) {
      return this.lambdaQuery().eq(NcNode::getParentId,pkgNode.getParentId())
         .eq(NcNode::getName,pkgNode.getName()).list();
    }
}