package com.lxzn.webservice.impl;
|
|
import cn.hutool.core.util.StrUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.lxzn.activiti.service.IAssignFileStreamService;
|
import com.lxzn.framework.domain.filesystem.response.FileUploadResult;
|
import com.lxzn.framework.domain.nc.*;
|
import com.lxzn.framework.domain.nc.response.DocumentCode;
|
import com.lxzn.framework.domain.webservice.DispatchInfo;
|
import com.lxzn.framework.domain.webservice.PlmProgramSourceInfo;
|
import com.lxzn.framework.domain.webservice.ProcedureFinish;
|
import com.lxzn.framework.domain.webservice.request.NcProgramInfo;
|
import com.lxzn.framework.domain.webservice.request.PlmProgramSource;
|
import com.lxzn.framework.exception.ExceptionCast;
|
import com.lxzn.framework.model.response.CommonCode;
|
import com.lxzn.framework.utils.CxfClientUtil;
|
import com.lxzn.framework.utils.HttpRequest;
|
import com.lxzn.framework.utils.ValidateUtil;
|
import com.lxzn.framework.utils.ZipUtil;
|
import com.lxzn.framework.utils.file.FileUtil;
|
import com.lxzn.nc.service.*;
|
import com.lxzn.webservice.DncWebService;
|
import com.lxzn.framework.domain.webservice.request.WsDispatch;
|
import com.lxzn.framework.domain.webservice.request.WsProcedureFinish;
|
import com.lxzn.webservice.ext.MesResultModel;
|
import com.lxzn.webservice.service.IDispatchInfoService;
|
import com.lxzn.webservice.service.IPlmProgramSourceInfoService;
|
import com.lxzn.webservice.service.IProcedureFinishService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.io.FilenameUtils;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import javax.jws.WebMethod;
|
import javax.jws.WebParam;
|
import javax.jws.WebService;
|
import java.io.*;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.nio.file.StandardCopyOption;
|
import java.util.*;
|
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.stream.Collectors;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipFile;
|
import java.util.zip.ZipInputStream;
|
|
/**
|
* @Author: Lius
|
* @CreateTime: 2025-02-27
|
* @Description:
|
*/
|
@Service
|
@WebService(name = "DncWebService",
|
targetNamespace = "http://lxzn.webservice.com",
|
endpointInterface = "com.lxzn.webservice.DncWebService"
|
)
|
@Slf4j
|
public class DncWebServiceImpl implements DncWebService {
|
|
@Value("${ncPdm.file_path}")
|
private String filePath;
|
/**
|
* url
|
*/
|
@Value("${webservice.url}")
|
private String url;
|
|
/**
|
* namespace
|
*/
|
@Value("${webservice.namespace}")
|
private String namespace;
|
|
/**
|
* method
|
*/
|
@Value("${webservice.method}")
|
private String method;
|
@Resource
|
private IDispatchInfoService dispatchInfoService;
|
@Resource
|
private IProcedureFinishService procedureFinishService;
|
@Resource
|
private IDeviceInfoService deviceInfoService;
|
@Resource
|
private IAssignFileStreamService assignFileStreamService;
|
@Resource
|
private IPlmProgramSourceInfoService iPlmProgramSourceInfoService;
|
@Autowired
|
private IProductInfoService productInfoService;
|
@Autowired
|
private IComponentInfoService componentInfoService;
|
@Autowired
|
private IPermissionStreamService permissionStreamService;
|
@Autowired
|
private IComponentDepartmentService componentDepartmentService;
|
@Autowired
|
private IComponentPermissionService componentPermissionService;
|
@Autowired
|
private IPartsInfoService partsInfoService;
|
@Autowired
|
private IPartsDepartmentService partsDepartmentService;
|
@Autowired
|
private IPartsPermissionService partsPermissionService;
|
@Autowired
|
private IProcessStreamService processStreamService;
|
@Autowired
|
private IDocInfoService docInfoService;
|
@Autowired
|
private IDocRelativeService docRelativeService;
|
@Autowired
|
private IDocFileService docFileService;
|
@Autowired
|
private IDocClassificationService docClassificationService;
|
@Autowired
|
private IDeviceManagementService deviceManagementService;
|
|
/**
|
* 派工任务下发接口
|
* @param msg
|
* @return
|
*/
|
@Override
|
public String issuedDispatchTask(@WebParam(name = "msg") String msg){
|
log.info("MES派工任务下发原始数据 === {}", msg);
|
List<WsDispatch> wsDispatchList = JSONObject.parseArray(msg, WsDispatch.class);
|
for (WsDispatch wsDispatch : wsDispatchList) {
|
DispatchInfo dispatchInfo = new DispatchInfo();
|
BeanUtils.copyProperties(wsDispatch, dispatchInfo);
|
dispatchInfoService.save(dispatchInfo);
|
Map<Boolean,String> ncResult = disPatchTaskNc(dispatchInfo);
|
if (ncResult.containsKey(false)) {
|
// 返回具体的错误信息
|
return MesResultModel.error("NC任务下发失败: " + ncResult.get(false));
|
}
|
}
|
return MesResultModel.success("成功");
|
}
|
|
|
/**
|
* 下发程序
|
*/
|
public Map<Boolean,String> disPatchTaskNc(DispatchInfo dispatchInfo) {
|
String processCode = dispatchInfo.getOperationSeqNo() + "_" + dispatchInfo.getEquipmentId();
|
return assignFileStreamService.disPatchTaskNc(dispatchInfo.getEquipmentId(),
|
dispatchInfo.getWorkshop(),dispatchInfo.getMdsItemCode(),processCode,dispatchInfo.getRevisionNo(),dispatchInfo.getId());
|
}
|
|
/**
|
* 完工
|
*
|
* @param msg
|
* @return
|
*/
|
@Override
|
public String issuedProcedureFinish(String msg) {
|
log.info("MES完工原始数据 === {}", msg);
|
List<WsProcedureFinish> wsProcedureFinishList = JSONObject.parseArray(msg, WsProcedureFinish.class);
|
if (CollectionUtils.isEmpty(wsProcedureFinishList)) {
|
return MesResultModel.error("MesResultModel");
|
}
|
List<ProcedureFinish> procedureFinishList = wsProcedureFinishList.stream()
|
.map(source -> {
|
ProcedureFinish target = new ProcedureFinish();
|
BeanUtils.copyProperties(source, target);
|
return target;
|
})
|
.collect(Collectors.toList());
|
|
procedureFinishService.saveBatch(procedureFinishList);
|
//DNC系统依据工单号删除设备结构树上对应的NC程序及工控网NC程序
|
for (ProcedureFinish procedureFinish : procedureFinishList) {
|
//DNC系统依据工单号删除设备结构树上对应的NC程序及工控网NC程序
|
if (procedureFinish.getEndineStatus().equals("3")){
|
//删除设备结构树上对应的NC程序及工控网NC程序
|
ProductInfo productInfo = productInfoService.getByProductNo("PLM");
|
if (productInfo == null) {
|
log.error("======= 产品层级异常,请联系开发人员=======");
|
return MesResultModel.error("产品层级异常,请联系开发人员");
|
}
|
List<ComponentInfo> componentInfoList = componentInfoService.getByProductId(productInfo.getProductId());
|
List<String
|
|
> comlist = new ArrayList<>();
|
if (componentInfoList != null && !componentInfoList.isEmpty()) {
|
for (ComponentInfo cc : componentInfoList) {
|
comlist.add(cc.getComponentId());
|
}
|
} else {
|
return MesResultModel.error("无部件信息,请联系开发人员");
|
}
|
String processCode=procedureFinish.getMdsItemCode();
|
PartsInfo part = partsInfoService.getByCodeAndComIdList(procedureFinish.getMdsItemCode(),comlist);
|
if (part == null) {
|
return MesResultModel.error("无此零件信息"+"__" + processCode+",请联系开发人员");
|
}
|
processCode = processCode.split("_")[0];
|
//工序
|
ProcessStream stream = processStreamService.findByProcessEquipment(procedureFinish.getEquipmentId(),
|
processCode,procedureFinish.getRevisionNo(),productInfo.getProductId(),part.getPartsId());
|
if (stream == null) {
|
return MesResultModel.error("无此工序信息"+"__" + processCode+",请联系开发人员");
|
}
|
//查询对应NC程序
|
List<ProcessStream> processStreamList = new ArrayList<>();
|
processStreamList.add(stream);
|
List<DocInfo> docFileList = docInfoService.getByProcessIds(processStreamList);
|
if (docFileList == null || docFileList.isEmpty()) {
|
return MesResultModel.error("无此NC程序" + "__" + processCode + ",请联系开发人员");
|
}
|
docFileList.forEach(docFile -> {
|
docInfoService.deleteDocInfo(docFile.getDocId());
|
});
|
}
|
|
}
|
log.info("接收MES完工原始数据成功!");
|
return MesResultModel.success("成功");
|
}
|
|
|
/**
|
* 获取NC文件及属性信息(集成PLM)
|
*
|
* @param msg
|
* @return
|
*/
|
@WebMethod
|
@Override
|
@Transactional
|
public MesResultModel syncPlmNcLogProgram(@WebParam(name = "msg") String msg) {
|
log.info("NC文件及属性信息原始数据 === {}", msg);
|
List<PlmProgramSource> plmProgramSourceList = JSONObject.parseArray(msg, PlmProgramSource.class);
|
AtomicReference<MesResultModel> mesResultModel= new AtomicReference<>(new MesResultModel());
|
plmProgramSourceList.forEach(item -> {
|
mesResultModel.set(setTree(item));
|
});
|
return mesResultModel.get();
|
}
|
|
/**
|
* 封装树结构
|
1. 产品 集成数据
|
2、部件 按照部门(车间 ae8nc_plant )维度建立
|
3、零件 图号(零件号、零件名称) ae8part_no
|
4、工序 工艺ID(工艺规程编号/临规编号)
|
5. nc文件 (工序下)
|
6.机床型号与机床操作系统组成设备类与工序进行绑定
|
* @param plmProgramSource
|
* @return
|
*/
|
public MesResultModel setTree(PlmProgramSource plmProgramSource) {
|
MesResultModel model = new MesResultModel();
|
model.setSuccFlag("成功");
|
PlmProgramSourceInfo plmProgramSourceInfo = new PlmProgramSourceInfo();
|
BeanUtils.copyProperties(plmProgramSource, plmProgramSourceInfo);
|
plmProgramSourceInfo.setMesStatus("0");
|
iPlmProgramSourceInfoService.save(plmProgramSourceInfo);
|
// 产品
|
ProductInfo productInfo = productInfoService.getByProductNo("PLM");
|
if (productInfo == null) {
|
log.error("======= 产品层级异常,请联系开发人员=======");
|
return createErrorModel(model, "产品层级异常,请联系开发人员");
|
}
|
|
// 部件
|
ComponentInfo compTotal = componentInfoService.getByCode(plmProgramSourceInfo.getCzkAe8nDivision());
|
if (compTotal == null) {
|
compTotal = new ComponentInfo();
|
compTotal.setComponentCode(plmProgramSourceInfo.getCzkAe8nDivision());
|
compTotal.setProductId(productInfo.getProductId());
|
compTotal.setComponentName(plmProgramSourceInfo.getCzkAe8nDivision());
|
compTotal.setComponentStatus(1);
|
compTotal.setRankLevel(1);
|
compTotal.setParentId(productInfo.getProductId());
|
compTotal.setComponentId(IdWorker.getIdStr());
|
|
if (!componentInfoService.save(compTotal)) {
|
return createErrorModel(model, "添加部件层级,请联系开发人员");
|
}
|
|
if (!savePermissionList(compTotal, productInfo.getProductId())) {
|
return createErrorModel(model, "添加权限失败,请联系开发人员");
|
}
|
}
|
|
// 3、零件 图号(零件号) ae8part_no
|
PartsInfo part = partsInfoService.getByCode(plmProgramSourceInfo.getCzkAe8nPartNo());
|
if (part == null) {
|
List<PartsDepartment> partsDepartPermList = new ArrayList<>();
|
List<PermissionStream> partsPermList = new ArrayList<>();
|
List<PartsPermission> partsUserPermList = new ArrayList<>();
|
|
part = new PartsInfo();
|
part.setPartsId(IdWorker.getIdStr());
|
part.setComponentId(compTotal.getComponentId());
|
part.setProductId(compTotal.getProductId());
|
part.setPartsStatus(1);
|
part.setPartsCode(plmProgramSourceInfo.getCzkAe8nPartNo());
|
part.setPartsName(plmProgramSourceInfo.getCzkAe8nPartName());
|
part.setPartsModel(plmProgramSourceInfo.getCzkAe8nPartNo());
|
|
if (!partsInfoService.save(part)) {
|
return createErrorModel(model, "添加零件层级,请联系开发人员");
|
}
|
|
// 权限
|
List<PermissionStream> oldPermissionList = permissionStreamService.getAllByProductId(productInfo.getProductId());
|
if (oldPermissionList != null && !oldPermissionList.isEmpty()) {
|
for (PermissionStream p : oldPermissionList) {
|
if (ValidateUtil.validateString(p.getUserId())) {
|
// 用户权限
|
PartsPermission permission = new PartsPermission();
|
permission.setPartsId(part.getPartsId());
|
permission.setUserId(p.getUserId());
|
partsUserPermList.add(permission);
|
|
PermissionStream stream = new PermissionStream();
|
stream.setUserId(p.getUserId());
|
stream.setProductId(part.getProductId());
|
stream.setComponentId(part.getComponentId());
|
stream.setPartsId(part.getPartsId());
|
partsPermList.add(stream);
|
}
|
|
if (ValidateUtil.validateString(p.getDepartId())) {
|
// 部门权限
|
PartsDepartment department = new PartsDepartment();
|
department.setPartsId(part.getPartsId());
|
department.setDepartId(p.getDepartId());
|
partsDepartPermList.add(department);
|
|
PermissionStream stream = new PermissionStream();
|
stream.setProductId(part.getProductId());
|
stream.setComponentId(part.getComponentId());
|
stream.setPartsId(part.getPartsId());
|
stream.setDepartId(p.getDepartId());
|
partsPermList.add(stream);
|
}
|
}
|
|
if (!partsUserPermList.isEmpty() && !partsPermissionService.saveBatch(partsUserPermList)) {
|
return createErrorModel(model, "添加权限失败,请联系开发人员");
|
}
|
|
if (!partsDepartPermList.isEmpty() && !partsDepartmentService.saveBatch(partsDepartPermList)) {
|
return createErrorModel(model, "添加权限失败,请联系开发人员");
|
}
|
|
if (!partsPermList.isEmpty() && !permissionStreamService.saveBatch(partsPermList)) {
|
return createErrorModel(model, "添加权限失败,请联系开发人员");
|
}
|
}
|
}
|
|
// 4、工序 工艺ID(工艺规程编号/临规编号)
|
List<ProcessStream> addProcessList = new ArrayList<>();
|
List<ProcessStream> updateProcessList = new ArrayList<>();
|
ProcessStream stream; // skgx_id
|
String processCode = plmProgramSourceInfo.getCzkAe8nOpNo();
|
|
stream = processStreamService.findByProcessNoAndPartsId(processCode, part.getPartsId());
|
if (stream == null) {
|
stream = new ProcessStream();
|
stream.setProcessId(IdWorker.getIdStr());
|
stream.setProductId(part.getProductId());
|
stream.setComponentId(part.getComponentId());
|
stream.setCraftVersion(plmProgramSourceInfo.getCzkAe8nOpRev());
|
stream.setPartsId(part.getPartsId());
|
stream.setProcessCode(processCode);
|
stream.setProcessName(plmProgramSourceInfo.getCzkAe8nOpName());
|
/*数控工序号*/
|
stream.setCraftNo(plmProgramSourceInfo.getCzkAe8nOpNo());
|
/*机床操作系统 ae8nc_os*/
|
DeviceManagement deviceManagement = deviceManagementService.findEquipmentIdsFromEqId(
|
plmProgramSourceInfo.getCzkAe8nDivision(), plmProgramSourceInfo.getCzkAe8nEquipNo());
|
if (deviceManagement != null && StringUtils.isNotEmpty(deviceManagement.getEquipmentIds())) {
|
stream.setProcessingEquipmentCode(deviceManagement.getEquipmentIds());
|
} else {
|
stream.setProcessingEquipmentCode(plmProgramSourceInfo.getCzkAe8nEquipNo());
|
}
|
stream.setProcessingEquipmentOs(plmProgramSourceInfo.getCzkAe8nEquipsystem());
|
stream.setProcessingEquipmentModel(plmProgramSourceInfo.getCzkAe8nEquipmentModel());
|
addProcessList.add(stream);
|
} else {
|
stream.setProcessName(plmProgramSourceInfo.getCzkAe8nOpName());
|
updateProcessList.add(stream);
|
|
}
|
|
if (!addProcessList.isEmpty() && !processStreamService.saveBatch(addProcessList)) {
|
return createErrorModel(model, "添加工序,请联系开发人员");
|
}
|
|
if (!updateProcessList.isEmpty() && !processStreamService.updateBatchById(updateProcessList)) {
|
return createErrorModel(model, "更新工序,请联系开发人员");
|
}
|
|
// 下载数据并解压
|
DocClassification docClass = docClassificationService.getByCode("NC");
|
if (docClass == null) {
|
return createErrorModel(model, "下载数据并解压数据失败,请联系开发人员");
|
}
|
boolean b;
|
try {
|
File file = new File(plmProgramSource.getFileName());
|
if(file.exists() && file.isFile() && file.length() > 0) {
|
if(plmProgramSource.getFiles() == null || plmProgramSource.getFiles().isEmpty()) {
|
ExceptionCast.cast(CommonCode.FAIL);
|
}
|
for(Map.Entry<String,byte[]> entry : plmProgramSource.getNcfiles().entrySet()) {
|
String suffix = FileUtil.getFileSuffix(entry.getKey());
|
DocInfo docInfo = docInfoService.findByAttrAndDocName(FileUtil.getFilenameNonSuffix(entry.getKey()), 5,
|
stream.getProcessId(),suffix);
|
//新增
|
FileUploadResult fileUploadResult = FileUtil.uploadFileByFileNameAndFis(entry.getKey(),new ByteArrayInputStream(entry.getValue()));
|
if(fileUploadResult == null) {
|
ExceptionCast.cast(DocumentCode.DOC_UPLOAD_ERROR);
|
}
|
if(docInfo == null) {
|
docInfo = new DocInfo();
|
String docId = IdWorker.getIdStr();
|
docInfo.setDocId(docId);
|
docInfo.setDocName(fileUploadResult.getFileName());
|
docInfo.setDocSuffix(fileUploadResult.getFileSuffix());
|
if (StrUtil.isNotEmpty(plmProgramSourceInfo.getCzkReleaseStatus())){
|
docInfo.setDocStatus(5);
|
}else {
|
docInfo.setDocStatus(4);
|
}
|
DocRelative docRelative = new DocRelative();
|
docRelative.setAttributionId(stream.getProcessId());
|
docRelative.setDocId(docInfo.getDocId());
|
docRelative.setAttributionType(5);
|
docRelative.setClassificationId(docClass.getClassificationId());
|
b = docRelativeService.save(docRelative);
|
if(!b) {
|
return createErrorModel(model, "添加文件,请联系开发人员");
|
}
|
if (getFile(docInfo, fileUploadResult, docFileService)) {
|
return createErrorModel(model, "添加文件,请联系开发人员");
|
}
|
b = docInfoService.save(docInfo);
|
}else {
|
if (StringUtils.isNotEmpty(stream.getCraftVersion())&&StringUtils.isNotEmpty(plmProgramSource.getCzkAe8nOpRev())) {
|
//升版,保存原文件
|
if (!stream.getCraftVersion().equals(plmProgramSource.getCzkAe8nOpRev())) {
|
this.createFileOldNcPathAndCopy(docInfo,plmProgramSourceInfo);
|
}
|
}
|
DocFile docFile = getDocFile(docInfo, fileUploadResult);
|
b = docFileService.addDocFile(docFile);
|
if(!b){
|
return createErrorModel(model, "升版失败,请联系开发人员");
|
}
|
docInfo.setPublishVersion(docFile.getDocVersion());
|
docInfo.setPublishFileId(docFile.getFileId());
|
b = docInfoService.updateById(docInfo);
|
}
|
if(!b){
|
return createErrorModel(model, "添加文件,请联系开发人员");
|
}
|
}
|
return model;
|
}
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
model.setSuccFlag("失败");
|
model.setError("下载数据并解压数据失败,请联系开发人员");
|
return model;
|
}
|
return model;
|
}
|
|
private MesResultModel createErrorModel(MesResultModel model, String errorMsg) {
|
model.setSuccFlag("失败");
|
model.setError(errorMsg);
|
return model;
|
}
|
|
/**
|
* * NC程序齐套接口(MES)-DNC系统接收到PLM传的NC程序,将程序包对应的零件号、工序号、设备编号传给MES系统
|
* 定时扫描PlmProgramSourceInfo表中的数据,进行数据同步,每分钟同步一次
|
*/
|
@Scheduled(cron = "0 0/1 * * * ?")
|
@Transactional(rollbackFor = Exception.class)
|
public void syncWorkmanship() {
|
// 获取所有需要同步的数据
|
List<PlmProgramSourceInfo> plmProgramSourceInfos = iPlmProgramSourceInfoService.list(
|
new LambdaQueryWrapper<PlmProgramSourceInfo>()
|
.eq(PlmProgramSourceInfo::getMesStatus, "0")
|
.orderByAsc(PlmProgramSourceInfo::getId)
|
);
|
|
if (plmProgramSourceInfos.isEmpty()) {
|
log.info("无待同步数据,跳过处理");
|
return;
|
}
|
|
List<NcProgramInfo> ncProgramInfos = plmProgramSourceInfos.stream().map(item -> {
|
NcProgramInfo ncProgramInfo = new NcProgramInfo();
|
ncProgramInfo.setMdsItemCode(item.getCzkAe8nPartNo());
|
ncProgramInfo.setRevisionNo(item.getRevisionNo());
|
ncProgramInfo.setNcProgramName(item.getFileName());
|
ncProgramInfo.setOpreationSeqNo(item.getCzkAe8nOpNo());
|
ncProgramInfo.setEquipmentId(item.getCzkAe8nEquipNo());
|
ncProgramInfo.setWorkshop(item.getCzkAe8nDivision());
|
// ncProgramInfo.setMesId(item.getId()); // 确保生成符合要求的mesId
|
// ncProgramInfo.setOperFlag();
|
return ncProgramInfo;
|
}).collect(Collectors.toList());
|
|
log.info("WebService-NC程序齐套接口(MES)同步开始,时间: {}", new Date());
|
try {
|
String jsonPayload = JSONObject.toJSONString(ncProgramInfos);
|
log.debug("上报数据: {}", jsonPayload);
|
|
// 调用WebService并获取详细响应
|
String result = CxfClientUtil.invokeService(url, jsonPayload, namespace, method);
|
log.info("接口响应: {}", result);
|
|
// 解析响应结果,只会有成功和失败
|
if (result.contains("成功")) {
|
List<PlmProgramSourceInfo> toUpdate = new ArrayList<>();
|
|
plmProgramSourceInfos.forEach(item -> {
|
item.setMesStatus("1");
|
toUpdate.add(item);
|
});
|
// 批量更新状态
|
if (!toUpdate.isEmpty()) {
|
iPlmProgramSourceInfoService.updateBatchById(toUpdate);
|
log.info("成功更新{}条数据状态", toUpdate.size());
|
}
|
}else {
|
log.error("同步MES系统失败: {}", result);
|
}
|
} catch (Exception e) {
|
log.error("同步MES系统失败: {}", e.getMessage(), e);
|
// 仅标记处理失败的记录
|
plmProgramSourceInfos.forEach(item -> item.setMesStatus("0"));
|
iPlmProgramSourceInfoService.updateBatchById(plmProgramSourceInfos);
|
throw new RuntimeException("同步失败,已回滚状态", e); // 触发事务回滚
|
}
|
}
|
|
/**
|
* 创建升版文件保存地址并复制文件
|
* 层级结构: plm_bak->ae8ncPlant(车间)->ae8partNo(零件图号)->skgxId(工序)->具体文件
|
* @param docInfo 文档信息
|
* @param plmProgramSourceInfo 程序源信息
|
* @return 操作成功返回true,失败返回false
|
*/
|
public boolean createFileOldNcPathAndCopy(DocInfo docInfo, PlmProgramSourceInfo plmProgramSourceInfo) {
|
// 获取最近版本的文档文件
|
DocFile docFile = docFileService.getDocFileNearest(docInfo.getDocId());
|
if (docFile == null) {
|
log.error("未找到最近版本的文档文件,docId: {}", docInfo.getDocId());
|
return false;
|
}
|
|
// 构建保存路径
|
String directoryPath = String.join(File.separator,
|
"plm_bak",
|
plmProgramSourceInfo.getCzkAe8nDivision(),
|
plmProgramSourceInfo.getCzkAe8nPartNo(),
|
plmProgramSourceInfo.getCzkAe8nOpNo(),
|
""); // 末尾添加分隔符
|
|
File directory = new File(directoryPath);
|
|
// 创建目录(如果不存在)
|
if (!directory.exists() && !directory.mkdirs()) {
|
log.error("创建目录失败: {}", directoryPath);
|
return false;
|
}
|
|
// 构建目标文件路径
|
String targetFileName = buildTargetFileName(docFile);
|
String targetFilePath = directoryPath + targetFileName;
|
|
// 复制源文件到目标路径
|
try {
|
Path sourcePath = Paths.get(docFile.getFilePath());
|
Path targetPath = Paths.get(targetFilePath);
|
|
// 使用Files.copy复制文件,REPLACE_EXISTING选项处理文件已存在的情况
|
Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
|
|
log.info("文件复制成功: {} -> {}", docFile.getFilePath(), targetFilePath);
|
return true;
|
} catch (IOException e) {
|
log.error("文件复制失败: {}", e.getMessage(), e);
|
// 失败时删除已创建的空目录
|
if (directory.exists() && isDirectoryEmpty(directory)) {
|
directory.delete();
|
}
|
return false;
|
}
|
}
|
|
/**
|
* 构建目标文件名(包含版本信息)
|
* 格式: 原文件名_V版本号.后缀
|
* 例如: drawing_V1.0.dwg
|
*/
|
private String buildTargetFileName(DocFile docFile) {
|
String baseName = FilenameUtils.getBaseName(docFile.getFileName());
|
String version = docFile.getDocVersion() != null ? docFile.getDocVersion() : "1.0";
|
String suffix = docFile.getFileSuffix() != null ? docFile.getFileSuffix() : "";
|
|
// 确保版本号格式正确
|
if (!version.startsWith("V")) {
|
version = "V" + version;
|
}
|
|
// 确保后缀以点开头
|
if (!suffix.isEmpty() && !suffix.startsWith(".")) {
|
suffix = "." + suffix;
|
}
|
|
return baseName + "_" + version + suffix;
|
}
|
|
/**
|
* 检查目录是否为空
|
*/
|
private boolean isDirectoryEmpty(File directory) {
|
if (!directory.isDirectory()) {
|
return false;
|
}
|
String[] files = directory.list();
|
return files == null || files.length == 0;
|
}
|
|
private static String buildRequestBody(NcProgramInfo ncProgramInfo) {
|
StringBuilder body = new StringBuilder();
|
body.append("mesId=").append(ncProgramInfo.getMesId())
|
.append("&mdsItemCode=").append(ncProgramInfo.getMdsItemCode())
|
.append("&revisionNo=").append(ncProgramInfo.getRevisionNo())
|
.append("&opreationSeqNo=").append(ncProgramInfo.getOpreationSeqNo())
|
.append("&equipmentId=").append(ncProgramInfo.getEquipmentId())
|
.append("&workshop=").append(ncProgramInfo.getWorkshop());
|
return body.toString();
|
}
|
|
|
public static boolean getFile(DocInfo docInfo, FileUploadResult fileUploadResult, IDocFileService docFileService) {
|
boolean b;
|
DocFile docFile = new DocFile();
|
docFile.setDocId(docInfo.getDocId());
|
docFile.setFileName(fileUploadResult.getFileName());
|
docFile.setFileEncodeName(fileUploadResult.getEncodeFileName());
|
docFile.setFilePath(fileUploadResult.getFilePath());
|
docFile.setFileSize(fileUploadResult.getFileSize());
|
docFile.setFileSuffix(fileUploadResult.getFileSuffix());
|
b = docFileService.addDocFile(docFile);
|
if(!b) {
|
return true;
|
}
|
docInfo.setPublishVersion(docFile.getDocVersion());
|
docInfo.setPublishFileId(docFile.getFileId());
|
return false;
|
}
|
|
private static DocFile getDocFile(DocInfo docInfo, FileUploadResult fileUploadResult) {
|
DocFile docFile = new DocFile();
|
docFile.setDocId(docInfo.getDocId());
|
docFile.setFileName(fileUploadResult.getFileName());
|
docFile.setFileEncodeName(fileUploadResult.getEncodeFileName());
|
docFile.setFilePath(fileUploadResult.getFilePath());
|
docFile.setFileSize(fileUploadResult.getFileSize());
|
docFile.setFileSuffix(fileUploadResult.getFileSuffix());
|
return docFile;
|
}
|
|
private boolean savePermissionList(ComponentInfo comp, String totalOrSonId) {
|
boolean b;
|
List<PermissionStream> oldPermissionList = permissionStreamService.getAllByProductId(totalOrSonId);
|
//权限
|
List<ComponentDepartment> componentDepartmentList = new ArrayList<>();
|
List<PermissionStream> componentDepartPermList = new ArrayList<>();
|
List<ComponentPermission> componentPermissionList = new ArrayList<>();
|
ComponentDepartment department;
|
ComponentPermission permission;
|
PermissionStream stream;
|
if(oldPermissionList != null && !oldPermissionList.isEmpty()) {
|
for (PermissionStream p : oldPermissionList) {
|
if (ValidateUtil.validateString(p.getUserId())) {
|
//部门权限
|
permission = new ComponentPermission();
|
permission.setComponentId(comp.getComponentId());
|
permission.setUserId(p.getUserId());
|
componentPermissionList.add(permission);
|
|
stream = new PermissionStream();
|
stream.setUserId(p.getUserId());
|
stream.setProductId(comp.getProductId());
|
stream.setComponentId(comp.getComponentId());
|
componentDepartPermList.add(stream);
|
}
|
if (ValidateUtil.validateString(p.getDepartId())) {
|
//用户权限
|
department = new ComponentDepartment(comp.getComponentId(), p.getDepartId());
|
componentDepartmentList.add(department);
|
stream = new PermissionStream(comp.getProductId(), comp.getComponentId());
|
stream.setDepartId(p.getDepartId());
|
componentDepartPermList.add(stream);
|
}
|
|
}
|
}
|
if(!componentDepartmentList.isEmpty()) {
|
b = componentDepartmentService.saveBatch(componentDepartmentList);
|
if(!b) {
|
return false;
|
}
|
}
|
if(!componentPermissionList.isEmpty()) {
|
b = componentPermissionService.saveBatch(componentPermissionList);
|
if(!b) {
|
return false;
|
}
|
}
|
if(!componentDepartPermList.isEmpty()) {
|
b = permissionStreamService.saveBatch(componentDepartPermList);
|
if(!b) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
|
@Override
|
public String readFileName(String itemCode){
|
return "pdm_" + itemCode + ".zip";
|
}
|
|
/**
|
* 从指定的 ZIP 压缩包中提取所有 .nc 文件的内容及其文件名
|
*
|
* @param zipFilePath ZIP 压缩包的文件路径
|
* @return 包含文件名和对应文件内容的映射
|
* @throws IOException 如果在读取压缩包或文件内容时发生 I/O 错误
|
*/
|
public static Map<String, String> extractNcFilesWithNamesFromZip(String zipFilePath) throws IOException {
|
// 用于存储文件名和对应文件内容的映射
|
Map<String, String> ncFilesMap = new HashMap<>();
|
// 使用 try-with-resources 语句确保 ZipFile 资源被正确关闭
|
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
|
// 遍历压缩包中的每个条目
|
for (ZipEntry entry : zipFile.stream().collect(Collectors.toList())) {
|
// 检查条目是否为 .nc 文件
|
if (entry.getName().endsWith(".nc")) {
|
// 用于存储单个 .nc 文件的内容
|
StringBuilder content = new StringBuilder();
|
// 使用 try-with-resources 语句确保输入流和读取器资源被正确关闭
|
try (InputStream inputStream = zipFile.getInputStream(entry);
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
String line;
|
// 逐行读取文件内容
|
while ((line = reader.readLine()) != null) {
|
content.append(line).append("\n");
|
}
|
// 将文件名和文件内容添加到映射中
|
ncFilesMap.put(entry.getName(), content.toString());
|
}
|
}
|
}
|
}
|
return ncFilesMap;
|
}
|
}
|