重构请购单转换到采购订单的功能- 重新组织代码结构,提高可读性和可维护性

- 增加 processBatch 和 updatePrayBillVOs 方法,优化批量处理逻辑
- 改进错误处理,使用日志记录详细信息
- 移除未使用的导入和变量,简化代码
This commit is contained in:
张明 2025-06-03 14:05:49 +08:00
parent cd417c2c5f
commit 4f5d008934
1 changed files with 66 additions and 40 deletions

View File

@ -1,7 +1,6 @@
package nc.bs.pu.m21.plugin;
import java.util.*;
import java.util.stream.Collectors;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log;
@ -14,7 +13,6 @@ import nc.bs.pubapp.AppBsContext;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.impl.pubapp.pattern.data.vo.VOUpdate;
import nc.impl.pubapp.pattern.page.db.IDDBPage;
import nc.itf.uap.pf.IPfExchangeService;
import nc.util.mmf.framework.base.MMArrayUtil;
import nc.util.mmf.framework.db.MMSqlBuilder;
import nc.vo.pu.m20.entity.PraybillHeaderVO;
@ -24,6 +22,7 @@ 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.VOStatus;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.scmpub.res.billtype.POBillType;
@ -184,8 +183,6 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
logger.warn("单据转换未生成任何采购订单", this.getClass(), "processBatchTransfer");
return;
}
// 调用订单保存动作脚本
logger.info("开始保存采购订单", this.getClass(), "processBatchTransfer");
for (OrderVO orderVO : orderVOs) {
// 对采购订单赋值行号
OrderHeaderVO hvo = orderVO.getHVO();
@ -197,47 +194,76 @@ public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
bvo[j].setCrowno(String.valueOf(rowNum));
}
}
CloudPFlowContext cloudContext = this.getCloudPFlowContext(orderVOs);
SCMScriptResultDTO scriptResult = ((IBatchRunScriptService) NCLocator.getInstance().lookup(IBatchRunScriptService.class)).runBacth(cloudContext, OrderVO.class);
AbstractBill[] sucessVOs = scriptResult.getSucessVOs();
// 要进行批量更新的数组
List<PraybillVO> hvoList = new ArrayList<>(vos.length - sucessVOs.length);
Map<Integer, String> errorMessageMap = scriptResult.getErrorMessageMap();
if (!errorMessageMap.isEmpty()) {
errorMessageMap.keySet().forEach(key -> {
String errMsg = errorMessageMap.get(key);
if (errMsg.isEmpty()) {
return;
}
PraybillVO vo = vos[key];
// TODO 后面需要把确定的字段长度扩充一下 errMsg也需要截取一下
vo.getHVO().setVdef30(errMsg);
vo.getHVO().setVdef31("N");
hvoList.add(vo);
logger.warn("单据转换生成采购订单时发生错误: " + scriptResult.getErrorMessage(), this.getClass(), "processBatchTransfer");
});
}
// 进行批量更新
PraybillHeaderVO[] hvoBatchUpdatedList = {};
if (hvoList.isEmpty()) {
logger.warn("批量更新请购单表头失败,获取表头数组长度为0", this.getClass(), "processBatchTransfer");
return;
}
for (PraybillVO hvo : hvoList) {
hvoBatchUpdatedList = ArrayUtils.add(hvoBatchUpdatedList, hvo.getHVO());
logger.info("循环更新请购单表头: " + hvo.getHVO().getPk_praybill(), this.getClass(), "processBatchTransfer");
}
if (hvoBatchUpdatedList.length == 0) {
logger.warn("批量更新请购单表头失败,需要更新的数组为0", this.getClass(), "processBatchTransfer");
}
String[] headupname = new String[]{"fbillstatus"};
VOUpdate<PraybillHeaderVO> headupsrv = new VOUpdate<>();
headupsrv.update(hvoBatchUpdatedList, headupname);
// 批量处理
processBatch(vos, orderVOs);
} catch (Exception e) {
logger.error("批量转换请购单到采购订单失败", e, this.getClass(), "processBatchTransfer");
}
}
private void processBatch(PraybillVO[] vos, OrderVO[] orderVOs) {
CloudPFlowContext cloudContext = this.getCloudPFlowContext(orderVOs);
if (orderVOs.length == 1) {
PraybillVO singleVO = vos[0];
PraybillHeaderVO singleHVO = singleVO.getHVO();
SCMScriptResultDTO scriptResult = null;
try {
scriptResult = ((IBatchRunScriptService) NCLocator.getInstance().lookup(IBatchRunScriptService.class)).runBacth(cloudContext, OrderVO.class);
if (scriptResult.getErrorMessageMap().isEmpty() && scriptResult.getSucessNum() > 0) {
singleHVO.setStatus(VOStatus.UPDATED);
}
} catch (Exception e) {
// TODO 后面需要确定的字段
singleHVO.setVdef30(e.getMessage().substring(0, 300));
singleHVO.setVdef31("N");
logger.error("单个处理时失败", e, this.getClass(), "processBatch");
}
updatePrayBillVOs(List.of(singleVO));
return;
}
SCMScriptResultDTO scriptResult = ((IBatchRunScriptService) NCLocator.getInstance().lookup(IBatchRunScriptService.class)).runBacth(cloudContext, OrderVO.class);
AbstractBill[] sucessVOs = scriptResult.getSucessVOs();
// 要进行批量更新的数组
List<PraybillVO> hvoList = new ArrayList<>(vos.length - sucessVOs.length);
Map<Integer, String> errorMessageMap = scriptResult.getErrorMessageMap();
if (!errorMessageMap.isEmpty()) {
errorMessageMap.keySet().forEach(key -> {
String errMsg = errorMessageMap.get(key);
if (errMsg.isEmpty()) {
return;
}
PraybillVO vo = vos[key];
PraybillHeaderVO hvo = vo.getHVO();
// TODO 后面需要确定的字段
hvo.setVdef30(errMsg.substring(0, 300));
hvo.setVdef31("N");
hvoList.add(vo);
logger.warn("单据转换生成采购订单时发生错误: " + scriptResult.getErrorMessage(), this.getClass(), "processBatchTransfer");
});
}
// 进行批量更新 目的是为了记录信息
updatePrayBillVOs(hvoList);
}
private void updatePrayBillVOs(List<PraybillVO> hvoList) {
PraybillHeaderVO[] hvoBatchUpdatedList = {};
if (hvoList.isEmpty()) {
logger.warn("批量更新请购单表头失败,获取表头数组长度为0", this.getClass(), "processBatchTransfer");
return;
}
for (PraybillVO hvo : hvoList) {
hvoBatchUpdatedList = ArrayUtils.add(hvoBatchUpdatedList, hvo.getHVO());
logger.info("循环更新请购单表头: " + hvo.getHVO().getPk_praybill(), this.getClass(), "processBatchTransfer");
}
if (hvoBatchUpdatedList.length == 0) {
logger.warn("批量更新请购单表头失败,需要更新的数组为0", this.getClass(), "processBatchTransfer");
return;
}
String[] headupname = new String[]{"fbillstatus"};
VOUpdate<PraybillHeaderVO> headupsrv = new VOUpdate<>();
headupsrv.update(hvoBatchUpdatedList, headupname);
}
private CloudPFlowContext getCloudPFlowContext(OrderVO[] vos) {
CloudPFlowContext context = new CloudPFlowContext();
context.setActionName("SAVEBASE");