diff --git a/cash-service/product-service/src/main/java/com/czg/service/product/service/impl/ShopSyncServiceImpl.java b/cash-service/product-service/src/main/java/com/czg/service/product/service/impl/ShopSyncServiceImpl.java index 982a657a..0bd823fb 100644 --- a/cash-service/product-service/src/main/java/com/czg/service/product/service/impl/ShopSyncServiceImpl.java +++ b/cash-service/product-service/src/main/java/com/czg/service/product/service/impl/ShopSyncServiceImpl.java @@ -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 unitMap; - Map specMap; - Map categoryMap; - Map proMap = new HashMap<>(); - Map skuMap = new HashMap<>(); - checkShopInfo(sourceShopId, targetShopId); - List> mainMapList = new ArrayList<>(); - //商品 - try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { - CompletableFuture> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId, mainMapList), executor); - CompletableFuture> futureSpec = CompletableFuture.supplyAsync(() -> syncSpec(sourceShopId, targetShopId, mainMapList), executor); - CompletableFuture> futureCategory = CompletableFuture.supplyAsync(() -> syncCategory(sourceShopId, targetShopId, mainMapList), executor); + try { + Map unitMap; + Map specMap; + Map categoryMap; + Map proMap = new HashMap<>(); + Map skuMap = new HashMap<>(); + checkShopInfo(sourceShopId, targetShopId); + List> mainMapList = new ArrayList<>(); + //商品 + try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { + CompletableFuture> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId, mainMapList), executor); + CompletableFuture> futureSpec = CompletableFuture.supplyAsync(() -> syncSpec(sourceShopId, targetShopId, mainMapList), executor); + CompletableFuture> futureCategory = CompletableFuture.supplyAsync(() -> syncCategory(sourceShopId, targetShopId, mainMapList), executor); - CompletableFuture allFutures = CompletableFuture.allOf(futureUnit, futureSpec, futureCategory); - allFutures.join(); + CompletableFuture 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 conGroupMap = syncConsGroup(sourceShopId, targetShopId, mainMapList); + Map 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 conGroupMap = syncConsGroup(sourceShopId, targetShopId, mainMapList); - Map 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 oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list(); - if (CollUtil.isEmpty(oldProdGroup)) { - continue; - } - Set oldProds = oldProdGroup.stream().map(ProdGroupRelation::getProductId).collect(Collectors.toSet()); - List 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 prods = productService.queryChain().select(Product::getId).eq(Product::getShopId, shopId).in(Product::getSyncId, groupRelationMap.keySet()).list(); + List oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list(); + if (CollUtil.isEmpty(oldProdGroup)) { + continue; + } + Set oldProds = oldProdGroup.stream().map(ProdGroupRelation::getProductId).collect(Collectors.toSet()); + List 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 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 newCons = new ArrayList<>(); if (CollUtil.isEmpty(list)) { - List prods = prodConsRelationService.queryChain().select(ProdConsRelation::getProductId) - .eq(ProdConsRelation::getShopId, shopId).eq(ProdConsRelation::getProductId, product.getId()).listAs(Long.class); - if (CollUtil.isNotEmpty(prods)) { - List 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 map = list.stream().collect(Collectors.toMap(ProdConsRelation::getConsInfoId, ProdConsRelation::getSurplusStock)); - List 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); } } }