yangys
2025-07-04 fc588c6e5ccac038cab378931d9bac3033e28f98
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
 
package org.springblade.mdm.program.service;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.zip.ZipUtil;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.flow.entity.ApproveRecord;
import org.springblade.mdm.flow.service.ApproveRecordService;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcProgram;
import org.springblade.mdm.program.entity.NcProgramApproved;
import org.springblade.mdm.program.mapper.NcProgramApprovedMapper;
import org.springblade.mdm.program.vo.DncSendBackData;
import org.springblade.mdm.program.vo.NcProgramExportDncPageVO;
import org.springblade.mdm.program.vo.NcProgramExportDncQueryVO;
import org.springblade.mdm.utils.CustomBinaryWriter;
import org.springframework.stereotype.Service;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * 下发/回传程序统计
 *
 * @author yangys
 */
@Slf4j
@Service
@AllArgsConstructor
public class NcProgramApprovedService extends BizServiceImpl<NcProgramApprovedMapper, NcProgramApproved> {
    private final NcProgramService progService;
    private final OssTemplate ossTemplate;
    private final ApproveRecordService approvedService;
    private final NcNodeService ncNodeService;
    /**
     * 分页查询
     * @param query 查询参数
     * @return
     */
    public IPage<NcProgramExportDncPageVO> exportDncPageQuery(NcProgramExportDncQueryVO query) {
 
        IPage<NcProgramExportDncPageVO> page = this.getBaseMapper().exportDncPageQuery(Condition.getPage(query),query);
 
        return page;
 
    }
 
    /**
     * 导出dnc
     * @param approvedIdArr 待导出审批表id数组
     */
    public void exportDnc(Long[] approvedIdArr, OutputStream os) throws IOException {
        ZipOutputStream zipOut = new ZipOutputStream(os);
 
        ArrayList<Long> programIdList = new ArrayList<Long>();
 
        for (Long approvedId : approvedIdArr) {
            NcProgramApproved approved = this.getById(approvedId);
            programIdList.add(approved.getNcProgramId());
 
            NcProgram prog = progService.getById(approved.getNcProgramId());
 
            String filename = prog.getOssName();
            InputStream inputStream = ossTemplate.statFileStream(filename);
 
            addInputStreamToZip(zipOut, inputStream, prog.getName());
 
        }
        addDataJson(zipOut, programIdList);
 
    }
 
    /**
     * 导入数据文件
     * @param zipOut
     */
    void addDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
        addProgramDataJson(zipOut, programIdList);
        addApproveRecordDataJson(zipOut, programIdList);
 
        addNcNodeDataJson(zipOut, programIdList);
    }
 
    /**
     * 导入程序记录
     * @param zipOut
     * @param programIdList
     */
    void addProgramDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
        LambdaQueryWrapper<NcProgram> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(NcProgram::getId, programIdList);
        List<NcProgram> programList = progService.list(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        for(NcProgram program : programList){
            JSONObject recObj = new JSONObject();
            recObj.put("id", program.getId());
            recObj.put("code", program.getCode());
            recObj.put("ossName",program.getOssName());
            recObj.put("isTextFile",program.getIsTextFile());
            recObj.put("category",program.getCategory());
            recObj.put("description",program.getDescription());
            recObj.put("name",program.getName());
            recObj.put("partNo",program.getPartNo());
            recObj.put("ncNodeId",program.getNcNodeId());
            recObj.put("url",program.getUrl());
            recObj.put("isCured",program.getIsCured());
            recObj.put("isLocked",program.getIsLocked());
            recObj.put("isTest",program.getIsTest());
            recObj.put("machineCode",program.getMachineCode());
            recObj.put("processEdition",program.getProcessEdition());
            recObj.put("taskAssignTime",program.getTaskAssignTime());
 
            addSuperProperties(recObj,program);
 
            jsonArray.add(recObj);
        }
        addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),"exp_mdm_nc_program.json");
 
    }
 
    /**
     * 导入审批记录
     * @param zipOut
     * @param programIdList
     */
    void addApproveRecordDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
        LambdaQueryWrapper<ApproveRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ApproveRecord::getNcProgramId, programIdList);
        List<ApproveRecord> records = approvedService.list(queryWrapper);
        JSONArray jsonArray = new JSONArray();
        for(ApproveRecord record : records){
            JSONObject recObj = new JSONObject();
            recObj.put("id", record.getId());
            recObj.put("comment", record.getComment());
            recObj.put("userId",record.getUserId());
            recObj.put("userNickname",record.getUserNickname());
            recObj.put("operateTime",record.getOperateTime());
            recObj.put("operateResult",record.getOperateResult());
            recObj.put("taskName",record.getTaskName());
            recObj.put("ncProgramId",record.getNcProgramId());
            recObj.put("processInstanceId",record.getProcessInstanceId());
 
            addSuperProperties(recObj,record);
 
            jsonArray.add(recObj);
        }
        addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),"exp_mdm_approve_record.json");
 
    }
 
    /**
     * 导入节点
     * @param zipOut
     * @param programIdList
     */
    void addNcNodeDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
        LambdaQueryWrapper<NcProgram> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(NcProgram::getId, programIdList);
 
        List<NcProgram> programs = progService.list(queryWrapper);
        JSONArray jsonArray = new JSONArray();
 
        ArrayList<Long> exportNodeIdList     = new ArrayList<>();
        for(NcProgram program : programs){
            JSONObject recObj = new JSONObject();
            if(!exportNodeIdList.contains(program.getNcNodeId())){
                exportNodeIdList.add(program.getNcNodeId());
            }
            NcNode ncNode = ncNodeService.getById(program.getNcNodeId());
 
            if(StringUtils.isNotEmpty(ncNode.getParentIds())){
                List<Long> pids = Func.toLongList(ncNode.getParentIds());
                for(Long nodeId : pids){
                    if(!exportNodeIdList.contains(nodeId)){
                        exportNodeIdList.add(nodeId);
                    }
                }
 
            }
 
        }
 
        LambdaQueryWrapper<NcNode> nodeQueryWrapper = new LambdaQueryWrapper<>();
        nodeQueryWrapper.in(NcNode::getId, exportNodeIdList);
        List<NcNode> nodeList =ncNodeService.list(nodeQueryWrapper);
        for(NcNode node : nodeList){
            JSONObject recObj = new JSONObject();
 
            recObj.put("id", node.getId());
            recObj.put("nodeType", node.getNodeType());
            recObj.put("machineCode",node.getMachineCode());
            recObj.put("parentId",node.getParentId());
            recObj.put("description",node.getDescription());
            recObj.put("name",node.getName());
            recObj.put("remark",node.getRemark());
            recObj.put("partNo",node.getPartNo());
            recObj.put("parentIds",node.getParentIds());
            recObj.put("processName",node.getProcessName());
 
            addSuperProperties(recObj,node);
 
            jsonArray.add(recObj);
        }
        addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),"exp_mdm_nc_node.json");
    }
 
    void addSuperProperties(JSONObject recObj, BizEntity entity){
        recObj.put("tenantId",entity.getTenantId());
        recObj.put("createTime",entity.getCreateTime());
        recObj.put("updateTime",entity.getUpdateTime());
        recObj.put("createUser",entity.getCreateUser());
        recObj.put("updateUser",entity.getUpdateUser());
        recObj.put("status",entity.getStatus());
        recObj.put("createDept",entity.getCreateDept());
    }
 
    public void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName)
        throws IOException {
        // 创建新的 ZIP 条目
        ZipEntry zipEntry = new ZipEntry(entryName);
        zipOut.putNextEntry(zipEntry);
 
        // 将输入流写入 ZIP 输出流
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) >= 0) {
            zipOut.write(buffer, 0, length);
        }
 
        // 关闭当前条目
        zipOut.closeEntry();
    }
}