Commit eaa1dd00 by zhouhaibao

refactor(StoreExcelImportService): 优化商品类目查询逻辑

- 将商品类目查询改为缓存模式,避免重复调用远程接口
- 新增 goodsCategoryCache 成员变量存储商品类目缓存
- 在初始化商户缓存时同时查询并缓存商品类目数据
- 修改验证产品分类方法使用缓存数据替代实时查询
- 优化 validateCategorySceneRelation 方法使用缓存参数
- 添加异常处理确保缓存初始化失败时有默认值
1 parent 8b4553b4
......@@ -100,19 +100,41 @@ public class StoreExcelImportService {
private Integer otherCostPerMonthColumnIndex = null;
private Integer personnelCostColumnIndex = null;
private volatile List<GoodsCategoryDTO> goodsCategoryCache = null;
private void initMerchantCache() {
synchronized (this) {
log.info("开始初始化商户缓存...");
parkingStoreCache.clear();
qyStoreCache.clear();
serviceProviderCache.clear();
goodsCategoryCache = null;
queryParkingStoreList();
queryQyStoreList();
queryServiceProviderList();
queryGoodsCategoryList();
log.info("商户缓存初始化完成,园区店数量: {}, 服务商数量: {}", parkingStoreCache.size(), serviceProviderCache.size());
}
}
private void queryGoodsCategoryList() {
try {
log.info("开始查询商品类目...");
RpcResponse<List<GoodsCategoryDTO>> response = goodsCategoryConfigurationRemote.queryAll();
if (response != null && response.isSuccess() && response.getResult() != null) {
goodsCategoryCache = response.getResult();
log.info("查询到商品类目数量: {}", goodsCategoryCache.size());
} else {
goodsCategoryCache = new ArrayList<>();
log.warn("商品类目查询失败,返回数据为空");
}
} catch (Exception e) {
goodsCategoryCache = new ArrayList<>();
log.error("商品类目查询异常:{}", e.getMessage(), e);
}
}
private Map<String, String> findMerchantList(List<String> collect) {
Map<String, String> merchantNoMap = new HashMap<>();
try {
......@@ -896,24 +918,15 @@ public class StoreExcelImportService {
private void validateProductCategoryExists(StoreWithProductsDTO.ProductCategoryRuleDTO ruleDTO, Integer rowNum,
List<ImportErrorInfo> errors, String baseSceneCode) {
List<GoodsCategoryDTO> goodsCategoryList = new ArrayList<>();
try {
RpcResponse<List<GoodsCategoryDTO>> response = goodsCategoryConfigurationRemote.queryAll();
if (response.isSuccess() && response.getResult() != null) {
goodsCategoryList = response.getResult();
}
} catch (Exception e) {
log.error("validateProductCategoryExists查询商品类目失败:{}", e.getMessage(), e);
}
if (goodsCategoryList.isEmpty()) {
if (goodsCategoryCache.isEmpty()) {
errors.add(new ImportErrorInfo(rowNum, "产品分类无法识别", ruleDTO));
return;
}
String categoryName = ruleDTO.getProductCategory().trim();
GoodsCategoryDTO matchedCategory = matchCategoryByHierarchy(categoryName, goodsCategoryList);
GoodsCategoryDTO matchedCategory = matchCategoryByHierarchy(categoryName, goodsCategoryCache);
if (matchedCategory == null) {
errors.add(new ImportErrorInfo(rowNum, "产品分类[" + categoryName + "]无法识别", ruleDTO));
......@@ -922,7 +935,7 @@ public class StoreExcelImportService {
ruleDTO.setProductCategoryId(matchedCategory.getCategoryType());
boolean isValid = validateCategorySceneRelation(matchedCategory, goodsCategoryList, baseSceneCode);
boolean isValid = validateCategorySceneRelation(matchedCategory, goodsCategoryCache, baseSceneCode);
if (!isValid) {
errors.add(new ImportErrorInfo(rowNum, "产品分类[" + categoryName + "]不支持上架至当前场景", ruleDTO));
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!