feat: 金蝶锁单改1对1逐张调用并新增列表锁状态展示

This commit is contained in:
刘泉佳
2026-10-10 15:18:57 +08:00
parent 558c20efc2
commit 15d6f736e5
10 changed files with 178 additions and 69 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
@@ -36,17 +36,18 @@ 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 自代理同一惯用法)
@@ -105,31 +106,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
@@ -219,6 +225,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 +253,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 +304,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;