|
|
@@ -56,9 +56,10 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
private static final String SYNC_TYPE_DEVICE = "DEVICE";
|
|
|
private static final String SYNC_TYPE_PRODUCT = "PRODUCT";
|
|
|
|
|
|
+ @Async("taskExecutor")
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public SyncRecord syncDevices(Long operatorId, String operatorName) {
|
|
|
+ public void syncDevices(Long operatorId, String operatorName) {
|
|
|
log.info("开始同步设备数据,操作人: {}", operatorName);
|
|
|
|
|
|
// 1. 创建同步记录
|
|
|
@@ -129,8 +130,6 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
record.setErrorMessage("同步异常: " + e.getMessage());
|
|
|
updateById(record);
|
|
|
}
|
|
|
-
|
|
|
- return record;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -251,9 +250,10 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
return stats;
|
|
|
}
|
|
|
|
|
|
+ @Async("taskExecutor")
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public SyncRecord syncProducts(Long operatorId, String operatorName) {
|
|
|
+ public void syncProducts(Long operatorId, String operatorName) {
|
|
|
log.info("开始同步商品数据,操作人: {}", operatorName);
|
|
|
|
|
|
// 1. 创建同步记录
|
|
|
@@ -272,7 +272,9 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
int successCount = 0;
|
|
|
int failCount = 0;
|
|
|
int totalCount = 0;
|
|
|
+ int deletedCount = 0;
|
|
|
StringBuilder errorMsg = new StringBuilder();
|
|
|
+ java.util.Set<String> hahaCodes = new java.util.HashSet<>();
|
|
|
|
|
|
try {
|
|
|
// 2. 分页从哈哈平台商家商品库获取商品列表
|
|
|
@@ -283,7 +285,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
while (hasMore) {
|
|
|
// 调用哈哈平台API获取商家商品库列表
|
|
|
Map<String, Object> result = hahaClient.getGoodsApi().getMerchantList(pageNo, pageSize, null, null, null, null);
|
|
|
-
|
|
|
+
|
|
|
if (result == null || !result.containsKey("listData")) {
|
|
|
log.warn("获取商品列表返回数据为空");
|
|
|
break;
|
|
|
@@ -298,19 +300,22 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
// 3. 遍历商品列表,同步到本地数据库
|
|
|
for (Map<String, Object> productData : listData) {
|
|
|
totalCount++;
|
|
|
+ String code = productData.get("code") != null ? productData.get("code").toString() : null;
|
|
|
+ if (code != null) {
|
|
|
+ hahaCodes.add(code);
|
|
|
+ }
|
|
|
try {
|
|
|
syncSingleProduct(record.getId(), productData);
|
|
|
successCount++;
|
|
|
} catch (Exception e) {
|
|
|
failCount++;
|
|
|
- String code = productData.get("code") != null ? productData.get("code").toString() : "unknown";
|
|
|
String name = productData.get("name") != null ? productData.get("name").toString() : "unknown";
|
|
|
- String errMsg = String.format("商品 %s(%s) 同步失败: %s", code, name, e.getMessage());
|
|
|
+ String errMsg = String.format("商品 %s(%s) 同步失败: %s", code != null ? code : "unknown", name, e.getMessage());
|
|
|
errorMsg.append(errMsg).append("; ");
|
|
|
log.error(errMsg, e);
|
|
|
|
|
|
// 记录失败日志
|
|
|
- saveSyncLog(record.getId(), SYNC_TYPE_PRODUCT, code, name, 0, errMsg, JSON.toJSONString(productData));
|
|
|
+ saveSyncLog(record.getId(), SYNC_TYPE_PRODUCT, code != null ? code : "unknown", name, 0, errMsg, JSON.toJSONString(productData));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -324,7 +329,30 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 4. 更新同步记录
|
|
|
+ // 4. 清理哈哈中已删除的商品:标记本地已同步但哈哈中不存在的商品为删除
|
|
|
+ if (totalCount > 0 && !hahaCodes.isEmpty()) {
|
|
|
+ List<Product> orphanProducts = productService.lambdaQuery()
|
|
|
+ .eq(Product::getIsDeleted, CommonConstants.NOT_DELETED)
|
|
|
+ .eq(Product::getSyncStatus, SyncConstants.STATUS_SYNCED)
|
|
|
+ .notIn(Product::getCode, hahaCodes)
|
|
|
+ .list();
|
|
|
+
|
|
|
+ if (!orphanProducts.isEmpty()) {
|
|
|
+ deletedCount = orphanProducts.size();
|
|
|
+ List<Long> orphanIds = orphanProducts.stream().map(Product::getId).toList();
|
|
|
+ productService.lambdaUpdate()
|
|
|
+ .set(Product::getIsDeleted, CommonConstants.DELETED)
|
|
|
+ .set(Product::getUpdateTime, LocalDateTime.now())
|
|
|
+ .in(Product::getId, orphanIds)
|
|
|
+ .update();
|
|
|
+
|
|
|
+ log.info("标记已删除商品: {} 条, codes={}",
|
|
|
+ deletedCount,
|
|
|
+ orphanProducts.stream().map(Product::getCode).limit(20).toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 更新同步记录
|
|
|
record.setTotalCount(totalCount);
|
|
|
record.setSuccessCount(successCount);
|
|
|
record.setFailCount(failCount);
|
|
|
@@ -336,7 +364,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
}
|
|
|
updateById(record);
|
|
|
|
|
|
- log.info("商品数据同步完成,总数: {}, 成功: {}, 失败: {}", totalCount, successCount, failCount);
|
|
|
+ log.info("商品数据同步完成,总数: {}, 成功: {}, 失败: {}, 标记删除: {}", totalCount, successCount, failCount, deletedCount);
|
|
|
|
|
|
} catch (HahaException e) {
|
|
|
log.error("调用哈哈平台API失败", e);
|
|
|
@@ -353,8 +381,6 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
record.setErrorMessage("同步异常: " + e.getMessage());
|
|
|
updateById(record);
|
|
|
}
|
|
|
-
|
|
|
- return record;
|
|
|
}
|
|
|
|
|
|
/**
|