Merge branch 'feature/kingdee-bill-lock' into feature/payment-apply-bfi-sync

This commit is contained in:
刘泉佳
2026-10-12 10:53:05 +08:00
13 changed files with 285 additions and 84 deletions
@@ -13,6 +13,7 @@ import org.springblade.thirdparty.bfi.pojo.dto.BfiBillLockDTO;
import org.springblade.transport.pojo.entity.KingdeeBillLock;
import java.util.List;
import java.util.Map;
/**
* 金蝶单据锁定记录服务
@@ -24,7 +25,8 @@ public interface IKingdeeBillLockService extends IService<KingdeeBillLock> {
/**
* 推送金蝶成功后锁单(由各推送链路在主单事务提交后调用):
* 每张金蝶目标单一行锁记录(先落 failed 再调 lockBusiData,成功翻 locked);
* 同源单的目标单合并为一次调用(编号逗号拼接批量传参);已 locked 幂等跳过,failed 原地重锁并重置补偿计数;
* 锁单接口 1对1:逐张调用(单次一个源单号对一个金蝶单号),单张成败互不影响;
* 已 locked 幂等跳过,failed 原地重锁并重置补偿计数;
* 异常独立捕获,不影响主单同步状态
*
* @param sourceBillType WLTMS 单据类型(SETTLEMENT/INVOICE_APPLY)
@@ -55,9 +57,22 @@ public interface IKingdeeBillLockService extends IService<KingdeeBillLock> {
void checkEditable(String sourceBillType, Long sourceBillId);
/**
* 锁定补偿:扫 failed 且未超重试上限的锁记录重调 lockBusiData(由定时任务调用)
* 批量查源单当前锁状态(供列表/详情页填充展示标识):
* 每源单取最新一条非终态锁记录(locked/failed),unlocked 为终态不返回(前端视为无锁/已解锁)
*
* @return 补偿摘要
* @param sourceBillType WLTMS 单据类型
* @param sourceBillIds 源单主键集合
* @return sourceBillId -&gt; 最新锁记录,无活跃锁的源单不在 Map 中
*/
Map<Long, KingdeeBillLock> mapActiveLocks(String sourceBillType, List<Long> sourceBillIds);
/**
* 锁定补偿:扫 failed 且 try_count 未达重试上限(RETRY_MAX_COUNT)的锁记录,逐张重调 lockBusiData
* (接口 1对1:单次一个源单号对一个金蝶单号,单张成败互不影响;定时任务定时触发,亦可人工触发);
* 成功翻 locked,失败 try_count+1 并记失败原因;
* 计数烧满上限后停止自动补偿(人工重推「同步金蝶」会重置计数重新接管)。锁记录落库均为批量更新
*
* @return 补偿摘要(待补偿/成功张数),供调用方记日志或页面提示
*/
String retryFailedLocks();
}
@@ -42,6 +42,7 @@ import org.springblade.transport.mapper.ReceiptClaimSettlementMapper;
import org.springblade.transport.constant.KingdeeAttachmentConstant;
import org.springblade.transport.constant.KingdeeBfiConstant;
import org.springblade.transport.constant.KingdeeLockConstant;
import org.springblade.transport.pojo.entity.KingdeeBillLock;
import org.springblade.transport.constant.TransportBusiConstant;
import org.springblade.transport.mapper.ReceivablePayableDetailMapper;
import org.springblade.transport.mapper.ReceivablePayableCargoFeeMapper;
@@ -209,9 +210,34 @@ public class FormalSettlementServiceImpl extends BaseServiceImpl<FormalSettlemen
IPage<FormalSettlement> result = page(page, wrapper.orderByDesc(FormalSettlement::getCreateTime));
IPage<FormalSettlementVO> voPage = result.convert(this::toVO);
fillAttachmentSyncStatus(voPage.getRecords());
fillKingdeeLockStatus(voPage.getRecords());
return voPage;
}
/**
* 填充金蝶锁状态标识(locked-锁定中/failed-锁定失败;unlocked 终态不返回):
* 锁表按本页源单一次批量查询,避免 N+1;locked 时前端可禁用编辑/作废,
* failed 时展示失败原因(tryCount 达上限 5 提示重推同步金蝶)
*
* @param rows 本页 VO 列表
*/
private void fillKingdeeLockStatus(List<FormalSettlementVO> rows) {
if (Func.isEmpty(rows)) {
return;
}
Map<Long, KingdeeBillLock> lockMap = kingdeeBillLockService.mapActiveLocks(KingdeeLockConstant.SOURCE_SETTLEMENT,
rows.stream().map(FormalSettlementVO::getId).toList());
for (FormalSettlementVO vo : rows) {
KingdeeBillLock lockRecord = lockMap.get(vo.getId());
if (lockRecord == null) {
continue;
}
vo.setKingdeeLockStatus(lockRecord.getLockStatus());
vo.setKingdeeLockFailReason(lockRecord.getFailReason());
vo.setKingdeeTryCount(lockRecord.getTryCount());
}
}
/**
* 填充附件同步聚合态(无附件/未上传/部分未传/已上传):按结算类型分业务类型一次 GROUP BY 查询,避免 N+1
*
@@ -280,6 +306,7 @@ public class FormalSettlementServiceImpl extends BaseServiceImpl<FormalSettlemen
public FormalSettlementVO detail(Long id) {
FormalSettlement settlement = existing(id);
FormalSettlementVO vo = toVO(settlement);
fillKingdeeLockStatus(List.of(vo));
List<FormalSettlementSource> sources = sourceMapper.selectList(Wrappers.<FormalSettlementSource>lambdaQuery()
.eq(FormalSettlementSource::getFormalSettlementId, id).orderByAsc(FormalSettlementSource::getCreateTime));
vo.setSources(sources);
@@ -56,6 +56,7 @@ import org.springblade.transport.config.BfiProperties;
import org.springblade.transport.constant.KingdeeAttachmentConstant;
import org.springblade.transport.constant.KingdeeBfiConstant;
import org.springblade.transport.constant.KingdeeLockConstant;
import org.springblade.transport.pojo.entity.KingdeeBillLock;
import org.springblade.transport.constant.TransportBusiConstant;
import org.springblade.transport.mapper.ContractManageMapper;
import org.springblade.transport.mapper.CustomerArchiveMapper;
@@ -180,9 +181,34 @@ public class InvoiceApplicationServiceImpl extends BaseServiceImpl<InvoiceApplic
.orderByDesc(InvoiceApplication::getCreateTime);
IPage<InvoiceApplicationVO> voPage = page(page, wrapper).convert(this::toListVO);
fillAttachmentSyncStatus(voPage.getRecords());
fillKingdeeLockStatus(voPage.getRecords());
return voPage;
}
/**
* 填充金蝶锁状态标识(locked-锁定中/failed-锁定失败;unlocked 终态不返回):
* 锁表按本页源单一次批量查询,避免 N+1;locked 时前端可禁用编辑/作废,
* failed 时展示失败原因(tryCount 达上限 5 提示重推同步金蝶)
*
* @param rows 本页 VO 列表
*/
private void fillKingdeeLockStatus(List<InvoiceApplicationVO> rows) {
if (Func.isEmpty(rows)) {
return;
}
Map<Long, KingdeeBillLock> lockMap = kingdeeBillLockService.mapActiveLocks(KingdeeLockConstant.SOURCE_INVOICE_APPLY,
rows.stream().map(InvoiceApplicationVO::getId).toList());
for (InvoiceApplicationVO vo : rows) {
KingdeeBillLock lockRecord = lockMap.get(vo.getId());
if (lockRecord == null) {
continue;
}
vo.setKingdeeLockStatus(lockRecord.getLockStatus());
vo.setKingdeeLockFailReason(lockRecord.getFailReason());
vo.setKingdeeTryCount(lockRecord.getTryCount());
}
}
/**
* 填充附件同步聚合态(无附件/未上传/部分未传/已上传):同步表按本页 biz_id 一次 GROUP BY 查询,避免 N+1
*
@@ -208,6 +234,7 @@ public class InvoiceApplicationServiceImpl extends BaseServiceImpl<InvoiceApplic
public InvoiceApplicationVO detail(Long id) {
InvoiceApplication entity = existing(id);
InvoiceApplicationVO vo = toListVO(entity);
fillKingdeeLockStatus(List.of(vo));
vo.setSettlements(settlementRelationMapper.selectList(Wrappers.<InvoiceApplicationSettlement>lambdaQuery()
.eq(InvoiceApplicationSettlement::getInvoiceApplicationId, id)
.orderByAsc(InvoiceApplicationSettlement::getCreateTime)));
@@ -546,7 +573,7 @@ public class InvoiceApplicationServiceImpl extends BaseServiceImpl<InvoiceApplic
public String syncKingdee(Long id) {
// 单据级事务经自代理调用:单条失败只回滚自身
String billNo = SpringUtil.getBean(IInvoiceApplicationService.class).doSyncKingdee(id);
// 主单事务提交后锁定金蝶开票单(lockBusiData):每张发票张次一张金蝶单,多张逗号拼接批量锁定;
// 主单事务提交后锁定金蝶开票单(lockBusiData):每张发票张次一张金蝶单,接口1对1逐张锁定;
// 失败只留 failed 锁记录由补偿任务重锁,不污染主单同步状态;锁定失败摘要拼入返回值,页面直接可见
String lockFailSummary = lockKingdeeBillAfterPush(id, billNo);
// 主单事务提交后联带上传附件(A902):失败只记附件同步状态,不回滚不污染主单同步状态,重推自动补传
@@ -556,7 +583,7 @@ public class InvoiceApplicationServiceImpl extends BaseServiceImpl<InvoiceApplic
}
/**
* 金蝶开票单锁定(lockBusiData):A303-1 推送成功事务提交后调用,多张金蝶单批量锁定;
* 金蝶开票单锁定(lockBusiData):A303-1 推送成功事务提交后调用,接口1对1逐张锁定;
* 锁定异常独立捕获,不影响主单同步结果
*
* @param id 开票申请id
@@ -24,29 +24,33 @@ import org.springblade.transport.constant.TransportBusiConstant;
import org.springblade.transport.mapper.FormalSettlementMapper;
import org.springblade.transport.mapper.InvoiceApplicationMapper;
import org.springblade.transport.mapper.KingdeeBillLockMapper;
import org.springblade.transport.mapper.PaymentApplicationMapper;
import org.springblade.transport.pojo.entity.FormalSettlement;
import org.springblade.transport.pojo.entity.InvoiceApplication;
import org.springblade.transport.pojo.entity.KingdeeBillLock;
import org.springblade.transport.pojo.entity.PaymentApplication;
import org.springblade.transport.service.IFormalSettlementService;
import org.springblade.transport.service.IInvoiceApplicationService;
import org.springblade.transport.service.IKingdeeBillLockService;
import org.springblade.transport.service.IPaymentApplicationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 金蝶单据锁定记录服务实现
* <p>
* 锁定:推送链路事务提交后按源单分组批量 LOCK(每组一行到多行锁记录,先落 failed 再调 lockBusiData,
* 成功翻 locked),失败由补偿定时任务重试;解锁:金蝶 unLockNotifyBusiData 回调(支持逗号拼接批量),
* 锁定:推送链路事务提交后逐张 LOCK(接口 1对1:单次一个源单号对一个金蝶单号,每张先落 failed 行占位再调
* lockBusiData,成功翻 locked;单张成败互不影响),失败由补偿定时任务重试;落库统一批量更新。
* 解锁:金蝶 unLockNotifyBusiData 回调(支持逗号拼接批量),
* DELETE 时分发废除源单并还原关联数据。
* 与结算单/开票申请服务互相依赖,为避免构造器循环依赖,单据服务经 {@code SpringUtil.getBean} 运行时获取
* (与本工程 doSyncKingdee 自代理同一惯用法)
@@ -65,6 +69,7 @@ public class KingdeeBillLockServiceImpl extends ServiceImpl<KingdeeBillLockMappe
private final BfiProperties bfiProperties;
private final FormalSettlementMapper formalSettlementMapper;
private final InvoiceApplicationMapper invoiceApplicationMapper;
private final PaymentApplicationMapper paymentApplicationMapper;
@Override
public String lockAfterPush(String sourceBillType, Long sourceBillId, String sourceBillNo, String targetBillType, List<String> targetBillNos) {
@@ -105,31 +110,36 @@ public class KingdeeBillLockServiceImpl extends ServiceImpl<KingdeeBillLockMappe
if (pendingRecords.isEmpty()) {
return null;
}
// 同源单目标单合并一次调用:编号逗号拼接批量传参,整批同成败(应答仅聚合 updateCount)
try {
bfiService.lockBusiData(this.buildLockParam(sourceBillType, sourceBillNo, targetBillType,
pendingRecords.stream().map(KingdeeBillLock::getTargetBillNo).toList(), operator));
LocalDateTime now = LocalDateTime.now();
for (KingdeeBillLock lockRecord : pendingRecords) {
// 锁单接口 1对1:单次仅一个源单号对一个金蝶单号,逐张调用,单张成败互不影响;成败落库批量更新
LocalDateTime now = LocalDateTime.now();
List<KingdeeBillLock> lockedRecords = new ArrayList<>();
List<KingdeeBillLock> failedRecords = new ArrayList<>();
for (KingdeeBillLock lockRecord : pendingRecords) {
try {
bfiService.lockBusiData(this.buildLockParam(sourceBillType, sourceBillNo, targetBillType, lockRecord.getTargetBillNo(), operator));
lockRecord.setLockStatus(KingdeeLockConstant.STATUS_LOCKED);
lockRecord.setLockTime(now);
lockRecord.setFailReason(null);
this.updateById(lockRecord);
}
log.info("金蝶单据批量锁定成功:源单[{}:{}],金蝶单{}张[{}]", sourceBillType, sourceBillNo,
pendingRecords.size(), String.join(",", pendingRecords.stream().map(KingdeeBillLock::getTargetBillNo).toList()));
return null;
} catch (Exception exception) {
String failReason = limitText(exception.getMessage(), 500);
for (KingdeeBillLock lockRecord : pendingRecords) {
lockedRecords.add(lockRecord);
log.info("金蝶单据锁定成功:源单[{}:{}],金蝶单[{}]", sourceBillType, sourceBillNo, lockRecord.getTargetBillNo());
} catch (Exception exception) {
String failReason = limitText(exception.getMessage(), 500);
lockRecord.setTryCount((lockRecord.getTryCount() == null ? 0 : lockRecord.getTryCount()) + 1);
lockRecord.setFailReason(failReason);
this.updateById(lockRecord);
failedRecords.add(lockRecord);
log.error("金蝶单据锁定失败(待补偿重锁):源单[{}:{}],金蝶单[{}],原因:{}", sourceBillType, sourceBillNo, lockRecord.getTargetBillNo(), failReason, exception);
}
String failedNos = pendingRecords.stream().map(KingdeeBillLock::getTargetBillNo).collect(Collectors.joining(","));
log.error("金蝶单据批量锁定失败(待补偿重锁):源单[{}:{}],金蝶单{}张[{}]", sourceBillType, sourceBillNo, pendingRecords.size(), failedNos, exception);
return failedNos;
}
this.updateBatchById(lockedRecords);
this.updateBatchById(failedRecords);
if (failedRecords.isEmpty()) {
log.info("金蝶单据锁定全部成功:源单[{}:{}],金蝶单{}张[{}]", sourceBillType, sourceBillNo, lockedRecords.size(),
lockedRecords.stream().map(KingdeeBillLock::getTargetBillNo).collect(Collectors.joining(",")));
return null;
}
String failedNos = failedRecords.stream().map(KingdeeBillLock::getTargetBillNo).collect(Collectors.joining(","));
log.warn("金蝶单据锁定汇总:源单[{}:{}],成功{}张,失败{}张[{}]", sourceBillType, sourceBillNo, lockedRecords.size(), failedRecords.size(), failedNos);
return failedNos;
}
@Override
@@ -204,6 +214,20 @@ public class KingdeeBillLockServiceImpl extends ServiceImpl<KingdeeBillLockMappe
}
SpringUtil.getBean(IInvoiceApplicationService.class).voidByKingdee(application.getId(), operator);
}
case KingdeeLockConstant.SOURCE_PAY_APPLY -> {
PaymentApplication application = paymentApplicationMapper.selectById(lockRecord.getSourceBillId());
if (application == null || Objects.equals(application.getIsDeleted(), 1)) {
log.warn("金蝶删单回调源付款申请不存在:id={}", lockRecord.getSourceBillId());
return;
}
if (TransportBusiConstant.VOIDED.equals(application.getApprovalStatus())) {
log.info("金蝶删单回调源付款申请[{}]已作废(幂等跳过)", application.getPaymentNo());
return;
}
// 复用 ADR-0005 解锁口径:不作废源单,kingdeeStatus→生成失败、清编号内码(原值留档),
// 不自动重推,人工核实后重新推送(A103-2 勾稽预检防重复推单)
SpringUtil.getBean(IPaymentApplicationService.class).unlockBill(application.getId(), "金蝶删单回调");
}
default -> log.warn("金蝶删单回调暂不支持的单据类型:targetBillType={}(映射预留)", lockRecord.getTargetBillType());
}
}
@@ -219,6 +243,26 @@ public class KingdeeBillLockServiceImpl extends ServiceImpl<KingdeeBillLockMappe
}
}
@Override
public Map<Long, KingdeeBillLock> mapActiveLocks(String sourceBillType, List<Long> sourceBillIds) {
if (Func.isEmpty(sourceBillIds)) {
return Map.of();
}
List<KingdeeBillLock> lockRecords = this.list(Wrappers.<KingdeeBillLock>lambdaQuery()
.eq(KingdeeBillLock::getSourceBillType, sourceBillType)
.in(KingdeeBillLock::getSourceBillId, sourceBillIds)
.in(KingdeeBillLock::getLockStatus, List.of(KingdeeLockConstant.STATUS_LOCKED, KingdeeLockConstant.STATUS_FAILED))
.orderByAsc(KingdeeBillLock::getId));
// 升序取回后写覆盖:每源单保留最新一条;unlocked 终态不参与查询
return lockRecords.stream().collect(Collectors.toMap(KingdeeBillLock::getSourceBillId, Function.identity(), (first, second) -> second));
}
/**
* 锁定补偿:扫 failed 且 try_count 未达重试上限(RETRY_MAX_COUNT)的锁记录,逐张重调 lockBusiData
* (接口 1对1:单次一个源单号对一个金蝶单号,单张成败互不影响,由定时任务定时触发,亦可人工触发)。
* <p>成功翻 locked(补锁时间、清失败原因);失败 try_count+1 并记失败原因,计数烧满上限后不再自动重试
* (人工重推「同步金蝶」重置计数重新接管);成败落库均为批量更新(updateBatchById),避免逐张 update
*/
@Override
public String retryFailedLocks() {
List<KingdeeBillLock> failedList = this.list(Wrappers.<KingdeeBillLock>lambdaQuery()
@@ -227,49 +271,49 @@ public class KingdeeBillLockServiceImpl extends ServiceImpl<KingdeeBillLockMappe
if (Func.isEmpty(failedList)) {
return "无待补偿锁记录";
}
// 按源单分组,组内合并一次调用(编号逗号拼接批量传参,整批同成败)
Map<String, List<KingdeeBillLock>> groupBySource = failedList.stream()
.collect(Collectors.groupingBy(record -> record.getSourceBillType() + ":" + record.getSourceBillId(), LinkedHashMap::new, Collectors.toList()));
int success = 0;
for (List<KingdeeBillLock> group : groupBySource.values()) {
KingdeeBillLock first = group.get(0);
log.info("金蝶单据锁定补偿开始:待补偿{}张", failedList.size());
LocalDateTime now = LocalDateTime.now();
List<KingdeeBillLock> lockedRecords = new ArrayList<>();
List<KingdeeBillLock> failedRecords = new ArrayList<>();
for (KingdeeBillLock lockRecord : failedList) {
String operator = Func.isBlank(lockRecord.getLockOperator()) ? "system" : lockRecord.getLockOperator();
try {
bfiService.lockBusiData(this.buildLockParam(first.getSourceBillType(), first.getSourceBillNo(), first.getTargetBillType(),
group.stream().map(KingdeeBillLock::getTargetBillNo).toList(),
Func.isBlank(first.getLockOperator()) ? "system" : first.getLockOperator()));
LocalDateTime now = LocalDateTime.now();
for (KingdeeBillLock lockRecord : group) {
lockRecord.setLockStatus(KingdeeLockConstant.STATUS_LOCKED);
lockRecord.setLockTime(now);
lockRecord.setFailReason(null);
this.updateById(lockRecord);
}
success += group.size();
bfiService.lockBusiData(this.buildLockParam(lockRecord.getSourceBillType(), lockRecord.getSourceBillNo(),
lockRecord.getTargetBillType(), lockRecord.getTargetBillNo(), operator));
lockRecord.setLockStatus(KingdeeLockConstant.STATUS_LOCKED);
lockRecord.setLockTime(now);
lockRecord.setFailReason(null);
lockedRecords.add(lockRecord);
log.info("金蝶单据锁定补偿成功:源单[{}:{}],金蝶单[{}]",
lockRecord.getSourceBillType(), lockRecord.getSourceBillNo(), lockRecord.getTargetBillNo());
} catch (Exception exception) {
String failReason = limitText(exception.getMessage(), 500);
for (KingdeeBillLock lockRecord : group) {
lockRecord.setTryCount((lockRecord.getTryCount() == null ? 0 : lockRecord.getTryCount()) + 1);
lockRecord.setFailReason(failReason);
this.updateById(lockRecord);
}
log.error("金蝶单据锁定补偿失败:源单[{}:{}],金蝶单{}张", first.getSourceBillType(), first.getSourceBillNo(), group.size(), exception);
lockRecord.setTryCount((lockRecord.getTryCount() == null ? 0 : lockRecord.getTryCount()) + 1);
lockRecord.setFailReason(failReason);
failedRecords.add(lockRecord);
log.error("金蝶单据锁定补偿失败(try_count已+1,待下次补偿):源单[{}:{}],金蝶单[{}],原因:{}",
lockRecord.getSourceBillType(), lockRecord.getSourceBillNo(), lockRecord.getTargetBillNo(), failReason, exception);
}
}
return "待补偿" + failedList.size() + "张,成功" + success + "张";
this.updateBatchById(lockedRecords);
this.updateBatchById(failedRecords);
log.info("金蝶单据锁定补偿结束:待补偿{}张,成功{}张,仍失败{}张[{}]", failedList.size(), lockedRecords.size(), failedRecords.size(),
failedRecords.stream().map(KingdeeBillLock::getTargetBillNo).collect(Collectors.joining(",")));
return "待补偿" + failedList.size() + "张,成功" + lockedRecords.size() + "张";
}
/**
* 组装 lockBusiData 参数:WLTMS 调金蝶,源系统取配置(默认 WLTMS),目标系统 XH,
* 操作类型固定 LOCK/SAVE,多张目标单编号逗号拼接批量传参
* 操作类型固定 LOCK/SAVE;接口 1对1:单次仅一个源单号对一个金蝶单号
*
* @param sourceBillType WLTMS 单据类型
* @param sourceBillNo WLTMS 单号
* @param targetBillType 金蝶目标单据类型
* @param targetBillNos 金蝶单据编号集合
* @param targetBillNo 金蝶单据编号(单张)
* @param operator 操作人
* @return 锁单参数
*/
private BfiBillLockDTO buildLockParam(String sourceBillType, String sourceBillNo, String targetBillType, List<String> targetBillNos, String operator) {
private BfiBillLockDTO buildLockParam(String sourceBillType, String sourceBillNo, String targetBillType, String targetBillNo, String operator) {
BfiBillLockDTO param = new BfiBillLockDTO();
param.setLockAction(KingdeeLockConstant.ACTION_LOCK);
param.setSourceSystem(Func.isBlank(bfiProperties.getSrcSys()) ? KingdeeLockConstant.SYSTEM_WLTMS : bfiProperties.getSrcSys());
@@ -278,7 +322,7 @@ public class KingdeeBillLockServiceImpl extends ServiceImpl<KingdeeBillLockMappe
param.setSourceBillType(sourceBillType);
param.setSourceBillNo(sourceBillNo);
param.setTargetBillType(targetBillType);
param.setTargetBillNo(String.join(",", targetBillNos));
param.setTargetBillNo(targetBillNo);
param.setTime(LocalDateTime.now().format(BFI_DATE_TIME));
param.setOperator(operator);
return param;
@@ -51,6 +51,7 @@ import org.springblade.thirdparty.bfi.util.BfiUtil;
import org.springblade.transport.config.BfiProperties;
import org.springblade.transport.constant.KingdeeAttachmentConstant;
import org.springblade.transport.constant.KingdeeBfiConstant;
import org.springblade.transport.constant.KingdeeLockConstant;
import org.springblade.transport.constant.TransportBusiConstant;
import org.springblade.transport.mapper.BillLedgerMapper;
import org.springblade.transport.mapper.BillLedgerUsageMapper;
@@ -82,6 +83,7 @@ import org.springblade.transport.pojo.entity.FormalSettlement;
import org.springblade.transport.pojo.entity.FormalSettlementDetail;
import org.springblade.transport.pojo.entity.FormalSettlementPayment;
import org.springblade.transport.pojo.entity.FormalSettlementSource;
import org.springblade.transport.pojo.entity.KingdeeBillLock;
import org.springblade.transport.pojo.entity.PaymentApplication;
import org.springblade.transport.pojo.entity.PaymentApplicationInvoice;
import org.springblade.transport.pojo.entity.PaymentApplicationRecord;
@@ -93,6 +95,7 @@ import org.springblade.transport.pojo.entity.TemporaryCreditLimit;
import org.springblade.transport.pojo.vo.PaymentApplicationReferenceAmountVO;
import org.springblade.transport.pojo.vo.PaymentApplicationVO;
import org.springblade.transport.service.IFormalSettlementService;
import org.springblade.transport.service.IKingdeeBillLockService;
import org.springblade.transport.service.IPaymentApplicationService;
import org.springblade.transport.service.KingdeeSyncContext;
import org.springblade.transport.support.KingdeeApBillSyncSupport;
@@ -155,6 +158,7 @@ public class PaymentApplicationServiceImpl extends BaseServiceImpl<PaymentApplic
private final BillLedgerUsageMapper billLedgerUsageMapper;
private final TemporaryCreditLimitMapper temporaryCreditLimitMapper;
private final IFormalSettlementService formalSettlementService;
private final IKingdeeBillLockService kingdeeBillLockService;
private final IBfiService bfiService;
private final KingdeeApBillSyncSupport kingdeeApBillSyncSupport;
private final KingdeeAttachmentSyncSupport kingdeeAttachmentSyncSupport;
@@ -193,6 +197,7 @@ public class PaymentApplicationServiceImpl extends BaseServiceImpl<PaymentApplic
return vo;
});
fillAttachmentSyncStatus(KingdeeAttachmentConstant.BIZ_PAY_APPLICATION, voPage.getRecords());
fillKingdeeLockStatus(voPage.getRecords());
return voPage;
}
@@ -218,10 +223,35 @@ public class PaymentApplicationServiceImpl extends BaseServiceImpl<PaymentApplic
}
}
/**
* 填充金蝶锁状态标识(locked-锁定中/failed-锁定失败;unlocked 终态不返回):
* 锁表按本页源单一次批量查询,避免 N+1;locked 时前端可禁用编辑/作废,
* failed 时展示失败原因(tryCount 达上限 5 提示重推同步金蝶)
*
* @param rows 本页 VO 列表
*/
private void fillKingdeeLockStatus(List<PaymentApplicationVO> rows) {
if (Func.isEmpty(rows)) {
return;
}
Map<Long, KingdeeBillLock> lockMap = kingdeeBillLockService.mapActiveLocks(KingdeeLockConstant.SOURCE_PAY_APPLY,
rows.stream().map(PaymentApplicationVO::getId).toList());
for (PaymentApplicationVO vo : rows) {
KingdeeBillLock lockRecord = lockMap.get(vo.getId());
if (lockRecord == null) {
continue;
}
vo.setKingdeeLockStatus(lockRecord.getLockStatus());
vo.setKingdeeLockFailReason(lockRecord.getFailReason());
vo.setKingdeeTryCount(lockRecord.getTryCount());
}
}
@Override
public PaymentApplicationVO detail(Long id) {
PaymentApplication entity = existing(id);
PaymentApplicationVO vo = PaymentApplicationWrapper.build().entityVO(entity);
fillKingdeeLockStatus(List.of(vo));
vo.setInvoices(invoiceMapper.selectList(Wrappers.<PaymentApplicationInvoice>lambdaQuery()
.eq(PaymentApplicationInvoice::getPaymentApplicationId, id).orderByAsc(PaymentApplicationInvoice::getLineNo)));
vo.setPaymentRecords(recordMapper.selectList(Wrappers.<org.springblade.transport.pojo.entity.PaymentApplicationRecord>lambdaQuery()
@@ -386,6 +416,8 @@ public class PaymentApplicationServiceImpl extends BaseServiceImpl<PaymentApplic
public void voidBill(PaymentApplicationStatusRequest request) {
PaymentApplication entity = lockedPayment(request.getId());
if (!TransportBusiConstant.APPROVED.equals(entity.getApprovalStatus())) throw new ServiceException("仅审批通过的付款申请允许作废");
// 金蝶锁定中禁止作废(lockBusiData 已锁单,金蝶侧未解锁前单据不可改动)
kingdeeBillLockService.checkEditable(KingdeeLockConstant.SOURCE_PAY_APPLY, request.getId());
if (isBillPayment(entity.getPaymentMethod())) releaseBillBalance(entity);
entity.setApprovalStatus(TransportBusiConstant.VOIDED);
entity.setCurrentNode("已作废");
@@ -427,9 +459,34 @@ public class PaymentApplicationServiceImpl extends BaseServiceImpl<PaymentApplic
self().markKingdeeSyncFailed(id);
throw new ServiceException("推送金蝶付款申请失败:" + exception.getMessage());
}
// 主单事务提交后锁定金蝶付款申请单(lockBusiData):一张付款申请对一张金蝶付款申请单,接口1对1;
// 失败只留 failed 锁记录由补偿任务重锁,不污染主单同步状态;锁定失败摘要拼入返回值,页面直接可见
String lockFailSummary = lockKingdeeBillAfterPush(id);
// 主单事务提交后联带上传附件(A902):失败只记附件同步状态,不回滚不污染主单同步状态,重推自动补传
syncAttachmentsAfterPush(id);
return kingdeeBillId;
return Func.isBlank(lockFailSummary) ? kingdeeBillId
: kingdeeBillId + ";注意:金蝶单据锁定未成功(" + lockFailSummary + "),系统将自动补偿重锁,重推同步金蝶可立即补锁";
}
/**
* 金蝶付款申请单锁定(lockBusiData):A103-1/A103-3 推送成功事务提交后调用,
* 一张付款申请对一张金蝶付款申请单;锁定异常独立捕获,不影响主单同步结果
*
* @param id 付款申请id
* @return 锁定失败的金蝶单号摘要,全部锁定成功返回 null
*/
private String lockKingdeeBillAfterPush(Long id) {
try {
PaymentApplication entity = getById(id);
if (entity == null || Func.isBlank(entity.getKingdeeBillNo())) {
return null;
}
return kingdeeBillLockService.lockAfterPush(KingdeeLockConstant.SOURCE_PAY_APPLY, id, entity.getPaymentNo(),
KingdeeLockConstant.TARGET_PAY_APPLY, Func.toStrList(entity.getKingdeeBillNo()));
} catch (Exception exception) {
log.error("付款申请[{}]金蝶单据锁定联带动作异常(锁记录已落库待补偿)", id, exception);
return "锁定动作异常,已留档待补偿";
}
}
/**