lyh
2025-06-25 f64ce002aac67c5b7068654ed6229ea02f87520d
lxzn-module-dnc/src/main/java/org/jeecg/modules/dncFlow/service/impl/DispatchFileServiceImpl.java
@@ -2,8 +2,10 @@
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.TaskService;
@@ -11,6 +13,9 @@
import org.flowable.task.api.Task;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.dnc.constant.DncPassLogPassType;
import org.jeecg.modules.dnc.constant.DocAttributionTypeEnum;
import org.jeecg.modules.dnc.dto.TransferPackage;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.DocInfoMapper;
@@ -19,7 +24,10 @@
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.response.UcenterCode;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.service.impl.FileFerryService;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.dnc.utils.date.DateUtil;
import org.jeecg.modules.dnc.utils.file.FileUtilS;
import org.jeecg.modules.dncFlow.entity.DispatchFile;
import org.jeecg.modules.dncFlow.mapper.DispatchFileMapper;
import org.jeecg.modules.dncFlow.service.IDispatchFileService;
@@ -28,15 +36,27 @@
import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService;
import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
import org.jeecg.modules.flowable.apithird.service.FlowCommonService;
import org.jeecg.modules.flowable.domain.vo.FlowHistoricalVo;
import org.jeecg.modules.flowable.service.IFlowDefinitionService;
import org.jeecg.modules.flowable.service.IFlowTaskService;
import org.jeecg.modules.flowable.service.IHisWorkTaskService;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
/**
@@ -51,12 +71,6 @@
    @Autowired
    @Lazy
    private IDocInfoService docInfoService;
    @Autowired
    private IProcessStreamService processStreamService;
    @Autowired
    private IWorkStepService workStepService;
    @Autowired
    private IPermissionStreamNewService permissionStreamNewService;
    @Resource
    private FlowCommonService flowCommonService;
    @Resource
@@ -64,15 +78,33 @@
    @Autowired
    private IFlowTaskService flowTaskService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IFlowMyBusinessService flowMyBusinessService;
    @Autowired
    private DocInfoMapper docInfoMapper;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private IHisWorkTaskService hisWorkTaskService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Autowired
    private IDeviceManagementService deviceManagementService;
    @Autowired
    private IDncPassLogService dncPassLogService;
    @Autowired
    private IDocFileService docFileService;
    @Autowired
    private IDocRelativeService docRelativeService;
    @Autowired
    @Lazy
    private FileFerryService ferryService;
    @Value("${deploy.secretFolder}")
    private String secretFolder;    //工控网传输nc文件夹
    @Value("${fileHomePath}")
    private String fileHomePath;
    /**
     * 流程启动,保存对应的数据
     * @param dispatchFile
@@ -109,6 +141,29 @@
            docInfoService.updateById(docInfo);
        }
        return result;
    }
    /**
     * 文件查询审签流程
     * @param attributionId,attributionType,docId
     * @return
     */
    @Override
    public Result<?> queryDispatchDocFile(String attributionId, String attributionType, String docId){
        DispatchFile dispatchFile=super.getOne(new LambdaQueryWrapper<DispatchFile>()
                .eq(DispatchFile::getAttributionId, attributionId).eq(DispatchFile::getAttributionType, attributionType)
                .eq(DispatchFile::getDocId, docId));
        if (dispatchFile==null){
            return Result.error("未找到对应审签文件");
        }else {
            FlowMyBusiness flowMyBusiness=flowMyBusinessService.selectByDataId(dispatchFile.getId());
            if (flowMyBusiness==null){
                return Result.error("未找到对应审签流程");
            }else {
                List<FlowHistoricalVo> queryHisTaskByProcInstId=hisWorkTaskService.queryHisTaskByProcInstId(flowMyBusiness.getProcessInstanceId());
                return Result.ok(queryHisTaskByProcInstId);
            }
        }
    }
    /**
@@ -230,6 +285,7 @@
     * @return
     */
    @Override
    @Transactional
    public Result<?> auditDispatchFile(DispatchFileFlowTaskVo dispatchFileFlowTaskVo) {
        try {
            // 参数校验
@@ -302,6 +358,36 @@
    }
    /**
     * 批量审批操作
     * @param dispatchFileFlowTaskVo
     * @return
     */
    @Override
    @Transactional
    public Result<?> auditBatchDispatchFile(DispatchFileFlowTaskVo dispatchFileFlowTaskVo) throws InvocationTargetException, IllegalAccessException {
        if (dispatchFileFlowTaskVo.getTaskIds()==null || dispatchFileFlowTaskVo.getTaskIds().isEmpty()) {
            return Result.error(CommonCode.INVALID_PARAM.toString());
        }
        if (dispatchFileFlowTaskVo.getTaskIds().contains(",")){
            String[] taskIds = dispatchFileFlowTaskVo.getTaskIds().split(",");
            for (String taskId : taskIds) {
                FlowMyBusiness flowMyBusiness=flowMyBusinessService.getOne(new QueryWrapper<FlowMyBusiness>().eq("task_id",taskId));
                DispatchFileFlowTaskVo dispatchFileFlowTaskVoNew=new DispatchFileFlowTaskVo();
                BeanUtils.copyProperties(dispatchFileFlowTaskVoNew, dispatchFileFlowTaskVo);
                dispatchFileFlowTaskVoNew.setTaskId(taskId);
                dispatchFileFlowTaskVoNew.setDataId(flowMyBusiness.getDataId());
                dispatchFileFlowTaskVoNew.setInstanceId(flowMyBusiness.getProcessInstanceId());
                if (dispatchFileFlowTaskVo.getTargetKey().equals("task_prepare")){
                    reStartDispatchFile(dispatchFileFlowTaskVoNew);
                }else {
                    auditDispatchFile(dispatchFileFlowTaskVoNew);
                }
            }
        }
        return Result.OK("操作成功");
    }
    /**
     * 查询审签基本信息
     * @param id
     */
@@ -317,6 +403,34 @@
        docInfoQueryRequest.setDocClassCode(dispatchFile.getDocClassCode());
        List<DocInfo> docInfoList = docInfoService.findList(docInfoQueryRequest);
        return Result.OK(docInfoList);
    }
    /**
     * 发起定型流程
     */
    @Override
    public Result<?> submitProccess(DispatchFile dispatchFile) {
        checkParam(dispatchFile);
        //获取文件信息
        DocInfo docInfo = docInfoService.getById(dispatchFile.getDocId());
        if (docInfo.getDocDispatchStatus()==5){
            return Result.error("定型已通过,不能重复操作");
        }
        PermissionStreamNew permissionStreams = getPermissionStreams(dispatchFile);
        if (permissionStreams==null){
            return Result.error("用户没有权限");
        }
        super.save(dispatchFile);
        flowCommonService.initActBusiness(docInfo.getDocName()+"."+docInfo.getDocSuffix()+"文件进行定型",
                dispatchFile.getId(), "IDispatchFileService", "ncFileSettingProcessApproval", null);
        Map<String, Object> variables = new HashMap<>();
        variables.put("dataId", dispatchFile.getId());
        variables.put("organization", "导入文档默认启动流程");
        variables.put("comment", "导入文档默认启动流程");
        variables.put("proofreading",true);
        Result result= flowDefinitionService.startProcessInstanceByKey("ncFileSettingProcessApproval", variables);
        if (!result.isSuccess())
            super.removeById(dispatchFile.getId());
        return result;
    }
    private Map<String, Object> setProcessVariables(DispatchFile dispatchFile, String userId, DispatchFileFlowTaskVo dispatchFileFlowTaskVo) {
@@ -340,7 +454,7 @@
        return values;
    }
    private void updateStatus(DispatchFile dispatchFile, DocInfo docInfo, DispatchFileFlowTaskVo dispatchFileFlowTaskVo) {
    private void updateStatus(DispatchFile dispatchFile, DocInfo docInfo, DispatchFileFlowTaskVo dispatchFileFlowTaskVo) throws IOException {
        if (StrUtil.isNotBlank(dispatchFileFlowTaskVo.getProofreadStatus())) {
            switch (dispatchFileFlowTaskVo.getProofreadStatus()) {
                case "1":
@@ -371,8 +485,94 @@
        if (dispatchFileFlowTaskVo.getStereotype() != null && dispatchFileFlowTaskVo.getStereotype()) {
            dispatchFile.setCurrentNode("5");
            docInfo.setDocDispatchStatus(5);
            //定型通过
            DeviceType deviceType=deviceTypeService.getById(dispatchFile.getDeviceTypeId());
            DeviceManagement deviceManagement=deviceManagementService.getById(deviceType.getDeviceManagementId());
            DocFile docFile=docFileService.getById(dispatchFile.getFileId());
            DocRelative docRelative=docRelativeService.getOne(new QueryWrapper<DocRelative>().eq("doc_id",docInfo.getDocId())
                    .eq("attribution_id",dispatchFile.getAttributionId()).eq("attribution_type",dispatchFile.getAttributionType()));
            handleFileProcessing(docFile,deviceManagement,secretFolder);
            handleProductTree(docInfo,docRelative,deviceManagement);
        }
    }
    //封装处理文件
    private void handleFileProcessing(DocFile docFile, DeviceManagement deviceManagement, String secretFolder) throws IOException {
        if (!docFile.getFileSuffix().equals("zip") && !docFile.getFileSuffix().equals("rar")) {
            DncPassLog passInfoTxt = new DncPassLog();
            Date dateFirst = DateUtil.getNow();
            passInfoTxt.setDayTime(DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY));
            /*查询最后一条记录*/
            //休眠 500毫秒
            DncPassLog dncPassLog  = dncPassLogService.findDayTime(DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY));
            int fileNc =0;
            if (dncPassLog !=null) {
                fileNc = dncPassLog.getSequenceNumber() + 1;
            } else {
                fileNc =  1;
            }
            //处理文件名称  文件路径
            String sequenceNc = String.format("%06d",fileNc);
            DncPassLog passInfoNc = new DncPassLog();
            passInfoNc.setSequenceNumber(fileNc);
            passInfoNc.setSequenceOrder(sequenceNc);
            passInfoNc.setDayTime(DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY));
            passInfoNc.setPassType(DncPassLogPassType.NCFILE.getCode());
            passInfoNc.setPassName(docFile.getFileName());
            try {
                Thread.sleep(1000);
                Date date = new Date();
                passInfoNc.setCreateTime(date);
                System.out.println(DateUtil.format(date,DateUtil.STR_DATE_TIME));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            dncPassLogService.save(passInfoNc);
            // 4. 执行文件操作,后缀为NC
            Path source = Paths.get(fileHomePath+docFile.getFilePath(), docFile.getFileEncodeName());
            String destFileName = "10A" + DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY)+sequenceNc + "_" + deviceManagement.getDeviceManagementCode()+".NC";
            Path destination = Paths.get(secretFolder, destFileName);
            Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
        }
    }
    /**
     * 处理对应产品结构树、nc文件、刀具列表、程序加工确认表封装
     * @param docInfo
     */
    private void handleProductTree(DocInfo docInfo, DocRelative docRelative, DeviceManagement deviceManagement) {
        /*查询最后一条记录*/
        //休眠 500毫秒
        DncPassLog passInfoTxt = new DncPassLog();
        Date dateFirst = DateUtil.getNow();
        passInfoTxt.setDayTime(DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY));
        DncPassLog dncPassLog  = dncPassLogService.findDayTime(DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY));
        int fileTxt = 0, fileNc =0;
        if (dncPassLog !=null) {
            fileTxt = dncPassLog.getSequenceNumber() + 1;
        } else {
            fileTxt =  1;
        }
        String sequence = String.format("%06d",fileTxt);
        passInfoTxt.setSequenceNumber(fileTxt);
        passInfoTxt.setCreateTime(dateFirst);
        passInfoTxt.setSequenceOrder(sequence);
        System.out.println(DateUtil.format(dateFirst,DateUtil.STR_DATE_TIME));
        passInfoTxt.setPassType(DncPassLogPassType.PRODUCTSTRUCTURE.getCode());
        dncPassLogService.save(passInfoTxt);
        String fileName="10A"+DateUtil.format(dateFirst,DateUtil.STR_YEARMONTHDAY);
        if (Objects.equals(docInfo.getAttributionType(), DocAttributionTypeEnum.PROCESS.getCode())){
            //工序对应设备类
            String filePath = ferryService.exportData(TransferPackage.DataType.PROCESS, docRelative.getId(),fileName+sequence+"_"+deviceManagement.getDeviceManagementCode()+".ferry");
            System.out.println("工序数据已导出: " + filePath);
        }else {
            //工步对应设备类
            String filePath = ferryService.exportData(TransferPackage.DataType.WORKSTEP, docRelative.getId(),fileName+sequence+"_"+deviceManagement.getDeviceManagementCode()+".ferry");
            System.out.println("工步数据已导出: " + filePath);
        }
    }
    //传参验证
    public boolean checkParam(DispatchFile dispatchFile) {
@@ -391,67 +591,12 @@
        return true;
    }
    //获取文件信息
    private DocInfo getDocInfo(DispatchFile dispatchFile) {
        DocInfo docInfo = docInfoService.getByDocAttrAndDocId(dispatchFile.getDocId(),
                Integer.parseInt(dispatchFile.getAttributionType()), dispatchFile.getAttributionId());
        if (docInfo == null || docInfo.getDocStatus() == 3) {
            ExceptionCast.cast(ActivitiCode.ACT_DOC_ERROR);
        }
        return docInfo;
    }
    private PermissionStreamNew getPermissionStreams(DispatchFile dispatchFile) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        DeviceType deviceType = deviceTypeService.getById(dispatchFile.getAttributionId());
        String attributionId = deviceType != null ? deviceType.getAttributionId() : dispatchFile.getAttributionId();
        PermissionStreamNew permissionStreams;
        if (dispatchFile.getAttributionType().equals("5")) {
            // 工序
            permissionStreams = handleProcess(dispatchFile, attributionId, user);
        } else {
            // 工步
            permissionStreams = handleWorkStep(dispatchFile, attributionId, user);
        }
        if (permissionStreams == null) {
        PermissionStreamNew permissionStreamNew = permissionService.getPermissionStreams(dispatchFile);
        if (permissionStreamNew == null) {
            ExceptionCast.cast(ActivitiCode.ACT_NODE_DEPART_NONE);
        }
        return permissionStreams;
    }
    private PermissionStreamNew handleProcess(DispatchFile dispatchFile, String attributionId, LoginUser user) {
        ProcessStream processStream = processStreamService.getById(attributionId);
        if (processStream == null) {
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        dispatchFile.setProductId(processStream.getProductId());
        dispatchFile.setComponentId(processStream.getComponentId());
        dispatchFile.setPartsId(processStream.getPartsId());
        dispatchFile.setPsvId(processStream.getPsvId());
        dispatchFile.setProcessId(processStream.getProcessId());
        if (deviceTypeService.getById(dispatchFile.getAttributionId()) != null) {
            dispatchFile.setDeviceTypeId(deviceTypeService.getById(dispatchFile.getAttributionId()).getId());
        }
        return permissionStreamNewService.loadPermissionStreamNewByBusinessIdAndUserId(processStream.getProcessId(), user.getId(), "5");
    }
    private PermissionStreamNew handleWorkStep(DispatchFile dispatchFile, String attributionId, LoginUser user) {
        WorkStep workStep = workStepService.getById(attributionId);
        if (workStep == null) {
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        dispatchFile.setProductId(workStep.getProductId());
        dispatchFile.setComponentId(workStep.getComponentId());
        dispatchFile.setPartsId(workStep.getPartsId());
        dispatchFile.setPsvId(workStep.getPsvId());
        dispatchFile.setProcessId(workStep.getProcessId());
        dispatchFile.setStepId(workStep.getId());
        if (deviceTypeService.getById(dispatchFile.getAttributionId()) != null) {
            dispatchFile.setDeviceTypeId(deviceTypeService.getById(dispatchFile.getAttributionId()).getId());
        }
        return permissionStreamNewService.loadPermissionStreamNewByBusinessIdAndUserId(workStep.getId(), user.getId(), "6");
        return permissionStreamNew;
    }
    @Override