修改同步计费

This commit is contained in:
wuweihua
2026-10-13 10:21:28 +08:00
parent 26bdd3ba87
commit 4d27887d4a
14 changed files with 1231 additions and 12 deletions
@@ -0,0 +1,185 @@
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.PrcBillingTask;
import com.bms.price.domain.PrcBillingTaskRetryTask;
import com.bms.price.domain.vo.BillingTaskSubmitRequest;
import com.bms.price.domain.vo.TabOrderMonitorQuery;
import com.bms.price.domain.vo.TabOrderMonitorVo;
import com.bms.price.enums.BillTaskSuccessStatus;
import com.bms.price.oracle.TabOrderMonitorService;
import com.bms.price.search.IBillSearchStore;
import com.bms.price.search.TabOrderSyncEsLog;
import com.bms.price.service.IPrcBillingTaskService;
/**
* Page Oracle TAB_ORDER then submitAndProcess each row.
*/
@Component
public class TabOrderSyncAsyncRunner
{
private static final Logger log = LoggerFactory.getLogger(TabOrderSyncAsyncRunner.class);
public static final String CACHE_KEY_PREFIX = "billing:orderSource:sync:";
public static final int TTL_MINUTES = 120;
public static final int DEFAULT_BATCH = 50;
public static final int MIN_BATCH = 20;
public static final int MAX_BATCH = 200;
@Autowired
private TabOrderMonitorService tabOrderMonitorService;
@Autowired
@Lazy
private IPrcBillingTaskService billingTaskService;
@Autowired
private RedisCache redisCache;
@Autowired
private IBillSearchStore billSearchStore;
@Async("threadPoolTaskExecutor")
public void run(String asyncTaskId, TabOrderMonitorQuery query, int batchSize)
{
int batch = clampBatch(batchSize);
try
{
long totalLong = tabOrderMonitorService.count(query);
int total = totalLong > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) totalLong;
put(asyncTaskId, PrcBillingTaskRetryTask.STATUS_RUNNING, 0, "starting", total, 0, 0, 0, null);
if (total <= 0)
{
put(asyncTaskId, PrcBillingTaskRetryTask.STATUS_SUCCESS, 100, "no tasks", 0, 0, 0, 0, null);
return;
}
int processed = 0;
int ok = 0;
int fail = 0;
int pageNum = 1;
while (true)
{
List<TabOrderMonitorVo> rows = tabOrderMonitorService.selectPage(query, pageNum, batch);
if (rows == null || rows.isEmpty())
{
break;
}
put(asyncTaskId, PrcBillingTaskRetryTask.STATUS_RUNNING,
Math.min(99, (int) ((processed * 100L) / Math.max(1, total))),
"processing " + processed + "/" + total, total, processed, ok, fail, null);
for (TabOrderMonitorVo row : rows)
{
try
{
PrcBillingTask task = billingTaskService.submitAndProcess(toSubmit(row));
if (task != null && BillTaskSuccessStatus.SUCCESS.getCode().equals(task.getSuccessStatus()))
{
ok++;
}
else
{
fail++;
}
}
catch (RuntimeException e)
{
fail++;
log.warn("orderSource sync failed orderId={} transfer={} msg={}",
row == null ? null : row.getOrderId(),
row == null ? null : row.getTransferOrderId(),
e.getMessage());
}
processed++;
int percent = Math.min(99, (int) ((processed * 100L) / Math.max(1, total)));
put(asyncTaskId, PrcBillingTaskRetryTask.STATUS_RUNNING, percent,
"processing " + processed + "/" + total, total, processed, ok, fail, null);
}
if (rows.size() < batch)
{
break;
}
pageNum++;
}
put(asyncTaskId, PrcBillingTaskRetryTask.STATUS_SUCCESS, 100,
"done ok=" + ok + " fail=" + fail, total, processed, ok, fail, null);
java.util.Map<String, Object> resp = new java.util.LinkedHashMap<String, Object>();
resp.put("asyncTaskId", asyncTaskId);
resp.put("total", Integer.valueOf(total));
resp.put("processed", Integer.valueOf(processed));
resp.put("ok", Integer.valueOf(ok));
resp.put("fail", Integer.valueOf(fail));
TabOrderSyncEsLog.save(billSearchStore, asyncTaskId, "FILTER_DONE", query, resp, "SUCCESS", null);
log.info("orderSource sync done asyncTaskId={} total={} ok={} fail={}", asyncTaskId, total, ok, fail);
}
catch (Exception e)
{
log.error("orderSource sync failed asyncTaskId={}", asyncTaskId, e);
String msg = e.getMessage();
if (StringUtils.isEmpty(msg))
{
msg = e.getClass().getSimpleName();
}
put(asyncTaskId, PrcBillingTaskRetryTask.STATUS_FAIL, 100, "failed", 0, 0, 0, 0, msg);
TabOrderSyncEsLog.save(billSearchStore, asyncTaskId, "FILTER_DONE", query, null, "FAIL", msg);
}
}
public void put(String taskId, String status, int percent, String message,
int total, int processed, int ok, int fail, String error)
{
PrcBillingTaskRetryTask task = new PrcBillingTaskRetryTask();
task.setTaskId(taskId);
task.setStatus(status);
task.setPercent(percent);
task.setMessage(message);
task.setTotal(total);
task.setProcessed(processed);
task.setOk(ok);
task.setFail(fail);
task.setError(error);
redisCache.setCacheObject(CACHE_KEY_PREFIX + taskId, task, TTL_MINUTES, TimeUnit.MINUTES);
}
public PrcBillingTaskRetryTask get(String taskId)
{
return redisCache.getCacheObject(CACHE_KEY_PREFIX + taskId);
}
public static int clampBatch(Integer batchSize)
{
int n = batchSize == null ? DEFAULT_BATCH : batchSize.intValue();
if (n < MIN_BATCH)
{
return MIN_BATCH;
}
if (n > MAX_BATCH)
{
return MAX_BATCH;
}
return n;
}
public static BillingTaskSubmitRequest toSubmit(TabOrderMonitorVo row)
{
if (row == null)
{
throw new com.bms.common.exception.ServiceException("order row required");
}
BillingTaskSubmitRequest req = new BillingTaskSubmitRequest();
req.setTransferNo(row.getTransferOrderId());
req.setTrackingNo(row.getOrgOrderId());
req.setOrderNo(row.getOrderId());
req.setCustomerCode(row.getCustomerCode());
req.setBillTime(row.getPickupDate());
return req;
}
}