yangys
2025-12-03 b4d10ff7535002dddb63a0b28ddb37fee7ed1e9d
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
 
package org.springblade.mdm.program.controller;
 
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.service.FlowProgramFileService;
import org.springblade.mdm.flow.service.TaskDispatchService;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcNodeHis;
import org.springblade.mdm.program.service.NcNodeHisService;
import org.springblade.mdm.program.service.NcNodeService;
import org.springblade.mdm.program.service.ProgramFlowStatusQueryService;
import org.springblade.mdm.program.vo.CompareDataVO;
import org.springblade.mdm.program.vo.NcNodeOldQueryVO;
import org.springblade.mdm.program.vo.NcNodeQueryVO;
import org.springblade.mdm.program.vo.NcNodeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
 
/**
 * 程序节点
 *
 * @author yangys
 */
@NonDS
@RestController
@RequestMapping("/program/nodehis")
@AllArgsConstructor
@Tag(name = "程序节点", description = "程序节点")
@Slf4j
public class NcNodeHisController {
    private final OssTemplate ossTemplate;
    private final NcNodeHisService nodeHisService;
    private final FlowProgramFileService flowProgramFileService;
 
 
    @GetMapping("/history-by-nodeid")
    @Operation(summary = "根据绑定节点id获取历史列表", description = "程序历史列表,仅‘程序包’字典值60的数据。用于显示程序的‘历史版本’")
    public R<List<NcNodeVO>> historyByBindNodeId(@Parameter(description="节点ID(nodeType=60的节点id)")@RequestParam Long id) {
        return R.data(this.nodeHisService.historyByNodeId(id));
    }
 
    @GetMapping("/files-by-parent-id")
    @Operation(summary = "获取历史程序包文件列表", description = "获取历史程序包文件列表")
    public R<List<NcNodeVO>> filesByParentNodeId(@Parameter(description="节点ID(nodeType=60的节点id)")@RequestParam Long id) {
        List<NcNodeHis> nodes = nodeHisService.lambdaQuery().eq(NcNodeHis::getParentId,id).orderByDesc(NcNodeHis::getCreateTime).list();
        List<NcNodeVO> voList = new ArrayList<>(nodes.stream().map(nh -> {NcNodeVO vo = new NcNodeVO();
            BeanUtils.copyProperties(nh,vo);
            return vo;
        }).toList());
        NcNodeHis pkgNode = nodeHisService.getById(id);
        //文件,按照先程序,后其他排序
        if(!voList.isEmpty()) {
            if (voList.get(0).getNodeType().equals(NcNode.TYPE_PROGRAM_FILE)) {
                Comparator<NcNodeVO> cp = new Comparator<>() {
                    @Override
                    public int compare(NcNodeVO n1, NcNodeVO n2) {
                        if (n1.getName().startsWith(pkgNode.getName()) && !n2.getName().startsWith(pkgNode.getName())) {
                            return -1;
                        } else {
                            return 1;
                        }
                    }
                };
                voList.sort(cp.thenComparing(NcNodeVO::getName));
 
            }
        }
        return R.data(voList);
    }
 
    @GetMapping("/content-by-nodeid")
    @Operation(summary = "根据节点获取文件内容", description = "仅限文本格式的内容,二进制文件将返回空串")
    public R<String> fileContentByNodeId(@Parameter(description = "节点id") Long nodeId) {
        try {
            NcNodeHis node = this.nodeHisService.getById(nodeId);
            if(node.getFlowProgramFileId() != null) {
                return R.data(flowProgramFileService.getFileContent(node.getFlowProgramFileId()));
            }else{
                return R.data("找不到文件");
            }
        }catch(Exception e) {
            log.error("获取文件内容失败",e);
            return R.fail(e.getMessage());
        }
    }
 
    @GetMapping("/link-by-nodeid")
    @Operation(summary = "根据节点获取文件内容", description = "仅限文本格式的内容,二进制文件将返回空串")
    public R<String> fileLinkByNodeId(@Parameter(description = "节点id") Long nodeId) {
        try {
            NcNodeHis node = this.nodeHisService.getById(nodeId);
            FlowProgramFile flowFile = this.flowProgramFileService.getById(node.getFlowProgramFileId());
 
            if(node.getFlowProgramFileId() != null || flowFile == null) {
                return R.data(this.ossTemplate.fileLink(flowFile.getOssName()));
            }else{
                return R.data("");
            }
        }catch(Exception e) {
            log.error("获取文件内容失败",e);
            return R.fail(e.getMessage());
        }
    }
 
    @GetMapping("/download-by-nodeid")
    @Operation(summary = "下载节点对应的文件", description = "下载节点对应的文件")
    public void downloadByNodeId(@Parameter(description = "节点id") Long nodeId, HttpServletResponse response) throws IOException {
 
        NcNodeHis node = nodeHisService.getById(nodeId);
        if(node.getFlowProgramFileId() != null) {
            flowProgramFileService.download(node.getFlowProgramFileId(),response);
        }else{
            log.error("非文件节点");
            throw new ServiceException("节点无文件id");
        }
 
    }
}