yangys
2025-07-25 86d0a38916d2edb66e451c44f1f1b9729dee965b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 
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.NcNodeProgramQueryVO;
import org.springblade.mdm.program.vo.NcNodeProgramVO;
import org.springblade.mdm.program.vo.NcNodeQueryVO;
import org.springblade.mdm.program.vo.NcNodeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * 程序节点
 *
 * @author yangys
 */
@Slf4j
@Service
@AllArgsConstructor
public class NcNodeService extends BizServiceImpl<NcNodeMapper, NcNode> {
 
 
    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<NcNodeVO> 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<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;
    }
 
}