增加产品价和红线价重计费功能

This commit is contained in:
wuweihua
2026-10-15 16:09:05 +08:00
parent 41ce05a08a
commit 071e7f0c55
9 changed files with 615 additions and 25 deletions
@@ -0,0 +1,114 @@
package com.bms.price.task;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.bms.common.core.redis.RedisCache;
import com.bms.common.utils.StringUtils;
import com.bms.price.domain.BatchAddIncomeBillFeeTask;
import com.bms.price.service.IPrcOrderBillService;
/** Async: recalc product_amount or redline_amount only (no AR guard). */
@Component
public class PrcRecalcBillPriceAsyncRunner
{
private static final Logger log = LoggerFactory.getLogger(PrcRecalcBillPriceAsyncRunner.class);
public static final String CACHE_KEY_PREFIX = "price:billing:recalcPrice:task:";
public static final int TTL_MINUTES = 120;
@Autowired
@Lazy
private IPrcOrderBillService orderBillService;
@Autowired
private RedisCache redisCache;
@Async("threadPoolTaskExecutor")
public void run(String taskId, String mode, List<String> billIds)
{
int total = billIds == null ? 0 : billIds.size();
int success = 0;
int fail = 0;
StringBuilder failTips = new StringBuilder();
try
{
put(taskId, BatchAddIncomeBillFeeTask.STATUS_RUNNING, 1, "running", null, total, 0, 0);
if (total == 0)
{
put(taskId, BatchAddIncomeBillFeeTask.STATUS_FAIL, 100, "failed", "no bills", 0, 0, 0);
return;
}
for (int i = 0; i < total; i++)
{
String billId = billIds.get(i);
try
{
orderBillService.recalcBillPriceByBillId(billId, mode);
success++;
}
catch (Exception e)
{
fail++;
appendFail(failTips, billId, e);
log.warn("recalc price failed taskId={} mode={} billId={}", taskId, mode, billId, e);
}
int percent = Math.min(99, (int) Math.floor(100.0 * (i + 1) / total));
put(taskId, BatchAddIncomeBillFeeTask.STATUS_RUNNING, percent,
"processing " + (i + 1) + "/" + total, null, total, success, fail);
}
String msg = "success=" + success + ", fail=" + fail;
put(taskId, BatchAddIncomeBillFeeTask.STATUS_SUCCESS, 100, msg,
fail > 0 && failTips.length() > 0 ? failTips.toString() : null, total, success, fail);
log.info("recalc price done taskId={} mode={} total={} success={} fail={}",
taskId, mode, total, success, fail);
}
catch (Exception e)
{
log.error("recalc price aborted taskId={} mode={}", taskId, mode, e);
put(taskId, BatchAddIncomeBillFeeTask.STATUS_FAIL, 100, "failed", errorMessage(e), total, success, fail);
}
}
public void put(String taskId, String status, int percent, String message, String error,
int total, int success, int fail)
{
BatchAddIncomeBillFeeTask task = new BatchAddIncomeBillFeeTask();
task.setTaskId(taskId);
task.setStatus(status);
task.setPercent(percent);
task.setMessage(message);
task.setError(error);
task.setTotal(total);
task.setSuccess(success);
task.setFail(fail);
redisCache.setCacheObject(CACHE_KEY_PREFIX + taskId, task, TTL_MINUTES, TimeUnit.MINUTES);
}
public BatchAddIncomeBillFeeTask get(String taskId)
{
return redisCache.getCacheObject(CACHE_KEY_PREFIX + taskId);
}
private static void appendFail(StringBuilder failTips, String billId, Exception e)
{
if (failTips.length() >= 800)
{
return;
}
if (failTips.length() > 0)
{
failTips.append("; ");
}
failTips.append(billId).append(":").append(errorMessage(e));
}
private static String errorMessage(Exception e)
{
return StringUtils.isEmpty(e.getMessage()) ? e.getClass().getSimpleName() : e.getMessage();
}
}