|  |  | 
 |  |  | import com.dy.common.webFilter.UserTokenContext; | 
 |  |  | import com.dy.common.webUtil.QueryResultVo; | 
 |  |  | import com.dy.pmsGlobal.daoOth.OthFileMapper; | 
 |  |  | import com.dy.pmsGlobal.daoPr.PrAssemblyPlanMapper; | 
 |  |  | import com.dy.pmsGlobal.daoPr.PrProductionNodeMapper; | 
 |  |  | import com.dy.pmsGlobal.daoPr.PrProductionProcessMapper; | 
 |  |  | import com.dy.pmsGlobal.daoPr.PrWorkingInstructionMapper; | 
 |  |  | import com.dy.pmsGlobal.daoPr.*; | 
 |  |  | import com.dy.pmsGlobal.dyFile.FileOperate; | 
 |  |  | import com.dy.pmsGlobal.dyFile.FileRestVo; | 
 |  |  | import com.dy.pmsGlobal.pojoBa.BaUser; | 
 |  |  | import com.dy.pmsGlobal.pojoOth.OthFile; | 
 |  |  | import com.dy.pmsGlobal.pojoPr.PrAssemblyPlan; | 
 |  |  | import com.dy.pmsGlobal.pojoPr.PrProductionNode; | 
 |  |  | import com.dy.pmsGlobal.pojoPr.PrProductionProcess; | 
 |  |  | import com.dy.pmsGlobal.pojoPr.PrWorkingInstruction; | 
 |  |  | import com.dy.pmsGlobal.pojoPr.*; | 
 |  |  | import com.dy.pmsGlobal.util.UserUtil; | 
 |  |  | import com.dy.pmsProduct.taskPlan.PlanStatusEnum; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.concurrent.atomic.AtomicInteger; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | 
 |  |  |     private PrProductionProcessMapper processDao; | 
 |  |  |     private PrProductionNodeMapper nodeDao; | 
 |  |  |     private PrWorkingInstructionMapper workDao; | 
 |  |  |     private PrBillOfMaterialMapper billDao; | 
 |  |  |     private UserUtil userUtil; | 
 |  |  |     private FileOperate fileOperate; | 
 |  |  |     private OthFileMapper othFileMapper; | 
 |  |  | 
 |  |  |     public void setOthFileMapper(OthFileMapper othFileMapper) { | 
 |  |  |         this.othFileMapper = othFileMapper; | 
 |  |  |     } | 
 |  |  |     @Autowired | 
 |  |  |     public void setBillDao(PrBillOfMaterialMapper billDao) { | 
 |  |  |         this.billDao = billDao; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Transactional | 
 |  |  |     public int save(PrProductionProcess process) { | 
 |  |  | 
 |  |  |         if (CollectionUtils.isNotEmpty(planList)) { | 
 |  |  |             if (planList.stream().anyMatch(plan -> plan.getInputNumber() > 0)) { //存在任务已经投入生产 | 
 |  |  |                 if (!originProductionProcess.getProId().equals(process.getProId())) { | 
 |  |  |                     throw new RuntimeException("存在已经投入生产绑定的任务,产品不能修改"); | 
 |  |  |                     throw new RuntimeException("存在已经投入生产的绑定任务,产品不能修改"); | 
 |  |  |                 } | 
 |  |  |                 //节点id不能删除 | 
 |  |  |                 if (!originProductionProcess.getNodes().stream().allMatch(node -> process.getNodes().stream().anyMatch(newNode ->  node.getId().equals(newNode.getId())))) { | 
 |  |  |                     throw new RuntimeException("存在已经投入生产绑定的任务,节点不能删除"); | 
 |  |  |                     throw new RuntimeException("已有绑定的任务投入生产,节点不能删除"); | 
 |  |  |                 } | 
 |  |  |             } else if (CollectionUtils.isNotEmpty(onlinePlanList)) { | 
 |  |  |                 //先将组装任务置为暂停状态 | 
 |  |  | 
 |  |  |         if (loginUser != null) { | 
 |  |  |             process.creator = loginUser.id; | 
 |  |  |         } | 
 |  |  |         AtomicInteger startCount = new AtomicInteger(); | 
 |  |  |         AtomicInteger endCount = new AtomicInteger(); | 
 |  |  |         process.nodes.forEach(node -> { | 
 |  |  |             node.processId = process.id; | 
 |  |  |             node.deleted = false; | 
 |  |  |             if(node.isStart){ | 
 |  |  |                 startCount.getAndIncrement(); | 
 |  |  |             } | 
 |  |  |             if(node.isEnd){ | 
 |  |  |                 endCount.getAndIncrement(); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |         if (startCount.get() != 1 || endCount.get() != 1) { | 
 |  |  |             throw new RuntimeException("开始节点和结束节点有且只能有一个"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // 将节点和工作指示的保存逻辑封装到一个方法中 | 
 |  |  | 
 |  |  |         if (CollectionUtils.isNotEmpty(workList)) { | 
 |  |  |             workDao.insertMany(workList); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         List<PrBillOfMaterial> billList = process.nodes.stream().map(node -> { | 
 |  |  |             if (node.bill != null) { | 
 |  |  |                 node.bill.nodeId = node.id; | 
 |  |  |                 return node.bill; | 
 |  |  |             } | 
 |  |  |             return null; | 
 |  |  |         }).filter(bill -> bill != null).toList(); | 
 |  |  |         if (CollectionUtils.isNotEmpty(billList)) { | 
 |  |  |             billDao.deleteByNodeId(billList.stream().map(PrBillOfMaterial::getNodeId).toList()); | 
 |  |  |             billDao.insertMany(billList); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public int delete(Long id) { | 
 |  |  | 
 |  |  |             process.nodes.forEach(node -> { | 
 |  |  |                 if (node.instruction != null) { | 
 |  |  |                     addUrl(node.instruction); | 
 |  |  |                 } | 
 |  |  |                 if (node.bill != null) { | 
 |  |  |                     addBillUrl(node.bill); | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  | 
 |  |  |                 if (node.instruction != null) { | 
 |  |  |                     addUrl(node.instruction); | 
 |  |  |                 } | 
 |  |  |                 if (node.bill != null) { | 
 |  |  |                     addBillUrl(node.bill); | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |         }); | 
 |  |  |         return rsVo; | 
 |  |  | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         FileRestVo fileRestVo = fileOperate.parseHashcode(fmUrl, file.hash); | 
 |  |  |         ins.webUrl = fileRestVo.fileSysRestUrl + fileRestVo.fileWebDownloadPath + ins.fileId; | 
 |  |  |         ins.webUrl = fileRestVo.fileWebDownloadPath + ins.fileId; | 
 |  |  |         ins.orgName = file.orgName; | 
 |  |  |         ins.extName = file.extName; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void addBillUrl(PrBillOfMaterial bill) { | 
 |  |  |         if (bill == null || bill.fileId == null) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         OthFile file = othFileMapper.selectByPrimaryKey(bill.fileId); | 
 |  |  |         if (file == null) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         FileRestVo fileRestVo = fileOperate.parseHashcode(fmUrl, file.hash); | 
 |  |  |         bill.webUrl = fileRestVo.fileWebDownloadPath + bill.fileId; | 
 |  |  |         bill.orgName = file.orgName; | 
 |  |  |         bill.extName = file.extName; | 
 |  |  |     } | 
 |  |  | } |