实现批量转换请购单到采购订单功能- 新增 BatchTransferToPurchaseOrder 类实现 IBackgroundWorkPlugin 接口

- 完成请购单查询、聚合 VO 组装、批量转换和保存采购订单等功能
- 添加日志记录和异常处理,确保任务执行的稳定性和可追踪性
This commit is contained in:
张明 2025-05-29 16:47:44 +08:00
parent 0a1bc358e6
commit 789c519cbe
1 changed files with 213 additions and 0 deletions

View File

@ -0,0 +1,213 @@
package nc.bs.pu.m21.plugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import nc.bs.logging.Log;
import nc.bs.pub.common.PfServiceScmUtil;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.pa.PreAlertReturnType;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.pubapp.AppBsContext;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.impl.pubapp.pattern.page.db.IDDBPage;
import nc.util.mmf.framework.base.MMArrayUtil;
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.OrderItemVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
public class BatchTransferToPurchaseOrder implements IBackgroundWorkPlugin {
private static final Log logger = Log.getInstance("devpoordertask");
@Override
public PreAlertObject executeTask(BgWorkingContext bgWorkingContext) throws BusinessException {
logger.info("开始执行批量转换请购单到采购订单任务", this.getClass(), "executeTask");
PreAlertObject retObj = new PreAlertObject();
retObj.setReturnType(PreAlertReturnType.RETURNNOTHING);
try {
// 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 {
logger.info("未找到满足条件的请购单,任务结束", this.getClass(), "executeTask");
}
} catch (Exception e) {
logger.error("执行批量转换请购单到采购订单任务失败", e, this.getClass(), "executeTask");
ExceptionUtils.marsh(e);
}
logger.info("批量转换请购单到采购订单任务执行完成", this.getClass(), "executeTask");
return retObj;
}
/**
* 查询满足条件的请购单
* 条件表体请购单主数量-累计订货主数量>0且没有行关闭
*/
private PraybillVO[] getQualifiedPurchaseRequests(BgWorkingContext bgWorkingContext) throws BusinessException {
logger.info("开始查询满足条件的请购单", this.getClass(), "getQualifiedPurchaseRequests");
try {
String sql = getSql(bgWorkingContext);
if (sql == null) {
logger.warn("生成的SQL为空返回空结果", this.getClass(), "getQualifiedPurchaseRequests");
return new PraybillVO[0];
}
List<PraybillVO> resultList = new ArrayList<>();
int pageSize = 2048;
IDDBPage page = new IDDBPage(sql, pageSize);
logger.info("开始分页查询,页大小: " + pageSize, this.getClass(), "getQualifiedPurchaseRequests");
int pageCount = 0;
while (page.hasNext()) {
pageCount++;
String[] ids = page.next();
PraybillVO[] aggvos = getBGWorkInfo(ids);
if (MMArrayUtil.isNotEmpty(aggvos)) {
resultList.addAll(Arrays.asList(aggvos));
logger.debug("" + pageCount + "页处理得到聚合VO数量: " + aggvos.length, this.getClass(), "getQualifiedPurchaseRequests");
}
}
logger.info("分页查询完成,总页数: " + pageCount + ", 最终结果数量: " + resultList.size(), this.getClass(), "getQualifiedPurchaseRequests");
return resultList.toArray(new PraybillVO[resultList.size()]);
} catch (Exception e) {
logger.error("查询满足条件的请购单失败", e, this.getClass(), "getQualifiedPurchaseRequests");
// marsh 抛出异常
ExceptionUtils.marsh(e);
return new PraybillVO[0];
}
}
/**
* 构建查询SQL 查询视图
*/
private String getSql(BgWorkingContext bgwc) {
logger.debug("开始构建查询SQL", this.getClass(), "getSql");
String[] pkorgs = bgwc.getPk_orgs();
String pkGroup = AppBsContext.getInstance().getPkGroup();
MMSqlBuilder sb = new MMSqlBuilder();
sb.append(" SELECT PK_PRAYBILL_B");
sb.append(" FROM TRANS_PRAYBILL_PURCHASE");
if (pkGroup != null) {
sb.append(" WHERE PK_GROUP ", pkGroup);
}
if (MMArrayUtil.isNotEmpty(pkorgs)) {
sb.append(" AND");
sb.append(" PK_ORG", pkorgs);
}
String finalSql = sb.toString();
logger.debug("SQL构建完成", this.getClass(), "getSql");
return finalSql;
}
/**
* 根据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);
if (MMArrayUtil.isEmpty(itemVOs)) {
logger.warn("根据ID未查询到请购单表体数据", this.getClass(), "getBGWorkInfo");
return new PraybillVO[0];
}
logger.debug("查询到请购单表体数量: " + itemVOs.length, this.getClass(), "getBGWorkInfo");
// 获取表头ID
List<String> headerIds = new ArrayList<>();
for (PraybillItemVO itemVO : itemVOs) {
if (!headerIds.contains(itemVO.getPk_praybill())) {
headerIds.add(itemVO.getPk_praybill());
}
}
logger.debug("需要查询的表头ID数量: " + headerIds.size(), this.getClass(), "getBGWorkInfo");
// 查询请购单表头
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);
}
/**
* 组装采购订单聚合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);
aggVO.setChildrenVO(itemList.toArray(new PraybillItemVO[itemList.size()]));
aggList.add(aggVO);
}
}
logger.debug("聚合VO组装完成数量: " + aggList.size(), this.getClass(), "getAggvofromHeaderAndItem");
return aggList.toArray(new PraybillVO[aggList.size()]);
}
/**
* 批量转换请购单到采购订单
*/
private void processBatchTransfer(PraybillVO[] vos, BgWorkingContext bgWorkingContext) throws BusinessException {
logger.info("开始批量转换请购单到采购订单,请购单数量: " + vos.length, this.getClass(), "processBatchTransfer");
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));
}
}
PfServiceScmUtil.processBatch("SAVEBASE", "21", orderVOs, null, null);
logger.info("采购订单保存完成", this.getClass(), "processBatchTransfer");
} else {
logger.warn("单据转换未生成任何采购订单", this.getClass(), "processBatchTransfer");
}
} catch (Exception e) {
logger.error("批量转换请购单到采购订单失败", e, this.getClass(), "processBatchTransfer");
throw new BusinessException("批量转换请购单到采购订单失败: " + e.getMessage(), e);
}
}
}