优化请购单转换采购订单批处理逻辑

- 调整代码结构,提高可读性和维护性- 优化分页查询,降低内存消耗
- 完善错误处理,确保数据一致性
-移除冗余日志,提升日志可读性
This commit is contained in:
张明 2025-05-30 10:30:38 +08:00
parent 34728b28ff
commit 9f109aeaaf
1 changed files with 41 additions and 54 deletions

View File

@ -1,8 +1,6 @@
package nc.bs.pu.m21.plugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log;
@ -20,10 +18,10 @@ import nc.util.mmf.framework.db.MMSqlBuilder;
import nc.vo.pu.m20.entity.PraybillHeaderVO;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pu.m21.entity.OrderHeaderVO;
import nc.vo.pu.m21.entity.OrderItemVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.scmpub.res.billtype.POBillType;
@ -44,13 +42,13 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
// 1. 查询满足条件的请购单聚合VO
PraybillVO[] purchaseRequestVOs = getQualifiedPurchaseRequests(bgWorkingContext);
logger.info("查询到满足条件的请购单数量: " + purchaseRequestVOs.length, this.getClass(), "executeTask");
if (purchaseRequestVOs.length > 0) {
// 2. 批量转换处理
processBatchTransfer(purchaseRequestVOs, bgWorkingContext);
logger.info("批量转换请购单到采购订单完成", this.getClass(), "executeTask");
} else {
if (purchaseRequestVOs.length <= 0) {
logger.info("未找到满足条件的请购单,任务结束", this.getClass(), "executeTask");
return null;
}
// 2. 批量转换处理
processBatchTransfer(purchaseRequestVOs, bgWorkingContext);
logger.info("批量转换请购单到采购订单完成", this.getClass(), "executeTask");
} catch (Exception e) {
logger.error("执行批量转换请购单到采购订单任务失败", e, this.getClass(), "executeTask");
ExceptionUtils.marsh(e);
@ -72,9 +70,8 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
return new PraybillVO[0];
}
List<PraybillVO> resultList = new ArrayList<>();
int pageSize = 2048;
int pageSize = 100;
IDDBPage page = new IDDBPage(sql, pageSize);
logger.info("开始分页查询,页大小: " + pageSize, this.getClass(), "getQualifiedPurchaseRequests");
int pageCount = 0;
while (page.hasNext()) {
pageCount++;
@ -121,9 +118,6 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
* 根据ID获取请购单聚合VO
*/
private PraybillVO[] getBGWorkInfo(String[] ids) {
if (logger.isDebugEnabled()) {
logger.debug("开始根据ID获取请购单聚合VOID数量: " + ids.length, this.getClass(), "getBGWorkInfo");
}
// 查询请购单表体
VOQuery<PraybillItemVO> itemQuery = new VOQuery<>(PraybillItemVO.class);
PraybillItemVO[] itemVOs = itemQuery.query(ids);
@ -144,35 +138,25 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
VOQuery<PraybillHeaderVO> headerQuery = new VOQuery<>(PraybillHeaderVO.class);
PraybillHeaderVO[] headerVOs = headerQuery.query(headerIds.toArray(new String[headerIds.size()]));
logger.debug("查询到请购单表头数量: " + (headerVOs != null ? headerVOs.length : 0), this.getClass(), "getBGWorkInfo");
return this.getAggvofromHeaderAndItem(headerVOs,
itemVOs);
return this.getAggvofromHeaderAndItem(headerVOs, itemVOs);
}
/**
* 组装采购订单聚合VO
*/
private PraybillVO[] getAggvofromHeaderAndItem(PraybillHeaderVO[] headerVOs, PraybillItemVO[] itemVOs) {
if (logger.isDebugEnabled()) {
logger.debug("开始组装聚合VO表头数量: " + (headerVOs != null ? headerVOs.length : 0) +
", 表体数量: " + (itemVOs != null ? itemVOs.length : 0), this.getClass(), "getAggvofromHeaderAndItem");
}
if (MMArrayUtil.isEmpty(headerVOs) || MMArrayUtil.isEmpty(itemVOs)) {
logger.warn("表头或表体数据为空无法组装聚合VO", this.getClass(), "getAggvofromHeaderAndItem");
return new PraybillVO[0];
}
List<PraybillVO> aggList = new ArrayList<>();
for (PraybillHeaderVO headerVO : headerVOs) {
List<PraybillItemVO> itemList = new ArrayList<>();
for (PraybillItemVO itemVO : itemVOs) {
if (headerVO.getPk_praybill().equals(itemVO.getPk_praybill())) {
itemList.add(itemVO);
}
}
if (!itemList.isEmpty()) {
PraybillVO aggVO = new PraybillVO();
aggVO.setParentVO(headerVO);
@ -180,7 +164,6 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
aggList.add(aggVO);
}
}
logger.debug("聚合VO组装完成数量: " + aggList.size(), this.getClass(), "getAggvofromHeaderAndItem");
return aggList.toArray(new PraybillVO[aggList.size()]);
}
@ -193,39 +176,43 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
try {
// 调用单据转换规则生成采购订单20->21
logger.info("调用单据转换规则,从请购单(20)转换为采购订单(21)", this.getClass(), "processBatchTransfer");
OrderVO[] orderVOs = PfServiceScmUtil.exeVOChangeByBillItfDef("20", "21", vos);
if (orderVOs != null && orderVOs.length > 0) {
logger.info("单据转换成功,生成采购订单数量: " + orderVOs.length, this.getClass(), "processBatchTransfer");
// 调用订单保存动作脚本
logger.info("开始保存采购订单", this.getClass(), "processBatchTransfer");
for (OrderVO orderVO : orderVOs) {
// 对采购订单赋值行号
OrderItemVO[] bvo = orderVO.getBVO();
for (int j = 0; j < bvo.length; j++) {
int rowNum = (j + 1) * 10;
bvo[j].setCrowno(String.valueOf(rowNum));
}
}
IPfExchangeService service = NCLocator.getInstance().lookup(IPfExchangeService.class);
OrderVO[] transVos = (OrderVO[]) service.runChangeDataAryNeedClassify("20", "21", orderVOs, null, 1);
CloudPFlowContext cloudContext = this.getCloudPFlowContext(transVos);
AppInfoContext.setBtnCode("Save");
SCMScriptResultDTO scriptResult = ((IBatchRunScriptService) NCLocator.getInstance().lookup(IBatchRunScriptService.class)).runBacth(cloudContext, OrderVO.class);
AbstractBill[] sucessVOs = scriptResult.getSucessVOs();
if (transVos == null || transVos.length <= 0) {
logger.info("转换时出现问题,传唤后VO对象数组为空", this.getClass(), "processBatchTransfer");
}
logger.info("采购订单保存完成", this.getClass(), "processBatchTransfer");
} else {
if (orderVOs == null || orderVOs.length == 0) {
logger.warn("单据转换未生成任何采购订单", this.getClass(), "processBatchTransfer");
return;
}
// 调用订单保存动作脚本
logger.info("开始保存采购订单", this.getClass(), "processBatchTransfer");
for (OrderVO orderVO : orderVOs) {
// 对采购订单赋值行号
OrderHeaderVO hvo = orderVO.getHVO();
OrderItemVO[] bvo = orderVO.getBVO();
for (int j = 0; j < bvo.length; j++) {
int rowNum = (j + 1) * 10;
// 设置pk_order
bvo[j].setPk_order(hvo.getPk_order());
bvo[j].setCrowno(String.valueOf(rowNum));
}
}
CloudPFlowContext cloudContext = this.getCloudPFlowContext(orderVOs);
AppInfoContext.setBtnCode("Save");
SCMScriptResultDTO scriptResult = ((IBatchRunScriptService) NCLocator.getInstance().lookup(IBatchRunScriptService.class)).runBacth(cloudContext, OrderVO.class);
AbstractBill[] sucessVOs = scriptResult.getSucessVOs();
Map<Integer, String> errorMessageMap = scriptResult.getErrorMessageMap();
if (!errorMessageMap.isEmpty()) {
errorMessageMap.keySet().forEach(key -> {
String errMsg = errorMessageMap.get(key);
if (errMsg.isEmpty()) return;
OrderVO orderVO = orderVOs[key];
// 给单据设置错误信息
orderVO.getHVO().setVdef30(errMsg);
orderVO.getHVO().setVdef31("N");
logger.warn("单据转换生成采购订单时发生错误: " + scriptResult.getErrorMessage(), this.getClass(), "processBatchTransfer");
});
}
} catch (Exception e) {
logger.error("批量转换请购单到采购订单失败", e, this.getClass(), "processBatchTransfer");
throw new BusinessException("批量转换请购单到采购订单失败: " + e.getMessage(), e);
ExceptionUtils.marsh(e);
}
}