商品同步问题

This commit is contained in:
wangw 2025-04-12 13:45:09 +08:00
parent ad803c859e
commit d06c6b8bcc
1 changed files with 80 additions and 74 deletions

View File

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