| package com.dy.pmsProduct.process; | 
|   | 
| import com.dy.common.webFilter.UserTokenContext; | 
| import com.dy.common.webUtil.QueryResultVo; | 
| import com.dy.pmsGlobal.daoOth.OthFileMapper; | 
| import com.dy.pmsGlobal.daoPr.PrProductionNodeMapper; | 
| import com.dy.pmsGlobal.daoPr.PrProductionProcessMapper; | 
| import com.dy.pmsGlobal.daoPr.PrWorkingInstructionMapper; | 
| 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.PrProductionNode; | 
| import com.dy.pmsGlobal.pojoPr.PrProductionProcess; | 
| import com.dy.pmsGlobal.pojoPr.PrWorkingInstruction; | 
| import com.dy.pmsGlobal.util.UserUtil; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.dubbo.common.utils.PojoUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.dao.DuplicateKeyException; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.util.List; | 
| import java.util.Map; | 
| import java.util.stream.Collectors; | 
|   | 
| @Slf4j | 
| @Service | 
| public class ProcessSv { | 
|     private PrProductionProcessMapper processDao; | 
|     private PrProductionNodeMapper nodeDao; | 
|     private PrWorkingInstructionMapper workDao; | 
|     private UserUtil userUtil; | 
|     private FileOperate fileOperate; | 
|     private OthFileMapper othFileMapper; | 
|     @Value("${dy.webFile.fmUrl}") | 
|     private String fmUrl ; | 
|     @Autowired | 
|     public void setProcessDao(PrProductionProcessMapper dao){ | 
|         processDao = dao; | 
|     } | 
|     @Autowired | 
|     public void setNodeDao(PrProductionNodeMapper dao){ | 
|         nodeDao = dao; | 
|     } | 
|     @Autowired | 
|     public void setWorkDao(PrWorkingInstructionMapper dao){ | 
|         workDao = dao; | 
|     } | 
|     @Autowired | 
|     public void setUserUtil(UserUtil userUtil){ | 
|         this.userUtil = userUtil; | 
|     } | 
|     @Autowired | 
|     public void setFileOperate(FileOperate fileOperate){ | 
|         this.fileOperate = fileOperate; | 
|     } | 
|     @Autowired | 
|     public void setOthFileMapper(OthFileMapper othFileMapper){ | 
|         this.othFileMapper = othFileMapper; | 
|     } | 
|     @Transactional | 
|     public int save(PrProductionProcess process){ | 
|         prepareProcess(process); | 
|         int count = processDao.insertSelective(process); | 
|         saveNodesAndInstructions(process); | 
|         return count; | 
|     } | 
|   | 
|     @Transactional | 
|     public int update(PrProductionProcess process){ | 
|         prepareProcess(process); | 
|         int count = processDao.updateByPrimaryKeySelective(process); | 
|         // 优化:只有当节点有变更时才删除并重新插入 | 
|         if (!process.nodes.isEmpty()) { | 
|             List<Long> nodeIdsToDelete = process.getNodes().stream() | 
|                     .map(PrProductionNode::getId) // 映射节点到其ID | 
|                     .collect(Collectors.toList()); | 
|             workDao.deleteByNodeId(nodeIdsToDelete); | 
|             nodeDao.deleteByProcessId(process.id); | 
|         } | 
|         saveNodesAndInstructions(process); | 
|         return count; | 
|     } | 
|   | 
|     // 提取共通逻辑到单独方法以减少代码重复 | 
|     private void prepareProcess(PrProductionProcess process){ | 
|         process.disabled = false; | 
|         process.deleted = false; | 
|         BaUser loginUser = userUtil.getUser(UserTokenContext.get()); | 
|         if(loginUser!=null){ | 
|             process.creator = loginUser.id; | 
|         } | 
|         process.nodes.forEach(node -> { | 
|             node.processId = process.id; | 
|             node.deleted= false; | 
|         }); | 
|     } | 
|   | 
|     // 将节点和工作指示的保存逻辑封装到一个方法中 | 
|     private void saveNodesAndInstructions(PrProductionProcess process){ | 
|         try{ | 
|             nodeDao.insertMany(process.nodes); | 
|         }catch (DuplicateKeyException e){ | 
|             throw new RuntimeException("节点顺序重复"); | 
|         } | 
|         List<PrWorkingInstruction> workList = process.nodes.stream().map(node -> { | 
|             if(node.instruction !=null){ | 
|                 node.instruction.nodeId = node.id; | 
|                 return node.instruction; | 
|             } | 
|             return null; | 
|         }).filter(work -> work != null).toList(); | 
|         workDao.insertMany(workList); | 
|     } | 
|   | 
|   | 
|   | 
|     public int delete(Long id){ | 
|         return processDao.deleteLogicById(id); | 
|     } | 
|   | 
|     public PrProductionProcess selectById(Long id){ | 
|         PrProductionProcess process = processDao.selectByPrimaryKey(id); | 
|         if(process != null){ | 
|             process.nodes.forEach(node -> { | 
|                 if(node.instruction != null){ | 
|                     addUrl(node.instruction); | 
|                 } | 
|             }); | 
|         } | 
|         return process; | 
|     } | 
|   | 
|     public QueryResultVo<List<PrProductionProcess>> selectSome(QueryVo queryVo){ | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         //查询符合条件的记录总数 | 
|         Long itemTotal = processDao.selectSomeCount(params); | 
|   | 
|         QueryResultVo<List<PrProductionProcess>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ; | 
|         //计算分页等信息 | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|   | 
|         //查询符合条件的记录 | 
|         rsVo.obj = processDao.selectSome(params); | 
|         rsVo.obj.stream().forEach(process -> { | 
|             process.nodes.forEach(node -> { | 
|                 if(node.instruction != null){ | 
|                     addUrl(node.instruction); | 
|                 } | 
|             }); | 
|         }); | 
|         return rsVo ; | 
|     } | 
|   | 
|     public List<Map<String,String>> queryAll(){ | 
|         return processDao.queryAll(); | 
|     } | 
|   | 
|     private void addUrl(PrWorkingInstruction ins){ | 
|         if (ins == null || ins.fileId == null) { | 
|             return; | 
|         } | 
|         OthFile file = othFileMapper.selectByPrimaryKey(ins.fileId); | 
|         if (file == null) { | 
|             return; | 
|         } | 
|         FileRestVo fileRestVo = fileOperate.parseHashcode(fmUrl, file.hash); | 
|         ins.webUrl = fileRestVo.fileSysRestUrl + fileRestVo.fileWebDownloadPath + ins.fileId; | 
|         ins.orgName = file.orgName; | 
|         ins.extName = file.extName; | 
|     } | 
| } |