增加可修改计费重功能
This commit is contained in:
+195
@@ -0,0 +1,195 @@
|
||||
package com.bms.price.task;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
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.ChargeWeightImportTask;
|
||||
import com.bms.price.service.IPrcOrderBillService;
|
||||
|
||||
/**
|
||||
* Async runner: import charge_weight updates and rebill each order.
|
||||
*/
|
||||
@Component
|
||||
public class PrcChargeWeightImportAsyncRunner
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(PrcChargeWeightImportAsyncRunner.class);
|
||||
|
||||
public static final String CACHE_KEY_PREFIX = "price:billing:chargeWeight:import:task:";
|
||||
public static final int TTL_MINUTES = 60;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private IPrcOrderBillService orderBillService;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
/**
|
||||
* @param items pre-validated rows with billId filled in remark field unused;
|
||||
* use orderNo + chargeWeight; billId stored in customer field as billId carrier
|
||||
* -- better use a dedicated work item. See run(List of Object[]).
|
||||
*/
|
||||
@Async("threadPoolTaskExecutor")
|
||||
public void run(String taskId, List<ChargeWeightImportWorkItem> items, int initialFail)
|
||||
{
|
||||
int total = (items == null ? 0 : items.size()) + Math.max(initialFail, 0);
|
||||
int success = 0;
|
||||
int fail = Math.max(initialFail, 0);
|
||||
StringBuilder failTips = new StringBuilder();
|
||||
try
|
||||
{
|
||||
put(taskId, ChargeWeightImportTask.STATUS_RUNNING, 1, "running", null, total, 0, fail);
|
||||
int workSize = items == null ? 0 : items.size();
|
||||
if (workSize == 0)
|
||||
{
|
||||
if (fail > 0)
|
||||
{
|
||||
put(taskId, ChargeWeightImportTask.STATUS_SUCCESS, 100,
|
||||
"success=0, fail=" + fail, "all rows failed validation", total, 0, fail);
|
||||
}
|
||||
else
|
||||
{
|
||||
put(taskId, ChargeWeightImportTask.STATUS_FAIL, 100, "failed", "no rows", 0, 0, 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < workSize; i++)
|
||||
{
|
||||
ChargeWeightImportWorkItem item = items.get(i);
|
||||
try
|
||||
{
|
||||
orderBillService.updateChargeWeightByBillId(item.getBillId(), item.getChargeWeight());
|
||||
success++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
fail++;
|
||||
if (failTips.length() < 800)
|
||||
{
|
||||
String tip = e.getMessage();
|
||||
if (StringUtils.isEmpty(tip))
|
||||
{
|
||||
tip = e.getClass().getSimpleName();
|
||||
}
|
||||
if (failTips.length() > 0)
|
||||
{
|
||||
failTips.append("; ");
|
||||
}
|
||||
failTips.append(item.getOrderNo()).append(":").append(tip);
|
||||
}
|
||||
log.warn("charge weight import failed taskId={} orderNo={}", taskId, item.getOrderNo(), e);
|
||||
}
|
||||
int done = i + 1 + Math.max(initialFail, 0);
|
||||
int percent = Math.min(99, (int) Math.floor(100.0 * done / Math.max(total, 1)));
|
||||
put(taskId, ChargeWeightImportTask.STATUS_RUNNING, percent,
|
||||
"processing " + done + "/" + total, null, total, success, fail);
|
||||
}
|
||||
String msg = "success=" + success + ", fail=" + fail;
|
||||
if (fail > 0 && failTips.length() > 0)
|
||||
{
|
||||
put(taskId, ChargeWeightImportTask.STATUS_SUCCESS, 100, msg, failTips.toString(),
|
||||
total, success, fail);
|
||||
}
|
||||
else
|
||||
{
|
||||
put(taskId, ChargeWeightImportTask.STATUS_SUCCESS, 100, msg, null, total, success, fail);
|
||||
}
|
||||
log.info("charge weight import done taskId={} total={} success={} fail={}",
|
||||
taskId, total, success, fail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("charge weight import aborted taskId={}", taskId, e);
|
||||
put(taskId, ChargeWeightImportTask.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)
|
||||
{
|
||||
ChargeWeightImportTask task = new ChargeWeightImportTask();
|
||||
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 ChargeWeightImportTask get(String taskId)
|
||||
{
|
||||
return redisCache.getCacheObject(CACHE_KEY_PREFIX + taskId);
|
||||
}
|
||||
|
||||
private static String errorMessage(Exception e)
|
||||
{
|
||||
String msg = e.getMessage();
|
||||
if (StringUtils.isEmpty(msg))
|
||||
{
|
||||
msg = e.getClass().getSimpleName();
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
/** Pre-validated work item for async import. */
|
||||
public static class ChargeWeightImportWorkItem implements java.io.Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String billId;
|
||||
private String orderNo;
|
||||
private BigDecimal chargeWeight;
|
||||
|
||||
public ChargeWeightImportWorkItem()
|
||||
{
|
||||
}
|
||||
|
||||
public ChargeWeightImportWorkItem(String billId, String orderNo, BigDecimal chargeWeight)
|
||||
{
|
||||
this.billId = billId;
|
||||
this.orderNo = orderNo;
|
||||
this.chargeWeight = chargeWeight;
|
||||
}
|
||||
|
||||
public String getBillId()
|
||||
{
|
||||
return billId;
|
||||
}
|
||||
|
||||
public void setBillId(String billId)
|
||||
{
|
||||
this.billId = billId;
|
||||
}
|
||||
|
||||
public String getOrderNo()
|
||||
{
|
||||
return orderNo;
|
||||
}
|
||||
|
||||
public void setOrderNo(String orderNo)
|
||||
{
|
||||
this.orderNo = orderNo;
|
||||
}
|
||||
|
||||
public BigDecimal getChargeWeight()
|
||||
{
|
||||
return chargeWeight;
|
||||
}
|
||||
|
||||
public void setChargeWeight(BigDecimal chargeWeight)
|
||||
{
|
||||
this.chargeWeight = chargeWeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user