同步问题

This commit is contained in:
2025-04-11 09:34:36 +08:00
parent 82d5b05e5e
commit 44fc50b809

View File

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