lc/lc123/cloud/app/plugin/operate/im/OtherInYdthPlugin.java

124 lines
5.9 KiB
Java
Raw Normal View History

2025-11-18 09:52:34 +00:00
package tqq9.lc123.cloud.app.plugin.operate.im;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.botp.runtime.ConvertOperationResult;
import kd.bos.entity.botp.runtime.PushArgs;
import kd.bos.entity.botp.runtime.SourceBillReport;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.ConvertServiceHelper;
import kd.bos.util.StringUtils;
import kd.sdk.plugin.Plugin;
import tqq9.lc123.cloud.app.plugin.utils.BotpParamUtils;
import java.math.BigDecimal;
import java.util.*;
/**
2025-11-19 09:24:07 +00:00
* 其他入库单审核时采购退货申请下推红字采购入库单审核
2025-11-18 09:52:34 +00:00
*/
public class OtherInYdthPlugin extends AbstractOperationServicePlugIn implements Plugin {
private final static Log logger = LogFactory.getLog(OtherInYdthPlugin.class);
private static String PM_PURREFUNDAPPLYBILL = "pm_purrefundapplybill";//采购退货申请单
private static String IM_PURINBILL = "im_purinbill";//采购入库单
private static String BILLENTRY = "billentry";//收货通知单分录,采购退货申请单分录
@Override
public void onPreparePropertys(PreparePropertysEventArgs e) {
super.onPreparePropertys(e);
e.getFieldKeys().add("billentry.tqq9_cgthflid");
}
@Override
public void afterExecuteOperationTransaction(AfterOperationArgs e) {
super.afterExecuteOperationTransaction(e);
DynamicObject[] dataEntities1 = e.getDataEntities();
for (DynamicObject dynamicObject : dataEntities1) {
DynamicObjectCollection billentry = dynamicObject.getDynamicObjectCollection("billentry");
List<Long> cgthflidList = new ArrayList<>();
for (DynamicObject object : billentry) {
String tqq9_cgthflid = object.getString("tqq9_cgthflid");
if (StringUtils.isNotEmpty(tqq9_cgthflid) && !"0".equals(tqq9_cgthflid)) {
cgthflidList.add(Long.valueOf(tqq9_cgthflid));
}
}
2025-11-19 09:24:07 +00:00
2025-11-18 09:52:34 +00:00
//根据采购退货分录id找到采购退货申请单下推-红字采购入库单
2025-11-19 09:24:07 +00:00
//其他入库单有几行明细 生成的采购入库单就几行明细
2025-11-18 09:52:34 +00:00
DynamicObject pm_purrefundapplybill = BusinessDataServiceHelper.loadSingle("pm_purrefundapplybill",
new QFilter[]{new QFilter("billentry.id", QCP.in, cgthflidList)});
2025-11-19 09:24:07 +00:00
if (null != pm_purrefundapplybill) {
// 2. 创建列表收集红字入库单明细数据
List<Map<String, Object>> redEntryDataList = new ArrayList<>();
// 3. 遍历其他入库单明细收集数据不创建DynamicObject
for (DynamicObject inEntry : billentry) {
// 获取当前行的信息
String returnEntryId = inEntry.getString("tqq9_cgthflid");
BigDecimal qty = inEntry.getBigDecimal("qty") == null ? BigDecimal.ZERO : inEntry.getBigDecimal("qty"); // 入库数量
String materialId = inEntry.getDynamicObject("material").getDynamicObject("masterid").getString("id");
if (returnEntryId != null && qty != null && materialId != null) {
// 创建数据Map收集明细信息
Map<String, Object> redEntryData = new HashMap<>();
redEntryData.put("materialId", materialId);
redEntryData.put("qty", qty); // 红字用负数
redEntryData.put("returnEntryId", returnEntryId);
redEntryDataList.add(redEntryData);
2025-11-18 09:52:34 +00:00
2025-11-19 09:24:07 +00:00
}
2025-11-18 09:52:34 +00:00
}
Long id = pm_purrefundapplybill.getLong("id");
2025-11-19 09:24:07 +00:00
2025-11-18 09:52:34 +00:00
Map<String, Set<JSONObject>> param = new HashMap<>();//传入转换规则处理参数
2025-11-19 09:24:07 +00:00
for (Map<String, Object> map : redEntryDataList) {
2025-11-18 09:52:34 +00:00
JSONObject detailObject = new JSONObject();
2025-11-19 09:24:07 +00:00
Object materialId = map.get("materialId");
Object qty = map.get("qty");
Object returnEntryId = map.get("returnEntryId");
detailObject.put("materialId", materialId);
detailObject.put("qty", qty);
detailObject.put("returnEntryId", returnEntryId);
param.computeIfAbsent((String) returnEntryId, k -> new HashSet<>()).add(detailObject);
2025-11-18 09:52:34 +00:00
}
2025-11-19 09:24:07 +00:00
HashMap<Long, Set<Long>> entitypkMap = new HashMap<>();//上下游id映射Map
for (Map<String, Object> map : redEntryDataList) {
Object returnEntryId = map.get("returnEntryId");
entitypkMap.computeIfAbsent(id, k -> new HashSet<>()).add(Long.valueOf((String) returnEntryId));
}
PushArgs pushArgs1 = BotpParamUtils.getPushArgs(PM_PURREFUNDAPPLYBILL, IM_PURINBILL, BILLENTRY, param, entitypkMap, "2304405848245415936");
ConvertOperationResult pushResult = ConvertServiceHelper.pushAndSave(pushArgs1);
2025-11-18 09:52:34 +00:00
List<SourceBillReport> billReports = pushResult.getBillReports();
2025-11-19 09:24:07 +00:00
//下推失败直接返回
if (!pushResult.isSuccess()) {
for (SourceBillReport billReport : billReports) {
String billMessage = billReport.getFailMessage();
logger.info("采购退货申请:" + pm_purrefundapplybill.getString("billno") + "下推红字采购入库单失败:" + billMessage);
}
}
2025-11-18 09:52:34 +00:00
}
2025-11-19 09:24:07 +00:00
2025-11-18 09:52:34 +00:00
}
}
}