记录抢单历史

This commit is contained in:
2026-06-11 22:51:00 +08:00
parent 49d5518fc5
commit 53678a4a46

View File

@@ -6,7 +6,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.dto.LbBuyAccountRushBuyContext; import com.rj.dto.LbBuyAccountRushBuyContext;
import com.rj.dto.LbRushBuyGoodsCoordinator; import com.rj.dto.LbRushBuyGoodsCoordinator;
import com.rj.entity.LbBuyAccount; import com.rj.entity.LbBuyAccount;
import com.rj.entity.LbBuyAccountHistory;
import com.rj.mapper.LbBuyAccountMapper; import com.rj.mapper.LbBuyAccountMapper;
import com.rj.service.ILbBuyAccountHistoryService;
import com.rj.service.ILbBuyAccountService; import com.rj.service.ILbBuyAccountService;
import com.rj.service.ILbGoodsService; import com.rj.service.ILbGoodsService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -45,6 +47,9 @@ public class LbBuyAccountServiceImpl
@Autowired @Autowired
private ILbGoodsService lbGoodsService; private ILbGoodsService lbGoodsService;
@Autowired
private ILbBuyAccountHistoryService lbBuyAccountHistoryService;
@Override @Override
public Map<String, Object> add(LbBuyAccount entity) { public Map<String, Object> add(LbBuyAccount entity) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
@@ -390,9 +395,10 @@ public class LbBuyAccountServiceImpl
resultMessage = attachAccountLabel( resultMessage = attachAccountLabel(
accountLabel, formatRushBuyResultMessage(rushBuyResult)); accountLabel, formatRushBuyResultMessage(rushBuyResult));
persistRushBuyOutcome( persistRushBuyOutcome(
account.getId(), account,
accountLabel, accountLabel,
formatRushBuyResultForDb(accountLabel, rushBuyResult)); formatRushBuyResultForDb(accountLabel, rushBuyResult),
rushBuyResult);
} }
} }
@@ -400,7 +406,7 @@ public class LbBuyAccountServiceImpl
item.put("message", resultMessage); item.put("message", resultMessage);
log.info("抢单任务结束,{}success={}message={}", accountLabel, item.get("success"), resultMessage); log.info("抢单任务结束,{}success={}message={}", accountLabel, item.get("success"), resultMessage);
if (account != null && account.getId() != null && item.get("rushBuyResult") == null) { if (account != null && account.getId() != null && item.get("rushBuyResult") == null) {
persistRushBuyOutcome(account.getId(), accountLabel, resultMessage); persistRushBuyOutcome(account, accountLabel, resultMessage, null);
} }
return item; return item;
} }
@@ -412,9 +418,10 @@ public class LbBuyAccountServiceImpl
String accountLabel = formatRushBuyAccountLabel(account.getId(), account); String accountLabel = formatRushBuyAccountLabel(account.getId(), account);
Object message = item.get("message"); Object message = item.get("message");
persistRushBuyOutcome( persistRushBuyOutcome(
account.getId(), account,
accountLabel, accountLabel,
message != null ? message.toString() : "未知抢单结果"); message != null ? message.toString() : "未知抢单结果",
null);
} }
private Map<String, Object> validateRequiredForAdd(LbBuyAccount entity) { private Map<String, Object> validateRequiredForAdd(LbBuyAccount entity) {
@@ -494,18 +501,24 @@ public class LbBuyAccountServiceImpl
} }
/** /**
* 将本次抢单结果与抢单时间写入 lb_buy_accountrush_buy_result、last_rush_buy_time * 将本次抢单结果与抢单时间写入 lb_buy_accountrush_buy_result、last_rush_buy_time
* 并新增一条 lb_buy_account_history 抢单历史记录。
*/ */
private void persistRushBuyOutcome(String accountId, String accountLabel, String resultMessage) { private void persistRushBuyOutcome(
if (accountId == null || accountId.trim().isEmpty()) { LbBuyAccount account,
String accountLabel,
String resultMessage,
Map<String, Object> rushBuyResult) {
if (account == null || account.getId() == null || account.getId().trim().isEmpty()) {
return; return;
} }
String accountId = account.getId().trim();
String storedResult = truncateRushBuyResult( String storedResult = truncateRushBuyResult(
resultMessage != null && !resultMessage.isEmpty() ? resultMessage : "未知抢单结果"); resultMessage != null && !resultMessage.isEmpty() ? resultMessage : "未知抢单结果");
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
try { try {
int rows = this.baseMapper.updateRushBuyOutcome( int rows = this.baseMapper.updateRushBuyOutcome(
accountId.trim(), storedResult, now, now); accountId, storedResult, now, now);
if (rows <= 0) { if (rows <= 0) {
log.warn("抢单结果落库未更新任何行,{}accountId={}lastRushBuyTime={}", log.warn("抢单结果落库未更新任何行,{}accountId={}lastRushBuyTime={}",
accountLabel, accountId, now); accountLabel, accountId, now);
@@ -517,6 +530,103 @@ public class LbBuyAccountServiceImpl
log.error("抢单结果落库异常,{}accountId={}lastRushBuyTime={}", log.error("抢单结果落库异常,{}accountId={}lastRushBuyTime={}",
accountLabel, accountId, now, e); accountLabel, accountId, now, e);
} }
persistRushBuyHistory(account, storedResult, rushBuyResult, now);
}
/**
* 每次抢单完成后向 lb_buy_account_history 新增一条历史记录。
*/
private void persistRushBuyHistory(
LbBuyAccount account,
String rushBuyResultText,
Map<String, Object> rushBuyResult,
LocalDateTime rushBuyTime) {
if (account == null) {
return;
}
String tenantId = account.getTenantId();
String tenantName = account.getTenantName();
String loginAccount = account.getLoginAccount();
if (tenantId == null || tenantId.trim().isEmpty()
|| tenantName == null || tenantName.trim().isEmpty()
|| loginAccount == null || loginAccount.trim().isEmpty()) {
log.warn("抢单历史落库跳过账号缺少必填字段accountId={}", account.getId());
return;
}
LbBuyAccountHistory history = new LbBuyAccountHistory();
history.setId(UUID.randomUUID().toString());
history.setTenantId(tenantId.trim());
history.setTenantName(tenantName.trim());
history.setLoginAccount(loginAccount.trim());
history.setLoginPassword(
account.getLoginPassword() != null ? account.getLoginPassword().trim() : "");
if (account.getNickname() != null) {
history.setNickname(account.getNickname().trim());
}
populateRushBuyHistoryStats(history, rushBuyResult);
history.setRushBuyResult(rushBuyResultText);
history.setLastRushBuyTime(rushBuyTime);
history.setCreateTime(rushBuyTime);
history.setUpdateTime(rushBuyTime);
try {
boolean ok = lbBuyAccountHistoryService.save(history);
if (ok) {
log.info("抢单历史已落库accountId={}loginAccount={}lastRushBuyTime={}"
+ "maxGrabCount={}rushBuyFailCount={}maxGrabAmount={}",
account.getId(), loginAccount, rushBuyTime,
history.getMaxGrabCount(), history.getRushBuyFailCount(), history.getMaxGrabAmount());
} else {
log.warn("抢单历史落库失败accountId={}loginAccount={}",
account.getId(), loginAccount);
}
} catch (Exception e) {
log.error("抢单历史落库异常accountId={}loginAccount={}",
account.getId(), loginAccount, e);
}
}
private static void populateRushBuyHistoryStats(
LbBuyAccountHistory history, Map<String, Object> rushBuyResult) {
int successCount = 0;
int failCount = 0;
int totalAmount = 0;
if (rushBuyResult != null) {
successCount = toNonNegativeInt(rushBuyResult.get("successCount"));
failCount = toNonNegativeInt(rushBuyResult.get("failCount"));
Object detailsObj = rushBuyResult.get("details");
if (detailsObj instanceof List<?> details) {
for (Object detailObj : details) {
if (!(detailObj instanceof Map<?, ?> detail)) {
continue;
}
if (Boolean.TRUE.equals(detail.get("success"))) {
totalAmount += toNonNegativeInt(detail.get("totalMoney"));
}
}
}
}
history.setMaxGrabAmount(totalAmount);
history.setMaxGrabCount(successCount + failCount);
history.setRushBuyFailCount(failCount);
}
private static int toNonNegativeInt(Object value) {
if (value == null) {
return 0;
}
if (value instanceof Number number) {
return Math.max(0, number.intValue());
}
try {
return Math.max(0, Integer.parseInt(value.toString().trim()));
} catch (NumberFormatException e) {
return 0;
}
} }
private static String formatRushBuyAccountLabel(String accountId, LbBuyAccount account) { private static String formatRushBuyAccountLabel(String accountId, LbBuyAccount account) {