diff --git a/mmpac/src/private/nc/bs/mmpac/pmo/pac0002/bp/rule/AfterApproveSyncHighpressureMesRule.java b/mmpac/src/private/nc/bs/mmpac/pmo/pac0002/bp/rule/AfterApproveSyncHighpressureMesRule.java index 42a9b6e6..d3930d42 100644 --- a/mmpac/src/private/nc/bs/mmpac/pmo/pac0002/bp/rule/AfterApproveSyncHighpressureMesRule.java +++ b/mmpac/src/private/nc/bs/mmpac/pmo/pac0002/bp/rule/AfterApproveSyncHighpressureMesRule.java @@ -23,12 +23,16 @@ import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO; import nc.vo.org.OrgVO; import nc.vo.pub.BusinessException; import nc.vo.pubapp.pattern.exception.ExceptionUtils; +import nc.vo.pubapp.pattern.pub.SqlBuilder; +import nc.vo.scmpub.res.billtype.MMBillType; import nc.vo.scmpub.util.ArrayUtil; +import nc.vo.trade.billsource.LightBillVO; +import nc.vo.trade.billsource.LinkQueryType; +import nccloud.baseapp.core.log.NCCForUAPLogger; +import nccloud.vo.trade.billsource.BillFlowVO; +import nccloud.web.riart.billflow.model.BillFlowModel; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Stream; /** @@ -74,9 +78,11 @@ public class AfterApproveSyncHighpressureMesRule implements IRule { if (checkIfOrg(orgCode, configParams)) { continue; } + String cpmohid = pmoHeadVO.getCpmohid(); + String vbillcode = pmoHeadVO.getVbillcode(); JSONObject singleObj = new JSONObject(); - singleObj.put("bipProcessProductionOrderId", pmoHeadVO.getVbillcode()); - singleObj.put("bipId", pmoHeadVO.getCpmohid());// 主键ID + singleObj.put("bipProcessProductionOrderId", vbillcode); + singleObj.put("bipId", cpmohid);// 主键ID // singleObj.put("orderQuantity", pmoHeadVO.getPlanQuantity()); // 订单数量(计划数量) singleObj.put("billDate", pmoHeadVO.getDbilldate().toString()); // 单据日期 singleObj.put("orderVersion", pmoHeadVO.getVersion()); // 版本号 @@ -131,10 +137,9 @@ public class AfterApproveSyncHighpressureMesRule implements IRule { // 上级-上一个生产订单的信息 itemObj.put("bipParentProcessProductionOrderId", null); itemObj.put("bipParentProcessProductionOrderRowNum", null); - Map prevOrderMap = findPrevOrder(cmoid); - if (prevOrderMap != null && !prevOrderMap.isEmpty()) { - itemObj.put("bipParentProcessProductionOrderId", prevOrderMap.get("vsrccode")); - itemObj.put("bipParentProcessProductionOrderRowNum", prevOrderMap.get("vsrcrowno")); + String vsrccode = findPrevOrder(cpmohid, vbillcode); + if (vsrccode != null && !vsrccode.isEmpty()) { + itemObj.put("bipParentProcessProductionOrderId", vsrccode);// 上一个生产订单单号 } // 顶级-源头单据信息 itemObj.put("bipOriginProcessProductionOrderId", item.getVfirstcode()); @@ -215,52 +220,89 @@ public class AfterApproveSyncHighpressureMesRule implements IRule { } /** - * 按来源链路逐级向上查找“上一张生产订单(55A2)”。 - * 规则: - * 1) 若上级为空,返回null; - * 2) 若到达源头单据且其类型不是55A2,返回null; - * 3) 若遇到类型为55A2,则立即返回该生产订单 + * 查询上一个生产订单的信息 * - * @throws BusinessException 业务异常 + * @param hid 当前生产订单的ID + * @param vbillcode 当前生产订单的单号 */ - private Map findPrevOrder(String currentMoBid) throws BusinessException { - String nextBid = currentMoBid; - while (nextBid != null) { - String sql = "SELECT vsrctrantype, vsrcid, vsrcbid, vsrccode, vsrcrowno, vfirstid, vfirstbid FROM mm_mo WHERE dr = 0 AND cmoid = '" + nextBid + "'"; - Map row = (Map) baseDAO.executeQuery(sql, new MapProcessor()); - if (row == null) { - return null; - } - - String vsrctrantype = (String) row.get("vsrctrantype"); - String vsrcid = (String) row.get("vsrcid"); - String vsrcbid = (String) row.get("vsrcbid"); - String vsrccode = (String) row.get("vsrccode"); - String vsrcrowno = (String) row.get("vsrcrowno"); - String vfirstid = (String) row.get("vfirstid"); - - // 上级为空 - if (vsrctrantype == null || vsrcid == null || vsrcbid == null) { - return null; - } - - // 命中上一张生产订单 - if ("55A2".equals(vsrctrantype)) { - Map result = new HashMap<>(); - result.put("vsrccode", vsrccode); - result.put("vsrcrowno", vsrcrowno); - return result; - } - - // 已到达源头但源头不是生产订单,则结束返回null - if (vsrcid.equals(vfirstid) && !"55A2".equals(vsrctrantype)) { - return null; - } - - // 继续向上(下一轮以来源明细作为当前明细) - nextBid = vsrcbid; + private String findPrevOrder(String hid, String vbillcode) throws BusinessException { + String preCode = null; + BillFlowModel billFlow = new BillFlowModel(); + LightBillVO vo = billFlow.querySourceBillVO(hid, MMBillType.ProduceOrder.getCode(), LinkQueryType.OnlyQueryBill); + if (vo == null) { + return null; } - return null; + LightBillVO[] sourceBillVOs = vo.getSourceBillVOs(); + if (sourceBillVOs == null || sourceBillVOs.length == 0) { + return null; + } + List list = this.dealVO(vo); + if (list == null || list.isEmpty()) { + return null; + } + for (BillFlowVO billFlowVO : list) { + // 判断是否为来源单据 -1来源单据 0本单据 1下游单据 + if ("-1".equals(billFlowVO.getIsForward())) { + LightBillVO lightBillVO = billFlowVO.getLightBillVO(); + String billType = lightBillVO.getType(); + String billCode = lightBillVO.getCode(); + // 不断向下寻找流程生产订单,直到找到最后一个流程生产订单 + if ("55A2".equals(billType) && !billCode.equals(vbillcode)) { + preCode = billCode; + } + } + } + NCCForUAPLogger.debug("preCode = " + preCode); + return preCode; + } + + + public List dealVO(LightBillVO vo) { + if (vo == null) { + return null; + } else { + Map setAll = new LinkedHashMap(); + this.getSource(vo, setAll); + List sourceList = setAll.get(vo.getID()).getSourceList(); + setAll.get(vo.getID()).setIsForward("0"); + setAll.get(vo.getID()).setSourceList(sourceList); + List list = new ArrayList(setAll.values()); + return list; + } + } + + public void getSource(LightBillVO vo, Map setAll) { + if (vo != null) { + BillFlowVO flow = new BillFlowVO(); + flow.setLightBillVO(vo); + flow.setIsForward("-1"); + List sourceList = flow.getSourceList(); + if (vo.getSourceBillVOs() == null) { + if (setAll.get(vo.getID()) == null) { + setAll.put(vo.getID(), flow); + } + + } else { + for (LightBillVO sourVO : vo.getSourceBillVOs()) { + sourceList.add(sourVO.getID()); + this.getSource(sourVO, setAll); + } + + vo.setSourceMap(new HashMap()); + flow.setSourceList(sourceList); + setAll.put(vo.getID(), flow); + } + } + } + + public Map getSource1(String pk, String tableName) throws BusinessException { + SqlBuilder sqlBuilder = new SqlBuilder(); + sqlBuilder.append(" select DISTINCT vsrctype type, vsrcid id"); + sqlBuilder.append(" from " + tableName); + sqlBuilder.append(" where dr = 0 and "); + sqlBuilder.append("", pk); + Map result = (Map) baseDAO.executeQuery(sqlBuilder.toString(), new MapProcessor()); + return result; }