修改产品导入

This commit is contained in:
wuweihua
2026-09-29 20:50:01 +08:00
parent 3f8f3a824d
commit 38f371742a
10 changed files with 613 additions and 139 deletions
@@ -0,0 +1,92 @@
package com.bms.price.task;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.BmsProductImportTask;
import com.bms.price.service.IBmsProductService;
/**
* Async runner for product Excel import (avoids HTTP timeout on large workbooks).
*/
@Component
public class BmsProductImportAsyncRunner
{
private static final Logger log = LoggerFactory.getLogger(BmsProductImportAsyncRunner.class);
public static final String CACHE_KEY_PREFIX = "price:product:import:task:";
public static final int TTL_MINUTES = 60;
@Autowired
private IBmsProductService productService;
@Autowired
private RedisCache redisCache;
/** Background all-country (Weishi catalog) import. */
@Async("threadPoolTaskExecutor")
public void runImportAll(String taskId, byte[] bytes, String filename, boolean updateSupport, String operName)
{
try
{
put(taskId, BmsProductImportTask.STATUS_RUNNING, 5, "parsing excel", null);
String message = productService.importProductExcel(bytes, filename, updateSupport, operName, taskId);
put(taskId, BmsProductImportTask.STATUS_SUCCESS, 100, message, null);
log.info("async product import done, taskId={}", taskId);
}
catch (Exception e)
{
log.error("async product import failed, taskId={}", taskId, e);
put(taskId, BmsProductImportTask.STATUS_FAIL, 100, "failed", errorMessage(e));
}
}
/** Background single-country cost-sheet import. */
@Async("threadPoolTaskExecutor")
public void runImportCountry(String taskId, byte[] bytes, String filename, String countryCode, String operName)
{
try
{
put(taskId, BmsProductImportTask.STATUS_RUNNING, 5, "parsing excel", null);
String message = productService.importCountryProductExcel(bytes, filename, countryCode, operName, taskId);
put(taskId, BmsProductImportTask.STATUS_SUCCESS, 100, message, null);
log.info("async product country import done, taskId={}, country={}", taskId, countryCode);
}
catch (Exception e)
{
log.error("async product country import failed, taskId={}, country={}", taskId, countryCode, e);
put(taskId, BmsProductImportTask.STATUS_FAIL, 100, "failed", errorMessage(e));
}
}
public void put(String taskId, String status, int percent, String message, String error)
{
BmsProductImportTask task = new BmsProductImportTask();
task.setTaskId(taskId);
task.setStatus(status);
task.setPercent(percent);
task.setMessage(message);
task.setError(error);
redisCache.setCacheObject(CACHE_KEY_PREFIX + taskId, task, TTL_MINUTES, TimeUnit.MINUTES);
}
public BmsProductImportTask 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;
}
}