同步问题

This commit is contained in:
wangw 2025-04-11 09:34:36 +08:00
parent 82d5b05e5e
commit 44fc50b809
1 changed files with 74 additions and 62 deletions

View File

@ -118,40 +118,45 @@ public class ShopSyncServiceImpl implements ShopSyncService {
@Transactional(rollbackFor = Exception.class)
public void sync(Long sourceShopId, Long targetShopId, Long sysUserId) {
log.info("同步开始,源店铺ID:{},目标店铺ID:{}", sourceShopId, targetShopId);
Map<Long, Long> unitMap;
Map<Long, Long> specMap;
Map<Long, Long> categoryMap;
Map<Long, Long> proMap = new HashMap<>();
Map<Long, Long> skuMap = new HashMap<>();
checkShopInfo(sourceShopId, targetShopId);
List<Map<String, Object>> mainMapList = new ArrayList<>();
//商品
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<Map<Long, Long>> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId, mainMapList), executor);
CompletableFuture<Map<Long, Long>> futureSpec = CompletableFuture.supplyAsync(() -> syncSpec(sourceShopId, targetShopId, mainMapList), executor);
CompletableFuture<Map<Long, Long>> futureCategory = CompletableFuture.supplyAsync(() -> syncCategory(sourceShopId, targetShopId, mainMapList), executor);
try {
Map<Long, Long> unitMap;
Map<Long, Long> specMap;
Map<Long, Long> categoryMap;
Map<Long, Long> proMap = new HashMap<>();
Map<Long, Long> skuMap = new HashMap<>();
checkShopInfo(sourceShopId, targetShopId);
List<Map<String, Object>> mainMapList = new ArrayList<>();
//商品
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<Map<Long, Long>> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId, mainMapList), executor);
CompletableFuture<Map<Long, Long>> futureSpec = CompletableFuture.supplyAsync(() -> syncSpec(sourceShopId, targetShopId, mainMapList), executor);
CompletableFuture<Map<Long, Long>> futureCategory = CompletableFuture.supplyAsync(() -> syncCategory(sourceShopId, targetShopId, mainMapList), executor);
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futureUnit, futureSpec, futureCategory);
allFutures.join();
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futureUnit, futureSpec, futureCategory);
allFutures.join();
unitMap = futureUnit.join();
specMap = futureSpec.join();
categoryMap = futureCategory.join();
unitMap = futureUnit.join();
specMap = futureSpec.join();
categoryMap = futureCategory.join();
} catch (Exception e) {
log.error("同步异常", e);
throw new CzgException("同步失败");
}
proMap = syncProduct(sourceShopId, targetShopId, unitMap, specMap, categoryMap, mainMapList);
skuMap = syncSku(sourceShopId, targetShopId, proMap);
syncGroup(sourceShopId, targetShopId, proMap, mainMapList);
syncProductPackage(targetShopId, proMap, skuMap);
//耗材
Map<Long, Long> conGroupMap = syncConsGroup(sourceShopId, targetShopId, mainMapList);
Map<Long, Long> consInfoMap = syncConsInfo(sourceShopId, targetShopId, conGroupMap, mainMapList);
syncConsPro(sourceShopId, targetShopId, consInfoMap, proMap);
syncNoticeService.addNotice(targetShopId, sysUserId, "数据同步", JSON.toJSONString(mainMapList), null);
} catch (Exception e) {
log.error("同步异常", e);
throw new CzgException("同步失败");
}
proMap = syncProduct(sourceShopId, targetShopId, unitMap, specMap, categoryMap, mainMapList);
skuMap = syncSku(sourceShopId, targetShopId, proMap);
syncGroup(sourceShopId, targetShopId, proMap, mainMapList);
syncProductPackage(targetShopId, proMap, skuMap);
//耗材
Map<Long, Long> conGroupMap = syncConsGroup(sourceShopId, targetShopId, mainMapList);
Map<Long, Long> consInfoMap = syncConsInfo(sourceShopId, targetShopId, conGroupMap, mainMapList);
syncConsPro(sourceShopId, targetShopId, consInfoMap, proMap);
syncNoticeService.addNotice(targetShopId, sysUserId, "数据同步", JSON.toJSONString(mainMapList), null);
log.info("同步结束,源店铺ID:{},目标店铺ID:{}", sourceShopId, targetShopId);
}
@ -581,23 +586,23 @@ public class ShopSyncServiceImpl implements ShopSyncService {
dataMap.put(newEntity.getId(), newEntity.getName());
buildNotice(mainMapList, "分组变动", 1, "group", dataMap);
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
if (CollUtil.isEmpty(groupRelationMap)) {
List<ProdGroupRelation> oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list();
if (CollUtil.isEmpty(oldProdGroup)) {
continue;
}
Set<Long> oldProds = oldProdGroup.stream().map(ProdGroupRelation::getProductId).collect(Collectors.toSet());
List<Long> rmIds = productService.queryChain().select(Product::getId).eq(Product::getShopId, shopId)
.in(Product::getId, oldProds).isNotNull(Product::getSyncId).listAs(Long.class);
if (CollUtil.isEmpty(rmIds)) {
continue;
}
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq(ProdGroupRelation::getProdGroupId, newEntity.getId());
queryWrapper.in(ProdGroupRelation::getProductId, rmIds);
prodGroupRelationService.remove(queryWrapper);
} else {
List<Product> prods = productService.queryChain().select(Product::getId).eq(Product::getShopId, shopId).in(Product::getSyncId, groupRelationMap.keySet()).list();
List<ProdGroupRelation> oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list();
if (CollUtil.isEmpty(oldProdGroup)) {
continue;
}
Set<Long> oldProds = oldProdGroup.stream().map(ProdGroupRelation::getProductId).collect(Collectors.toSet());
List<Long> rmIds = productService.queryChain().select(Product::getId).eq(Product::getShopId, shopId)
.in(Product::getId, oldProds).isNotNull(Product::getSyncId).listAs(Long.class);
if (CollUtil.isEmpty(rmIds)) {
continue;
}
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq(ProdGroupRelation::getProdGroupId, newEntity.getId());
queryWrapper.in(ProdGroupRelation::getProductId, rmIds);
prodGroupRelationService.remove(queryWrapper);
if (CollUtil.isNotEmpty(groupRelationMap)) {
List<Product> prods = productService.queryChain().eq(Product::getShopId, shopId).in(Product::getSyncId, groupRelationMap.keySet()).list();
for (Product prod : prods) {
ProdGroupRelation newProdGroupRelation = new ProdGroupRelation();
newProdGroupRelation.setProdGroupId(newEntity.getId());
@ -608,7 +613,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
}
}
if (CollUtil.isNotEmpty(groupRelations)) {
prodGroupRelationService.saveOrUpdateBatch(groupRelationList, 100);
prodGroupRelationService.saveBatch(groupRelationList, 100);
}
}
@ -885,32 +890,39 @@ public class ShopSyncServiceImpl implements ShopSyncService {
for (Long shopId : shopIds) {
Product product = productService.queryChain().eq(Product::getSyncId, sourceProdId).eq(Product::getShopId, shopId).one();
if (product != null) {
List<ConsInfo> newCons = new ArrayList<>();
if (CollUtil.isEmpty(list)) {
List<Long> prods = prodConsRelationService.queryChain().select(ProdConsRelation::getProductId)
.eq(ProdConsRelation::getShopId, shopId).eq(ProdConsRelation::getProductId, product.getId()).listAs(Long.class);
if (CollUtil.isNotEmpty(prods)) {
List<Long> remIds = productService.queryChain().select(Product::getId).in(Product::getId, prods).isNotNull(Product::getSyncId).listAs(Long.class);
if (CollUtil.isNotEmpty(remIds)) {
prodConsRelationService.remove(new QueryWrapper().eq(ProdConsRelation::getShopId, shopId).in(ProdConsRelation::getProductId, remIds));
}
newCons = consInfoService.queryChain()
.isNotNull(ConsInfo::getSyncId).eq(ConsInfo::getShopId, shopId).list();
if (CollUtil.isNotEmpty(newCons)) {
prodConsRelationService.remove(QueryWrapper.create()
.eq(ProdConsRelation::getShopId, shopId)
.eq(ProdConsRelation::getProductId, product.getId())
.in(ProdConsRelation::getConsInfoId, newCons.stream().map(ConsInfo::getId).collect(Collectors.toList())));
}
} else {
Map<Long, BigDecimal> map = list.stream().collect(Collectors.toMap(ProdConsRelation::getConsInfoId, ProdConsRelation::getSurplusStock));
List<ConsInfo> newCons = consInfoService.queryChain().select(ConsInfo::getId)
newCons = consInfoService.queryChain()
.in(ConsInfo::getSyncId, map.keySet()).eq(ConsInfo::getShopId, shopId).list();
for (ConsInfo newCon : newCons) {
ProdConsRelation prodConsRelation = new ProdConsRelation();
prodConsRelation.setShopId(shopId);
prodConsRelation.setProductId(product.getId());
prodConsRelation.setConsInfoId(newCon.getId());
prodConsRelation.setSurplusStock(map.get(newCon.getSyncId()));
newSaveList.add(prodConsRelation);
if (CollUtil.isNotEmpty(newCons)) {
prodConsRelationService.remove(QueryWrapper.create()
.eq(ProdConsRelation::getShopId, shopId)
.eq(ProdConsRelation::getProductId, product.getId())
.in(ProdConsRelation::getConsInfoId, newCons.stream().map(ConsInfo::getId).collect(Collectors.toList())));
for (ConsInfo newCon : newCons) {
ProdConsRelation prodConsRelation = new ProdConsRelation();
prodConsRelation.setShopId(shopId);
prodConsRelation.setProductId(product.getId());
prodConsRelation.setConsInfoId(newCon.getId());
prodConsRelation.setSurplusStock(map.get(newCon.getSyncId()));
newSaveList.add(prodConsRelation);
}
}
}
}
}
if (CollUtil.isNotEmpty(newSaveList)) {
prodConsRelationService.saveOrUpdateBatch(newSaveList, 100);
prodConsRelationService.saveBatch(newSaveList, 100);
}
}
}