| | |
| | | |
| | | 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 com.alibaba.csp.sentinel.util.StringUtil; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springblade.core.mp.base.BizEntity; |
| | | import org.flowable.engine.HistoryService; |
| | | import org.flowable.engine.history.HistoricProcessInstance; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | 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.basesetting.machine.service.MachineService; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.flow.constants.FlowConstant; |
| | | import org.springblade.mdm.flow.entity.FlowProgramFile; |
| | | 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.NcNodeHis; |
| | | 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.springblade.mdm.program.service.programannotation.AnnotationData; |
| | | import org.springblade.mdm.program.service.programannotation.AnnotationProcessor; |
| | | import org.springblade.mdm.program.service.programannotation.MachineAnnotationConfig; |
| | | import org.springblade.mdm.program.service.programannotation.ProcessorHelper; |
| | | import org.springblade.system.feign.IDictClient; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | |
| | | @AllArgsConstructor |
| | | public class NcProgramExportDNCService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> { |
| | | |
| | | private final NcProgramService progService; |
| | | private final IDictClient dictClient; |
| | | private final MachineService machineService; |
| | | private final NcProgramApprovedService approvedService; |
| | | private final ApproveRecordService approveRecordService; |
| | | private final NcNodeService ncNodeService; |
| | | private final NcNodeHisService ncNodeHisService; |
| | | private final OssTemplate ossTemplate; |
| | | private final FlowProgramFileService flowProgramFileService; |
| | | private final HistoryService historyService; |
| | | |
| | | 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"; |
| | | |
| | | /** |
| | | * 是否压缩包内的数据文件 |
| | | * @param filename 文件名称 |
| | | * @return |
| | | */ |
| | | public static boolean isDataFile(String filename){ |
| | | return StringUtils.equals(filename, PROGRAM_JSON_FILE) || StringUtils.equals(filename, NODE_JSON_FILE) || StringUtils.equals(filename, APPROVE_RECORD_JSON_FILE); |
| | | } |
| | | /** |
| | | * 分页查询 |
| | | * @param query 查询参数 |
| | | * @return |
| | | */ |
| | | public IPage<DncSendBackData> dncSendBackPageQuery(Query query) { |
| | | |
| | | IPage<DncSendBackData> page = this.getBaseMapper().dncSendBackpageQuery(Condition.getPage(query),query); |
| | | |
| | | return page; |
| | | |
| | | } |
| | | |
| | | |
| | | private final MachineAnnotationConfig annotationConfig; |
| | | /** |
| | | * 导出dnc压缩包 |
| | | * @param approvedIdArr 待导出审批表id数组 |
| | | * @param approvedIdArray 待导出审批表id数组 |
| | | */ |
| | | public void exportDnc(Long[] approvedIdArr, OutputStream os) throws IOException { |
| | | public void exportDnc(Long[] approvedIdArray, OutputStream os) throws IOException { |
| | | //FileOutputStream fos = new FileOutputStream("d:/exportDnc.zip"); |
| | | try (ZipOutputStream zipOut = new ZipOutputStream(os);) {//os |
| | | try (ZipOutputStream zipOut = new ZipOutputStream(os);os) {//os |
| | | |
| | | ArrayList<Long> programIdList = new ArrayList<Long>(); |
| | | |
| | | for (Long approvedId : approvedIdArr) { |
| | | for (Long approvedId : approvedIdArray) { |
| | | NcProgramApproved approved = approvedService.getById(approvedId); |
| | | programIdList.add(approved.getNcProgramId()); |
| | | addProgramPackageToZip(zipOut,approved); |
| | | |
| | | NcProgram prog = progService.getById(approved.getNcProgramId()); |
| | | approved.setStatus(NcProgramApproved.STATUS_EXPORTED); |
| | | approvedService.updateById(approved); |
| | | |
| | | String filename = prog.getOssName(); |
| | | InputStream inputStream = ossTemplate.statFileStream(filename); |
| | | |
| | | addInputStreamToZip(zipOut, inputStream, prog.getName()); |
| | | |
| | | } |
| | | addDataJson(zipOut, programIdList); |
| | | } |
| | | |
| | | os.close(); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 导入数据文件 |
| | | * @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("isLastEdition",program.getIsLastEdition()); |
| | | recObj.put("category",program.getCategory()); |
| | | recObj.put("description",program.getDescription()); |
| | | recObj.put("name",program.getName()); |
| | | recObj.put("drawingNo",program.getDrawingNo()); |
| | | recObj.put("ncNodeId",program.getNcNodeId()); |
| | | recObj.put("bindNcNodeId",program.getBindNcNodeId()); |
| | | 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)),PROGRAM_JSON_FILE); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 导入审批记录 |
| | | * @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 = approveRecordService.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.getBindNcNodeId())){ |
| | | exportNodeIdList.add(program.getBindNcNodeId()); |
| | | } |
| | | NcNode ncNode = ncNodeService.getById(program.getBindNcNodeId());//从绑定的节点本身开始导出 |
| | | |
| | | if(StringUtils.isNotEmpty(ncNode.getParentIds())){ |
| | | List<Long> pids = Func.toLongList(ncNode.getParentIds()); |
| | | for(Long nodeId : pids){ |
| | | if(!exportNodeIdList.contains(nodeId)){ |
| | | exportNodeIdList.add(nodeId); |
| | | NcNode pkgNode = this.ncNodeService.getById(approved.getNcNodeId()); |
| | | if(pkgNode.isDeviationProgram()){ |
| | | //偏离单的程序,如果审批导出记录是解锁形成的,还是需要锁定;普通试切过的,不锁定 |
| | | HistoricProcessInstance hisInst = historyService.createHistoricProcessInstanceQuery().processInstanceId(approved.getProcessInstanceId()).singleResult(); |
| | | if(hisInst != null && hisInst.getProcessDefinitionKey().equals(FlowConstant.UNLOCK_PROCESS_KEY)){ |
| | | pkgNode.lock("下发导出锁定"); |
| | | ncNodeService.updateById(pkgNode); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | }catch(Exception e){ |
| | | Throwable [] err = e.getSuppressed(); |
| | | throw new ServiceException("导出工控网错误"+e.getMessage()); |
| | | } |
| | | |
| | | 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("drawingNo",node.getDrawingNo()); |
| | | 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()); |
| | | /** |
| | | * 将程序包和下属文件加入压缩包 |
| | | * @param zipOut |
| | | * @param approved |
| | | */ |
| | | private void addProgramPackageToZip(ZipOutputStream zipOut, NcProgramApproved approved) throws IOException{ |
| | | NcNodeHis packageNodeHis = ncNodeHisService.getById(approved.getNcNodeId()); |
| | | |
| | | String packageFolder = packageNodeHis.getName()+"/"; |
| | | ZipEntry zipEntry = new ZipEntry(packageFolder);// "/"结尾表示文件夹 |
| | | zipOut.putNextEntry(zipEntry); |
| | | zipOut.closeEntry(); |
| | | |
| | | Machine machine = machineService.getByCode(packageNodeHis.getMachineCode());//程序包节点,获取注释用 |
| | | |
| | | //List<NcNode> programNodes = ncNodeService.lambdaQuery().eq(NcNode::getIsLastEdition,1).eq(NcNode::getParentId, approved.getNcNodeId()).list(); |
| | | List<NcNodeHis> programNodes = ncNodeHisService.lambdaQuery().eq(NcNodeHis::getParentId, approved.getNcNodeId()).list(); |
| | | |
| | | FlowProgramFile programFile; |
| | | NcNodeHis programPackageNode = this.ncNodeHisService.getById(approved.getNcNodeId()); |
| | | String status = "SQ"; |
| | | if(programPackageNode.isDeviationProgram()){ |
| | | status = "PL"; |
| | | }else if(programPackageNode.hasCured()){ |
| | | status = "GH"; |
| | | } |
| | | long deviationSerial = -1;//偏离单序号 ,-1为非偏离单,不需要加入 |
| | | if(packageNodeHis.isDeviationProgram()) { |
| | | deviationSerial = ncNodeHisService.getDeviationSerialForNode(packageNodeHis); |
| | | } |
| | | |
| | | //List<DictBiz> annotationDictList= programAnnotationService.getAnnotionDictList(); |
| | | for (NcNodeHis node : programNodes) { |
| | | String filePathInZip = genFilePathInZip(packageFolder,node,deviationSerial); |
| | | programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId()); |
| | | if(programFile.isProgram()) {//程序文件,才会加入压缩包 |
| | | InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName()); |
| | | String sendDir = machine.getProgSendDir()==null? StringUtil.EMPTY:machine.getProgSendDir(); |
| | | |
| | | AnnotationProcessor annoProcessor = ProcessorHelper.getProcessor(machine.getControlSystem(),annotationConfig); |
| | | AnnotationData annoData = new AnnotationData(); |
| | | annoData.setSendPath(sendDir); |
| | | annoData.setProgramStatus(status); |
| | | annoData.setDeviation(programPackageNode.getDeviation()); |
| | | annoData.setFilename(programFile.getName()); |
| | | InputStream addedInsFinal = annoProcessor.putAnnotation(annoData,inputStream); |
| | | addedInsFinal.reset(); |
| | | /* |
| | | InputStream addedInsFinal; |
| | | InputStream addedIns2 = programAnnotationService.setSendDirAndStatusAnnotation(sendDir,status,inputStream,machine.getControlSystem()); |
| | | if(StringUtils.isNotBlank(programPackageNode.getDeviation())) { |
| | | //偏离单,增加偏离单号 |
| | | addedInsFinal = programAnnotationService.setPldAnnotation(programPackageNode.getDeviation(),addedIns2, machine.getControlSystem(), annotationDictList);; |
| | | }else{ |
| | | addedInsFinal = addedIns2; |
| | | }*/ |
| | | this.addInputStreamToZip(zipOut, addedInsFinal, filePathInZip); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | public void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName) |
| | | /** |
| | | * 生成在zip中的文件路径 |
| | | * @param packageFolder zip中的文件夹 |
| | | * @param programNode 程序节点 |
| | | * @param deviationSerial 偏离单序号 |
| | | * @return zip中的文件路径 |
| | | */ |
| | | String genFilePathInZip(String packageFolder,NcNodeHis programNode,long deviationSerial){ |
| | | //为文件名增加偏离单号 |
| | | String filePathInZip; |
| | | |
| | | if(deviationSerial != -1){ |
| | | //加入偏离单序号 |
| | | String ext = FilenameUtils.getExtension(programNode.getName()); |
| | | String dotExt = StringUtils.isNotBlank(ext)?"."+ext:ext;//带点的扩展名 |
| | | |
| | | String temp = programNode.getName(); |
| | | if(StringUtils.isNotBlank(ext)) { |
| | | temp = StringUtils.removeEnd(temp,dotExt); |
| | | } |
| | | filePathInZip = packageFolder + temp+"-P"+deviationSerial+dotExt; |
| | | }else{ |
| | | //非偏离单,直接使用原文件名 |
| | | filePathInZip = packageFolder + programNode.getName(); |
| | | } |
| | | return filePathInZip; |
| | | } |
| | | /** |
| | | * 将 输入流 中的内容写入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); |