yangys
2025-08-06 1911be8941e5fe2705c2c56e74e52bd426468793
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
 
package org.springblade.mdm.program.service;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.IoUtil;
import org.springblade.mdm.flow.entity.ApproveRecord;
import org.springblade.mdm.flow.service.ApproveRecordService;
import org.springblade.mdm.flow.service.CureFlowService;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcProgram;
import org.springblade.mdm.program.entity.NcProgramExchange;
import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
import org.springblade.mdm.program.vo.MdmProgramImportVO;
import org.springblade.mdm.utils.CustomBinaryReader;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.*;
 
/**
 * MDM程序导入(工控网功能)
 * 目前工控网展现形式未定,暂时实现暂停
 * @author yangys
 */
@Slf4j
@Service
@AllArgsConstructor
public class MdmProgramImportService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
    private final CureFlowService cureFlowService;
    private final NcProgramService ncProgramService;
    private final NcNodeService ncNodeService;
    private final ApproveRecordService approveRecordService;
    private final BladeRedis bladeRedis;
    private final OssTemplate ossTemplate;
 
    private String getFileKey(){
        return "mdmimpfile-"+ AuthUtil.getUserId();
    }
    /**
     * MDM导入文件上传
     * @param file MDM涉密网导出文件
     * @return
     */
    public List<MdmProgramImportVO> mdmImportUpload(MultipartFile file) {
        List<MdmProgramImportVO> list;
        try {
            BladeFile bfile = ossTemplate.putFile(file);//上传,供后续入库使用
 
            //设置一个缓存,2小时过期
            bladeRedis.setEx(getFileKey(),bfile.getName(), Duration.ofHours(2));
            String pkgFileName = bladeRedis.get(getFileKey());
            InputStream zipFileInputStream = file.getInputStream();//test
 
            byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream);
            list = parseMdmZipFromByteArray(bytes);
 
 
        } catch (IOException e) {
            log.error("导入涉密网摆渡文件失败",e);
            throw new ServiceException("解析DNC回传数据失败");
        }
        return list;
    }
 
    public static List<MdmProgramImportVO> parseMdmZipFromByteArray(byte[] zipData) throws IOException {
        List<MdmProgramImportVO> list = new ArrayList<>();
        Map<String,String> fileMd5Map = new HashMap<>();
        Map<String,MdmProgramImportVO> fileDataMap = new HashMap<>();
        try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(zipData);
             ZipFile zipFile = new ZipFile(channel)) {
 
            ZipArchiveEntry entry;
            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
 
                String entryName = entry.getName();
 
                if (!entry.isDirectory()) {
                    //直接解析程序的json文件
 
 
                }
 
            }
 
        }
        //设置md5值
        fileDataMap.forEach((k,v)->{
            if(fileMd5Map.containsKey(k)){
                v.setMd5(fileMd5Map.get(k));
            }
        });
        return list;
    }
 
 
    /**
     * 入库mdm涉密网文件
     * @param ids id列表逗号分隔
     * @return
     */
    public void mdmFileAccept(String ids) throws IOException {
 
        String pkgFileName = bladeRedis.get(getFileKey());
 
        List<Long> idList = Func.toLongList(ids);
        //
        updateProgramFile(pkgFileName,idList);
 
    }
 
    /**
     * 更新程序文件
     * @param pkgFileName mdm程序包
     * @param idList 选定入库的
     */
    private void updateProgramFile(String pkgFileName,List<Long> idList) throws IOException {
        InputStream inputStream = this.ossTemplate.statFileStream(pkgFileName);
        byte[] bytes = FileUtil.copyToByteArray(inputStream);
 
        List<NcNode> nodeList = new ArrayList<>();
        List<NcProgram> progList = new ArrayList<>();
        List<ApproveRecord> recordList = new ArrayList<>();
 
        try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
             ZipFile zipFile = new ZipFile(channel)) {
 
            ZipArchiveEntry entry;
            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
 
                String entryName = entry.getName();
 
            }
 
        }
 
 
 
        for(NcNode node:nodeList){
            NcNode nodeTemp = this.ncNodeService.getById(node.getId());
            if(nodeTemp == null){
                ncNodeService.save(node);
            }else{
                ncNodeService.updateById(node);
            }
        }
 
        for(NcProgram prog:progList){
            NcProgram ncTemp = ncProgramService.getById(prog.getId());
            if(ncTemp == null){
                ncProgramService.save(prog);
            }else{
                ncProgramService.updateById(prog);
            }
        }
 
        for(ApproveRecord record:recordList) {
            ApproveRecord recordTemp = this.approveRecordService.getById(record.getId());
            if (recordTemp == null) {
                approveRecordService.save(record);
            } else {
                approveRecordService.updateById(record);
            }
        }
 
    }
 
    void setBaseProperties(BizEntity entity,JSONObject jsonObject){
        entity.setCreateTime(jsonObject.getDate("createTime"));
        entity.setUpdateTime(jsonObject.getDate("updateTime"));
        entity.setStatus(jsonObject.getInteger("status"));
        entity.setCreateUser(jsonObject.getLong("createUser"));
        entity.setUpdateUser(jsonObject.getLong("updateUser"));
    }
}