提交人:陈绍鑫

日期:2025/7/19 17:00
内容:自动兑付优化
This commit is contained in:
陈绍鑫 2025-07-19 18:00:53 +08:00
parent e45c1e6707
commit 493eedc3ea
1 changed files with 117 additions and 74 deletions

View File

@ -5,6 +5,7 @@ import kd.bos.algo.DataSet;
import kd.bos.algo.Row; import kd.bos.algo.Row;
import kd.bos.dataentity.OperateOption; import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.entity.operate.result.OperationResult; import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn; import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.ReturnOperationArgs; import kd.bos.entity.plugin.args.ReturnOperationArgs;
@ -20,7 +21,6 @@ import kd.tmc.cdm.common.constant.CdmEntityConst;
import kd.tmc.cdm.common.enums.BillStatusEnum; import kd.tmc.cdm.common.enums.BillStatusEnum;
import kd.tmc.cdm.common.enums.DraftTradeTypeEnum; import kd.tmc.cdm.common.enums.DraftTradeTypeEnum;
import kd.tmc.cdm.common.enums.DraftTranStatusEnum; import kd.tmc.cdm.common.enums.DraftTranStatusEnum;
import shkd.sys.sys.plugin.task.redeemTaskPlugin;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
@ -29,14 +29,91 @@ import java.util.stream.Collectors;
public class DrafttradebillOP extends AbstractOperationServicePlugIn { public class DrafttradebillOP extends AbstractOperationServicePlugIn {
private static final Log logger = LogFactory.getLog(DrafttradebillOP.class); private static final Log logger = LogFactory.getLog(DrafttradebillOP.class);
@Override @Override
public void onReturnOperation(ReturnOperationArgs e) { public void onReturnOperation(ReturnOperationArgs e) {
super.onReturnOperation(e); super.onReturnOperation(e);
List<Object> successPkIds = e.getOperationResult().getSuccessPkIds();//执行成功的数据 List<Object> successPkIds = e.getOperationResult().getSuccessPkIds();//执行成功的数据
for (Object successPkId : successPkIds) {
Long id=(Long)successPkId;
Map<String, HashSet<Long>> srcBills = BFTrackerServiceHelper.findSourceBills("cdm_drafttradebill", new Long[]{id}); List<Long> list = new ArrayList<>();//用于存放要处理的开票登记
// 过滤掉不符合类型的数据
check(successPkIds,list);
// 使用 Stream 获取 DynamicObject 集合
List<DynamicObject> dynamicObjects = list.stream()
.map(id -> BusinessDataServiceHelper.loadSingle(id, CdmEntityConst.CDM_PAYABLEBILL))
.collect(Collectors.toList());
List<Object> numbers = dynamicObjects.stream()
.map(dynamicObject -> dynamicObject.getString("billno"))
.collect(Collectors.toList());
logger.info("单据编码:"+numbers+"进入检验");
// 调用方法检查所有 drawercompany资金组织 是否相同
boolean drawercompany = checkIfAllNumbersAreSame(dynamicObjects,"company");
boolean draftbilltype = checkIfAllNumbersAreSame(dynamicObjects,"draftbilltype");
// if(draftbilltype&&drawercompany){
logger.info(numbers+"验证通过可以合并下推兑付");
OperateOption option = OperateOption.create();
option.setVariableValue("tradeType", "redeem");
option.setVariableValue("remarks", "后台下推");
option.setVariableValue(OperateOptionConst.BATCHOP_BATCHSIZE, String.valueOf(100)); // 设置每批处理量
//调用方法执行数据 - 自动兑付下推
OperationResult operationResult = OperationServiceHelper.executeOperate("pushandsaveredeem",
CdmEntityConst.CDM_PAYABLEBILL,
list.toArray(new Object[]{}), option);
if (operationResult.getAllErrorInfo().size() != 0) {
logger.info("单据编号:" + numbers + "后台生单失败,原因:" + operationResult.getAllErrorInfo().get(0).getMessage());
String targetpkvalue = operationResult.getAllErrorInfo().get(0).getErrorCustInfos().get("targetpkvalue");
if (!StringUtil.isEmpty(targetpkvalue)) {
DynamicObject cdm_drafttradebill = BusinessDataServiceHelper.loadSingle(targetpkvalue, "cdm_drafttradebill");
OperationResult delete = OperationServiceHelper.executeOperate("delete", "cdm_drafttradebill", new DynamicObject[]{cdm_drafttradebill}, OperateOption.create());
if (delete.isSuccess()) {
logger.info("删除单据编号:" + numbers + "成功");
} else {
logger.info("删除单据编号:" + numbers + "失败,原因:" + delete.getMessage());
}
}
}
// }else {
// logger.info(numbers+"验证不通过,分开生单兑付");
// //生单
// for (int i = 0; i < list.size(); i++) {
// DynamicObject cdm_payablebill = BusinessDataServiceHelper.loadSingle(list.get(i), CdmEntityConst.CDM_PAYABLEBILL);
// List<Long> dataEntitys = new ArrayList<>();
// dataEntitys.add(Long.parseLong(cdm_payablebill.getString("id")));
// OperateOption option = OperateOption.create();
// option.setVariableValue("tradeType", "redeem");
// option.setVariableValue("remarks", "后台下推");
// //调用方法执行数据 - 自动兑付下推
// OperationResult operationResult = OperationServiceHelper.executeOperate("pushandsaveredeem",
// CdmEntityConst.CDM_PAYABLEBILL,
// dataEntitys.toArray(new Object[]{}), option);
// if (operationResult.getAllErrorInfo().size() != 0) {
// logger.info("单据编号:" + cdm_payablebill.getString("billno") + "后台生单失败,原因:" + operationResult.getAllErrorInfo().get(0).getMessage());
// String targetpkvalue = operationResult.getAllErrorInfo().get(0).getErrorCustInfos().get("targetpkvalue");
// if (!StringUtil.isEmpty(targetpkvalue)) {
// DynamicObject cdm_drafttradebill = BusinessDataServiceHelper.loadSingle(targetpkvalue, "cdm_drafttradebill");
// OperationResult delete = OperationServiceHelper.executeOperate("delete", "cdm_drafttradebill", new DynamicObject[]{cdm_drafttradebill}, OperateOption.create());
// if (delete.isSuccess()) {
// logger.info("删除单据编号:" + cdm_payablebill.getString("billno") + "成功");
// } else {
// logger.info("删除单据编号:" + cdm_payablebill.getString("billno") + "失败,原因:" + delete.getMessage());
// }
// }
// }
// }
// }
}
private void check(List<Object> successPkIds,List<Long> list) {
for (Object successPkId : successPkIds) {
Long id = (Long) successPkId;
Map<String, HashSet<Long>> srcBills = BFTrackerServiceHelper.findSourceBills("cdm_drafttradebill", new Long[]{id});//上查开票登记
if (srcBills.containsKey(CdmEntityConst.CDM_PAYABLEBILL)) { if (srcBills.containsKey(CdmEntityConst.CDM_PAYABLEBILL)) {
HashSet<Long> ar_invoice = srcBills.get(CdmEntityConst.CDM_PAYABLEBILL); HashSet<Long> ar_invoice = srcBills.get(CdmEntityConst.CDM_PAYABLEBILL);
// 获取第一个元素如果集合不为空 // 获取第一个元素如果集合不为空
@ -44,31 +121,31 @@ public class DrafttradebillOP extends AbstractOperationServicePlugIn {
DynamicObject cdm_payablebill = BusinessDataServiceHelper.loadSingle(firstElement, CdmEntityConst.CDM_PAYABLEBILL); DynamicObject cdm_payablebill = BusinessDataServiceHelper.loadSingle(firstElement, CdmEntityConst.CDM_PAYABLEBILL);
String draftbillstatus = cdm_payablebill.getString("draftbillstatus"); String draftbillstatus = cdm_payablebill.getString("draftbillstatus");
String settlementtype = cdm_payablebill.getString("draftbilltype.settlementtype"); String settlementtype = cdm_payablebill.getString("draftbilltype.settlementtype");
if (!draftbillstatus.equals("payoffed")){ if (!draftbillstatus.equals("payoffed")) {
continue; continue;
} }
if (!settlementtype.equals("5")){ if (!settlementtype.equals("5")) {
continue; continue;
} }
QFilter filter = new QFilter("id", QCP.equals, firstElement); QFilter filter = new QFilter("id", QCP.equals, firstElement);
DynamicObject[] CDM_PAYABLEBILLs = BusinessDataServiceHelper.load(CdmEntityConst.CDM_PAYABLEBILL, "id,billno,amount,billstatus,draftbilltranstatus", filter.toArray()); DynamicObject[] CDM_PAYABLEBILLs = BusinessDataServiceHelper.load(CdmEntityConst.CDM_PAYABLEBILL, "id,billno,amount,billstatus,draftbilltranstatus", filter.toArray());
Set<Object> draftIds = (Set)Arrays.stream(CDM_PAYABLEBILLs).map((s) -> { Set<Object> draftIds = Arrays.stream(CDM_PAYABLEBILLs).map((s) -> {
return s.getPkValue(); return s.getPkValue();
}).collect(Collectors.toSet()); }).collect(Collectors.toSet());
QFilter filter1 = new QFilter("entrys.draftbill.id", "in", draftIds); QFilter filter1 = new QFilter("entrys.draftbill.id", "in", draftIds);
filter1.and("tradetype", "=", DraftTradeTypeEnum.REDEEM.getValue()); filter1.and("tradetype", "=", DraftTradeTypeEnum.REDEEM.getValue());
filter1.and("draftbilltranstatus", "=", "success"); filter1.and("draftbilltranstatus", "=", "success");
Map<Long, BigDecimal> amountMap = new HashMap(16); Map<Long, BigDecimal> amountMap = new HashMap(16);
DataSet rows = QueryServiceHelper.queryDataSet("PayableBillRedeemValidator", "cdm_drafttradebill", "entrys.draftbill.id,entrys.billamt", filter1.toArray(), (String)null); DataSet rows = QueryServiceHelper.queryDataSet("PayableBillRedeemValidator", "cdm_drafttradebill", "entrys.draftbill.id,entrys.billamt", filter1.toArray(), null);
Throwable var8 = null; Throwable var8 = null;
try { try {
Iterator var9 = rows.iterator(); Iterator var9 = rows.iterator();
while(var9.hasNext()) { while (var9.hasNext()) {
Row row = (Row)var9.next(); Row row = (Row) var9.next();
Long draftBillId = row.getLong("entrys.draftbill.id"); Long draftBillId = row.getLong("entrys.draftbill.id");
BigDecimal orDefault = (BigDecimal)amountMap.getOrDefault(draftBillId, BigDecimal.ZERO); BigDecimal orDefault = amountMap.getOrDefault(draftBillId, BigDecimal.ZERO);
amountMap.put(draftBillId, orDefault.add(row.getBigDecimal("entrys.billamt"))); amountMap.put(draftBillId, orDefault.add(row.getBigDecimal("entrys.billamt")));
} }
} catch (Throwable throwable) { } catch (Throwable throwable) {
@ -88,72 +165,38 @@ public class DrafttradebillOP extends AbstractOperationServicePlugIn {
} }
} }
Long draftId = cdm_payablebill.getLong("id");
BigDecimal sumBillAmt = amountMap.getOrDefault(draftId, BigDecimal.ZERO);
try { BigDecimal amount = cdm_payablebill.getBigDecimal("amount");
Long draftId = cdm_payablebill.getLong("id"); boolean isNoAmount = sumBillAmt.compareTo(BigDecimal.ZERO) != 0 && amount.compareTo(sumBillAmt) == 0;
BigDecimal sumBillAmt = (BigDecimal)amountMap.getOrDefault(draftId, BigDecimal.ZERO); String billStatus = cdm_payablebill.getString("billstatus");
BigDecimal amount = cdm_payablebill.getBigDecimal("amount"); String draftbillTranstatus = cdm_payablebill.getString("draftbilltranstatus");
boolean isNoAmount = sumBillAmt.compareTo(BigDecimal.ZERO) != 0 && amount.compareTo(sumBillAmt) == 0; if (!BillStatusEnum.AUDIT.getValue().equals(billStatus) || !DraftTranStatusEnum.SUCCESS.getValue().equals(draftbillTranstatus) || isNoAmount) {
String billStatus = cdm_payablebill.getString("billstatus"); logger.info("单据编号:" + cdm_payablebill.getString("billno") + "单据状态为“已审核”且票据交易状态为“交易成功”的未兑付票据才能操作票据兑付。");
String draftbillTranstatus = cdm_payablebill.getString("draftbilltranstatus"); continue;
if (!BillStatusEnum.AUDIT.getValue().equals(billStatus) || !DraftTranStatusEnum.SUCCESS.getValue().equals(draftbillTranstatus) || isNoAmount) {
logger.info("单据编号:"+cdm_payablebill.getString("billno")+"单据状态为“已审核”且票据交易状态为“交易成功”的未兑付票据才能操作票据兑付。");
continue;
}
List<Long> dataEntitys = new ArrayList<>();
dataEntitys.add(Long.parseLong(cdm_payablebill.getString("id")));
OperateOption option = OperateOption.create();
option.setVariableValue("tradeType", "redeem");
option.setVariableValue("remarks", "后台下推");
//调用方法执行数据 - 自动兑付下推
OperationResult operationResult = OperationServiceHelper.executeOperate("pushandsaveredeem",
CdmEntityConst.CDM_PAYABLEBILL,
dataEntitys.toArray(new Object[]{}), option);
if (operationResult.getAllErrorInfo().size()!=0) {
logger.info("单据编号:"+cdm_payablebill.getString("billno")+"后台生单失败,原因:"+operationResult.getAllErrorInfo().get(0).getMessage());
String targetpkvalue = operationResult.getAllErrorInfo().get(0).getErrorCustInfos().get("targetpkvalue");
if (!StringUtil.isEmpty(targetpkvalue)){
DynamicObject cdm_drafttradebill = BusinessDataServiceHelper.loadSingle(targetpkvalue, "cdm_drafttradebill");
OperationResult delete = OperationServiceHelper.executeOperate("delete", "cdm_drafttradebill", new DynamicObject[]{cdm_drafttradebill}, OperateOption.create());
if (delete.isSuccess()==true){
logger.info("删除单据编号:"+cdm_payablebill.getString("billno")+"成功");
}else {
logger.info("删除单据编号:"+cdm_payablebill.getString("billno")+"失败,原因:"+delete.getMessage());
}
}
}else {
// logger.info("单据编号:"+cdm_payablebill.getString("billno")+"后台生单成功");
// //根据源单id查询源单下游单据
// Map<String, HashSet<Long>> tarBills = BFTrackerServiceHelper.findTargetBills("cdm_payablebill", new Long[]{cdm_payablebill.getLong("id")});
// //返回的Map对象 如果有数据 String 对应的是 目标单据标识 value 对应的目标单ids
// Set<DynamicObject> bill = new HashSet<DynamicObject>();
// if (tarBills.containsKey("cdm_drafttradebill")) {
// HashSet<Long> billId = tarBills.get("cdm_drafttradebill");
// for (Long aLong : billId) {
// QFilter billfilter = new QFilter("ltradetype", QCP.equals, "redeem");
// billfilter.and(new QFilter("billstatus", QCP.equals, "C"));
// billfilter.and(new QFilter("id", QCP.equals, aLong));
// DynamicObject cdm_drafttradebill = BusinessDataServiceHelper.loadSingle("cdm_drafttradebill", "id,billno", billfilter.toArray());
// if (cdm_drafttradebill != null) {
// OperationResult operation = OperationServiceHelper.executeOperate("drawbillsave", "cdm_drafttradebill", new Object[]{aLong}, OperateOption.create());
// if (operation.isSuccess()) {
// logger.info("单据编号:" + cdm_drafttradebill.getString("billno") + "确认交易成功");
// } else {
// logger.info("单据编号:" + cdm_drafttradebill.getString("billno") + "确认交易失败,请检查交易状态或者手动操作");
// }
// }
// }
// }
}
} catch (NumberFormatException exception) {
logger.error("单据编号:"+cdm_payablebill.getString("billno")+"后台生单錯誤原因:"+exception.getMessage());
} }
list.add(Long.parseLong(cdm_payablebill.getString("id")));
} }
} }
} }
public boolean checkIfAllNumbersAreSame(List<DynamicObject> dynamicObjects,String s) {
if (dynamicObjects == null || dynamicObjects.isEmpty()) {
return true; // 空集合可以认为是所有 number 相同
}
// 获取第一个 DynamicObject number
Object firstNumber = dynamicObjects.get(0).get(s);
// 检查其余的 DynamicObject number 值是否与第一个相同
for (DynamicObject dynamicObject : dynamicObjects) {
Object currentNumber = dynamicObject.get(s);
if (!currentNumber.equals(firstNumber)) {
return false; // 如果发现不同的 number则返回 false
}
}
return true; // 所有 number 值相同返回 true
}
} }