| | |
| | | |
| | | 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.ZipArchiveEntry; |
| | | import org.apache.commons.compress.archivers.zip.ZipFile; |
| | | import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.jetbrains.annotations.NotNull; |
| | | 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.tool.utils.FileUtil; |
| | | 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.entity.FlowProgramFile; |
| | | import org.springblade.mdm.flow.service.ApproveRecordService; |
| | | import org.springblade.mdm.flow.service.FlowProgramFileService; |
| | | 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.entity.NcProgramExchange; |
| | | import org.springblade.mdm.program.mapper.NcProgramExchangeMapper; |
| | | import org.springblade.mdm.program.vo.DncSendBackData; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.*; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * 下发/回传程序统计 |
| | |
| | | @AllArgsConstructor |
| | | public class NcProgramExportDNCService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> { |
| | | |
| | | private final NcProgramService progService; |
| | | private final NcProgramApprovedService approvedService; |
| | | private final ApproveRecordService approveRecordService; |
| | | private final NcNodeService ncNodeService; |
| | | private final OssTemplate ossTemplate; |
| | | private final FlowProgramFileService flowProgramFileService; |
| | | //public static final String PROGRAM_JSON_FILE = "exp_mdm_nc_program.json"; |
| | | public static final String NODE_JSON_FILE = "exp_mdm_nc_node.json"; |
| | | public static final String APPROVE_RECORD_JSON_FILE = "exp_mdm_approve_record.json"; |
| | | |
| | | /** |
| | | * 分页查询 |
| | |
| | | return page; |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 导出dnc压缩包 |
| | | * @param approvedIdArr 待导出审批表id数组 |
| | | */ |
| | | public void exportDnc(Long[] approvedIdArr, OutputStream os) throws IOException { |
| | | //FileOutputStream fos = new FileOutputStream("d:/exportDnc.zip"); |
| | | try (ZipOutputStream zipOut = new ZipOutputStream(os);) {//os |
| | | |
| | | ArrayList<Long> programPackageNodeIdList = new ArrayList<Long>(); |
| | | |
| | | for (Long approvedId : approvedIdArr) { |
| | | NcProgramApproved approved = approvedService.getById(approvedId); |
| | | programPackageNodeIdList.add(approved.getNcNodeId()); |
| | | |
| | | addProgramPackageToZip(zipOut,approved); |
| | | } |
| | | addDataJson(zipOut, programPackageNodeIdList); |
| | | } |
| | | |
| | | os.close(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 将程序包和下属文件加入压缩包 |
| | | * @param zipOut |
| | | * @param approved |
| | | */ |
| | | private void addProgramPackageToZip(ZipOutputStream zipOut, NcProgramApproved approved) throws IOException{ |
| | | String packageFolder = approved.getProgramName()+"/"; |
| | | ZipEntry zipEntry = new ZipEntry(packageFolder);// "/"结尾表示文件夹 |
| | | zipOut.putNextEntry(zipEntry); |
| | | zipOut.closeEntry(); |
| | | |
| | | List<NcNode> programNodes = ncNodeService.lambdaQuery().eq(NcNode::getParentId, approved.getNcNodeId()).list(); |
| | | |
| | | FlowProgramFile programFile; |
| | | for (NcNode node : programNodes) { |
| | | String filePathInZip = packageFolder + node.getName(); |
| | | programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId()); |
| | | |
| | | InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName()); |
| | | |
| | | /* |
| | | ZipEntry fileEntry = new ZipEntry(filePathInZip); |
| | | |
| | | zipOut.putNextEntry(fileEntry); |
| | | programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId()); |
| | | |
| | | InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName()); |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | while ((length = inputStream.read(buffer)) >= 0) { |
| | | zipOut.write(buffer, 0, length); |
| | | } |
| | | zipOut.closeEntry(); |
| | | |
| | | */ |
| | | |
| | | this.addInputStreamToZip(zipOut,inputStream,filePathInZip); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 导入数据文件 |
| | | * @param zipOut |
| | | */ |
| | | void addDataJson(ZipOutputStream zipOut, List<Long> programPackageNodeIdList) throws IOException { |
| | | addNodeDataJson(zipOut, programPackageNodeIdList); |
| | | addApproveRecordDataJson(zipOut, programPackageNodeIdList); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * zip保重加入节点的数据 |
| | | * @param zipOut |
| | | * @param pkgNodeIdList |
| | | * @throws IOException |
| | | */ |
| | | void addNodeDataJson(ZipOutputStream zipOut,List<Long> pkgNodeIdList) throws IOException { |
| | | List<NcNode> pkgNodes = this.ncNodeService.lambdaQuery().in(NcNode::getId, pkgNodeIdList).list(); |
| | | |
| | | List<Long> allNodeIds = new ArrayList<>(); |
| | | for(NcNode node : pkgNodes){ |
| | | allNodeIds.addAll(Func.toLongList(node.getParentIds())); |
| | | allNodeIds.add(node.getId()); |
| | | } |
| | | //加入程序包下级的程序节点 |
| | | List<NcNode> programNodes = ncNodeService.lambdaQuery().in(NcNode::getParentId, pkgNodes.stream().map(NcNode::getId).toList()).list(); |
| | | |
| | | allNodeIds.addAll(programNodes.stream().map(NcNode::getId).toList()); |
| | | List<Long> distinctIds = allNodeIds.stream().distinct().toList(); |
| | | List<NcNode> allNodes = this.ncNodeService.lambdaQuery().in(NcNode::getId, distinctIds).list(); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | |
| | | for(NcNode node : allNodes){ |
| | | JSONObject recObj = getNodeJsonObject(node); |
| | | addSuperProperties(recObj,node); |
| | | jsonArray.add(recObj); |
| | | } |
| | | addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),NODE_JSON_FILE); |
| | | |
| | | } |
| | | |
| | | @NotNull |
| | | private static JSONObject getNodeJsonObject(NcNode node) { |
| | | JSONObject recObj = new JSONObject(); |
| | | recObj.put("id", node.getId()); |
| | | recObj.put("name", node.getName()); |
| | | recObj.put("drawingNo", node.getDrawingNo()); |
| | | recObj.put("drawingNoEdition", node.getDrawingNoEdition()); |
| | | recObj.put("productModel", node.getProductModel()); |
| | | recObj.put("processName", node.getProcessName()); |
| | | recObj.put("processEdition", node.getProcessEdition()); |
| | | recObj.put("craftEdition", node.getCraftEdition()); |
| | | recObj.put("parentIds", node.getParentIds()); |
| | | recObj.put("processNo", node.getProcessNo()); |
| | | |
| | | recObj.put("isCured", node.getIsCured()); |
| | | recObj.put("isLocked", node.getIsLocked()); |
| | | recObj.put("nodeType", node.getNodeType()); |
| | | |
| | | recObj.put("machineCode", node.getMachineCode()); |
| | | recObj.put("flowProgramFileId", node.getFlowProgramFileId()); |
| | | recObj.put("processInstanceId", node.getProcessInstanceId()); |
| | | return recObj; |
| | | } |
| | | |
| | | /** |
| | | * 导入审批记录 |
| | | * @param zipOut |
| | | * @param programPackageNodeIdList 程序包名的id列表 |
| | | */ |
| | | void addApproveRecordDataJson(ZipOutputStream zipOut, List<Long> programPackageNodeIdList) throws IOException { |
| | | /* |
| | | List<String> instanceIds = this.ncNodeService.lambdaQuery().in(NcNode::getId, programPackageNodeIdList) |
| | | .list().stream().map(NcNode::getProcessInstanceId).toList(); |
| | | */ |
| | | //List<ApproveRecord> records = approveRecordService.lambdaQuery().in(ApproveRecord::getProcessInstanceId, instanceIds).list(); |
| | | List<ApproveRecord> records = approveRecordService.lambdaQuery().in(ApproveRecord::getNcNodeId, programPackageNodeIdList).list(); |
| | | |
| | | 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("processInstanceId",record.getProcessInstanceId()); |
| | | recObj.put("ncNodeId",record.getNcNodeId()); |
| | | addSuperProperties(recObj,record); |
| | | |
| | | jsonArray.add(recObj); |
| | | } |
| | | addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),"exp_mdm_approve_record.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()); |
| | | } |
| | | |
| | | /** |
| | | * 将 输入流 中的内容写入zip |
| | | * @param zipOut zip输出流 |
| | | * @param inputStream 输入流 |
| | | * @param entryName 文件名 |
| | | * @throws IOException |
| | | */ |
| | | 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(); |
| | | } |
| | | } |
| | | |