lyh
2025-06-10 ec1d61de19f0fe6c3efc781e5d7a6af2ed65ae5d
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/DocInfoServiceImpl.java
@@ -16,6 +16,8 @@
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.ext.NcTxtFilePathInfo;
import org.jeecg.modules.dnc.listener.FileMonitorConfig;
import org.jeecg.modules.dnc.mapper.DocInfoMapper;
import org.jeecg.modules.dnc.request.DocInfoQueryRequest;
import org.jeecg.modules.dnc.request.DocInfoUploadRequest;
@@ -27,7 +29,9 @@
import org.jeecg.modules.dncFlow.entity.DispatchFile;
import org.jeecg.modules.dncFlow.service.IDispatchFileService;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.system.entity.SysParams;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.ISysParamsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,13 +43,22 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
@Service
public class DocInfoServiceImpl extends ServiceImpl<DocInfoMapper, DocInfo> implements IDocInfoService {
    @Autowired
    private FileMonitorConfig config;
    @Autowired
    private IMdcEquipmentService iMdcEquipmentService;
    @Autowired
    private IMdcProductionService iMdcProductionService;
    @Autowired
    private IDocClassificationService docClassificationService;
    @Autowired
@@ -55,11 +68,6 @@
    @Autowired
    @Lazy
    private IDocRelativeService docRelativeService;
    @Autowired
    @Lazy
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private IDeviceGroupService deviceGroupService;
    @Autowired
    private IDispatchFileService dispatchFileService;
    @Autowired
@@ -134,28 +142,28 @@
        docInfo.setPublishVersion(docFile.getDocVersion());
        docInfo.setPublishFileId(docFile.getFileId());
        //COPY
        DeviceInfo deviceInfo= deviceInfoService.getById(uploadRequest.getAttributionId());
        if (deviceInfo != null) {
            List<String> strings =  deviceGroupService.findListParentTreeAll(deviceInfo.getGroupId());
        MdcEquipment mdcEquipment= iMdcEquipmentService.getById(uploadRequest.getAttributionId());
        if (mdcEquipment != null) {
            List<String> strings = iMdcProductionService.findListParentTreeAll(mdcEquipment.getId());
            boolean copyFileNc = false;
            if (strings != null && !strings.isEmpty()) {
                String path = org.apache.commons.lang3.StringUtils.join(strings.toArray(), "/");
                copyFileNc = FileUtilS.copyFile(fileUploadResult.getFilePath()+"/" + fileUploadResult.getFileEncodeName(),
                        path+ "/" + deviceInfo.getDeviceNo() ,fileNameAll);
                        path+ "/" + mdcEquipment.getEquipmentId() ,fileNameAll);
                if (!copyFileNc) {
                    ExceptionCast.cast(ActivitiCode.ACT_FILE_ERROR);
                } else {
                    FileUtilS.deleteZipFromToSend(path + "/"+ deviceInfo.getDeviceNo(),
                    FileUtilS.deleteZipFromToSend(path + "/"+ mdcEquipment.getEquipmentId(),
                            docFile.getFileName(),docFile.getFileSuffix());
                }
            } else {
                copyFileNc =FileUtilS.copyFile(fileUploadResult.getFilePath()+"/" + fileUploadResult.getFileEncodeName(),
                        deviceInfo.getDeviceNo() ,fileNameAll);
                        mdcEquipment.getEquipmentId() ,fileNameAll);
                if (!copyFileNc) {
                    ExceptionCast.cast(ActivitiCode.ACT_FILE_ERROR);
                } else {
                    FileUtilS.deleteZipFromToSend(fileUploadResult.getFilePath()+"/" + fileUploadResult.getFileEncodeName() + "/"+ deviceInfo.getDeviceNo(),
                    FileUtilS.deleteZipFromToSend(fileUploadResult.getFilePath()+"/" + fileUploadResult.getFileEncodeName() + "/"+ mdcEquipment.getEquipmentId(),
                            docFile.getFileName(),docFile.getFileSuffix());
                }
@@ -212,8 +220,8 @@
        if (StringUtils.isEmpty(deviceNo)) {
            return false;
        }
        DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(deviceNo);
        if (deviceInfo == null) {
        MdcEquipment mdcEquipment = iMdcEquipmentService.findEquipmentNameByEquipmentId(deviceNo);
        if (mdcEquipment == null) {
            return false;
        }
        String fileName = FileUtilS.getFilenameNonSuffix(file.getOriginalFilename());
@@ -222,7 +230,7 @@
        if(fileUploadResult == null) {
            return false;
        }
        DocInfo en = findByAttrAndDocName(fileName, 4, deviceInfo.getDeviceId());
        DocInfo en = findByAttrAndDocName(fileName, 7, mdcEquipment.getId());
        if(en != null) {
            return false;
        }
@@ -235,9 +243,9 @@
        docInfo.setDocStatus(1);
        docInfo.setSyncStatus(2);
        DocRelative docRelative = new DocRelative();
        docRelative.setAttributionId(deviceInfo.getDeviceId());
        docRelative.setAttributionId(mdcEquipment.getId());
        docRelative.setDocId(docInfo.getDocId());
        docRelative.setAttributionType(4);
        docRelative.setAttributionType(7);
        docRelative.setClassificationId(docClass.getClassificationId());
        boolean b = docRelativeService.save(docRelative);
        if(!b) {
@@ -260,6 +268,72 @@
        return saveBool;
    }
    /**
     * 解析NC文档通用方法
     * @param pathFile,fileRec
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addDocInfoAnalysisNcService(String pathFile,File file) throws FileNotFoundException {
        String line;
        int count = 0;
        String loFilePath = config.getPaths().get(0)+"/" + file.getName();
        FileInputStream fis = new FileInputStream(loFilePath);
        NcTxtFilePathInfo result = new NcTxtFilePathInfo();
        Scanner sc = new Scanner(fis, "UTF-8");
        while (count < 8 &&  sc.hasNext()) {
            line = sc.nextLine();
            if (count ==0) {
                result.setFileTxtName(line.trim());
            } else  if (count ==1) {
                result.setFileNcName(line.trim());
            }else  if (count ==2) {
                result.setOrigFileName(line.trim());
            }else  if (count ==3) {
                result.setOrigFileSuffix(line.trim());
            }else  if (count ==4) {
                result.setFilePath(line.trim());
            }else  if (count ==5) {
                result.setEquipmentId(line.trim());
            }else  if (count ==6) {
                result.setFileAddOrDelete(Integer.valueOf(line.trim()));
            }else  if (count == 7) {
                result.setFileSize(line.trim());
            }
            count++;
        }
        sc.close();
        if (result.getFileAddOrDelete() != null ) {
            if ( result.getFileAddOrDelete()==1) {
                String path = config.getPaths().get(0) +"/" + result.getFileNcName()+ ".NC";
                    if (org.apache.commons.lang3.StringUtils.isNotBlank(result.getFileNcName()) &&
                            result.getFileNcName().equals("null")) {
                        //文件待处理
                        //备份数据
                        FileUtilS.deleteFileNewRec(loFilePath);
                    }
                File ncFile = new File(path);
                //已经存在文件需要备份
                String size = String.valueOf(ncFile.length());
                if (!size.equals(result.getFileSize())) {
                    return false;
                }
                boolean bool = this.addDocInfoRecService(result.getEquipmentId(),
                        ncFile,result.getOrigFileSuffix(),
                        FileUtilS.getFilenameNonSuffix(result.getOrigFileName())
                        ,result.getFilePath());
                if (bool) {
                    FileUtilS.deleteFileNewRec(path);
                    FileUtilS.deleteFileNewRec(loFilePath);
                }else {
                    return false;
                }
            }
        }
        return true;
    }
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addDocInfoRecService(String equipmentId,File fileRec,String fileNameSuffix,String fileNameNew,String filePath ) {
@@ -272,12 +346,11 @@
            ExceptionCast.cast(DocumentCode.DOC_CLASS_ERROR);
        }
        //根据设备编号查询数据
        String deviceNo = equipmentId;
        if (StringUtils.isEmpty(deviceNo)) {
        if (StringUtils.isEmpty(equipmentId)) {
            return false;
        }
        DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(deviceNo);
        if (deviceInfo == null) {
        MdcEquipment mdcEquipment = iMdcEquipmentService.findEquipmentNameByEquipmentId(equipmentId);
        if (mdcEquipment == null) {
            return false;
        }
        String fileName = FileUtilS.getFilenameNonSuffix(file.getOriginalFilename());
@@ -286,7 +359,7 @@
        if(fileUploadResult == null) {
            return false;
        }
        DocInfo en = findByAttrAndDocName(fileName, 4, deviceInfo.getDeviceId());
        DocInfo en = findByAttrAndDocName(fileName, 7, mdcEquipment.getId());
        if(en != null) {
            return false;
        }
@@ -299,9 +372,9 @@
        docInfo.setDocStatus(1);
        docInfo.setSyncStatus(2);
        DocRelative docRelative = new DocRelative();
        docRelative.setAttributionId(deviceInfo.getDeviceId());
        docRelative.setAttributionId(mdcEquipment.getId());
        docRelative.setDocId(docInfo.getDocId());
        docRelative.setAttributionType(4);
        docRelative.setAttributionType(7);
        docRelative.setClassificationId(docClass.getClassificationId());
        boolean b = docRelativeService.save(docRelative);
        if(!b) {
@@ -678,7 +751,7 @@
    public boolean deleteByDeviceId(String deviceId) {
        if(!ValidateUtil.validateString(deviceId))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        Integer attrType = 4;
        Integer attrType = 7;
        /*boolean b = docFileService.deleteByDocAttr(attrType, deviceId);
        if(!b)
            ExceptionCast.cast(CommonCode.FAIL);