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 ca2b980e0..d7783a1d9 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 @@ -467,86 +467,92 @@ public class ShopSyncServiceImpl implements ShopSyncService { @Override @Transactional(rollbackFor = Exception.class) public void syncProductBySourceShop(Long sourceShopId, Long productId, Long sysUserId) { - assertAutoSync(sourceShopId); - AssertUtil.isNull(productId, "{}不能为空", "商品ID"); - Set shopIds = checkSourceShopInfo(sourceShopId, 1); - if (CollUtil.isEmpty(shopIds)) { - return; - } - Product mainEntity = productService.getById(productId); - AssertUtil.isNull(mainEntity, "需要同步的{}不存在", "商品"); - List list = productService.queryChain().eq(Product::getSyncId, productId).in(Product::getShopId, shopIds).list(); - Map map = new HashMap<>(); - if (CollUtil.isNotEmpty(list)) { - map = list.stream().collect(Collectors.toMap(Product::getShopId, t -> t)); - } - Map specMap = new HashMap<>(); - if (mainEntity.getSpecId() != null) { - List specList = specService.queryChain().eq(ShopProdSpec::getSyncId, mainEntity.getSpecId()) - .in(ShopProdSpec::getShopId, shopIds).list(); - if (CollUtil.isNotEmpty(specList)) { - specMap = specList.stream().collect(Collectors.toMap(ShopProdSpec::getShopId, ShopProdSpec::getId)); + try { + log.info("商品同步开始,源店铺ID:{},商品ID:{}", sourceShopId, productId); + assertAutoSync(sourceShopId); + AssertUtil.isNull(productId, "{}不能为空", "商品ID"); + Set shopIds = checkSourceShopInfo(sourceShopId, 1); + if (CollUtil.isEmpty(shopIds)) { + return; } - } - Map unitMap = new HashMap<>(); - Set sysUnitMap = new HashSet<>(); - if (mainEntity.getUnitId() != null) { - List unitList = unitService.queryChain().eq(ShopProdUnit::getSyncId, mainEntity.getUnitId()) - .in(ShopProdUnit::getShopId, shopIds).list(); - if (CollUtil.isNotEmpty(unitList)) { - unitMap = unitList.stream().collect(Collectors.toMap(ShopProdUnit::getShopId, ShopProdUnit::getId)); + Product mainEntity = productService.getById(productId); + AssertUtil.isNull(mainEntity, "需要同步的{}不存在", "商品"); + List list = productService.queryChain().eq(Product::getSyncId, productId).in(Product::getShopId, shopIds).list(); + Map map = new HashMap<>(); + if (CollUtil.isNotEmpty(list)) { + map = list.stream().collect(Collectors.toMap(Product::getShopId, t -> t)); } - for (ShopProdUnit shopProdUnit : unitService.queryChain().eq(ShopProdUnit::getIsSystem, 1).list()) { - sysUnitMap.add(shopProdUnit.getId()); - } - } - Map categoryMap = new HashMap<>(); - if (mainEntity.getCategoryId() != null) { - List categoryList = categoryService.queryChain().eq(ShopProdCategory::getSyncId, mainEntity.getCategoryId()).in(ShopProdCategory::getShopId, shopIds).list(); - if (CollUtil.isNotEmpty(categoryList)) { - categoryMap = categoryList.stream().collect(Collectors.toMap(ShopProdCategory::getShopId, ShopProdCategory::getId)); - } - } - Map finalMap = map; - for (Long shopId : shopIds) { - List> mainMapList = new ArrayList<>(); - Map dataMap = new HashMap<>(); - Product newEntity = BeanUtil.copyProperties(mainEntity, Product.class); - newEntity.setSyncId(mainEntity.getSyncId()); - newEntity.setShopId(shopId); - newEntity.setCategoryId(newEntity.getCategoryId() != null ? categoryMap.get(shopId) : null); - newEntity.setSpecId(newEntity.getSpecId() != null ? specMap.get(shopId) : null); - if (newEntity.getUnitId() != null) { - if (!sysUnitMap.contains(newEntity.getUnitId())) { - newEntity.setUnitId(unitMap.get(shopId)); + Map specMap = new HashMap<>(); + if (mainEntity.getSpecId() != null) { + List specList = specService.queryChain().eq(ShopProdSpec::getSyncId, mainEntity.getSpecId()) + .in(ShopProdSpec::getShopId, shopIds).list(); + if (CollUtil.isNotEmpty(specList)) { + specMap = specList.stream().collect(Collectors.toMap(ShopProdSpec::getShopId, ShopProdSpec::getId)); } } - if (finalMap.containsKey(shopId)) { - Product childEntity = finalMap.get(shopId); - newEntity.setId(childEntity.getId()); - newEntity.setStartTime(null); - newEntity.setEndTime(null); - newEntity.setDays(null); - newEntity.setIsHot(null); - newEntity.setIsStock(null); - newEntity.setIsSoldStock(null); - newEntity.setStockNumber(null); - newEntity.setIsSale(null); - newEntity.setIsRefundStock(null); - productService.updateById(newEntity); - } else { - newEntity.setId(null); - newEntity.setIsSale(0); - newEntity.setStockNumber(0); - productService.save(newEntity); + Map unitMap = new HashMap<>(); + Set sysUnitMap = new HashSet<>(); + if (mainEntity.getUnitId() != null) { + List unitList = unitService.queryChain().eq(ShopProdUnit::getSyncId, mainEntity.getUnitId()) + .in(ShopProdUnit::getShopId, shopIds).list(); + if (CollUtil.isNotEmpty(unitList)) { + unitMap = unitList.stream().collect(Collectors.toMap(ShopProdUnit::getShopId, ShopProdUnit::getId)); + } + for (ShopProdUnit shopProdUnit : unitService.queryChain().eq(ShopProdUnit::getIsSystem, 1).list()) { + sysUnitMap.add(shopProdUnit.getId()); + } } - syncSkuBySourceShop(sourceShopId, shopId, productId, newEntity.getId()); - if ("package".equals(newEntity.getType()) || "coupon".equals(newEntity.getType())) { - syncProductPackageBySourceShop(newEntity, shopId); + Map categoryMap = new HashMap<>(); + if (mainEntity.getCategoryId() != null) { + List categoryList = categoryService.queryChain().eq(ShopProdCategory::getSyncId, mainEntity.getCategoryId()).in(ShopProdCategory::getShopId, shopIds).list(); + if (CollUtil.isNotEmpty(categoryList)) { + categoryMap = categoryList.stream().collect(Collectors.toMap(ShopProdCategory::getShopId, ShopProdCategory::getId)); + } } - dataMap.put(newEntity.getId(), newEntity.getName()); - buildNotice(mainMapList, "商品变动", 1, "product", dataMap); - syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity)); + Map finalMap = map; + for (Long shopId : shopIds) { + List> mainMapList = new ArrayList<>(); + Map dataMap = new HashMap<>(); + Product newEntity = BeanUtil.copyProperties(mainEntity, Product.class); + newEntity.setSyncId(mainEntity.getSyncId()); + newEntity.setShopId(shopId); + newEntity.setCategoryId(newEntity.getCategoryId() != null ? categoryMap.get(shopId) : null); + newEntity.setSpecId(newEntity.getSpecId() != null ? specMap.get(shopId) : null); + if (newEntity.getUnitId() != null) { + if (!sysUnitMap.contains(newEntity.getUnitId())) { + newEntity.setUnitId(unitMap.get(shopId)); + } + } + if (finalMap.containsKey(shopId)) { + Product childEntity = finalMap.get(shopId); + newEntity.setId(childEntity.getId()); + newEntity.setStartTime(null); + newEntity.setEndTime(null); + newEntity.setDays(null); + newEntity.setIsHot(null); + newEntity.setIsStock(null); + newEntity.setIsSoldStock(null); + newEntity.setStockNumber(null); + newEntity.setIsSale(null); + newEntity.setIsRefundStock(null); + productService.updateById(newEntity); + } else { + newEntity.setId(null); + newEntity.setIsSale(0); + newEntity.setStockNumber(0); + productService.save(newEntity); + } + syncSkuBySourceShop(sourceShopId, shopId, productId, newEntity.getId()); + if ("package".equals(newEntity.getType()) || "coupon".equals(newEntity.getType())) { + syncProductPackageBySourceShop(newEntity, shopId); + } + dataMap.put(newEntity.getId(), newEntity.getName()); + buildNotice(mainMapList, "商品变动", 1, "product", dataMap); + syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity)); + } + } catch (Exception e) { + log.error("商品同步异常", e); + throw new CzgException("商品同步失败"); } }