商品同步问题

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
@Transactional(rollbackFor = Exception.class)
public void syncProductBySourceShop(Long sourceShopId, Long productId, Long sysUserId) {
assertAutoSync(sourceShopId);
AssertUtil.isNull(productId, "{}不能为空", "商品ID");
Set<Long> shopIds = checkSourceShopInfo(sourceShopId, 1);
if (CollUtil.isEmpty(shopIds)) {
return;
}
Product mainEntity = productService.getById(productId);
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));
try {
log.info("商品同步开始,源店铺ID:{},商品ID:{}", sourceShopId, productId);
assertAutoSync(sourceShopId);
AssertUtil.isNull(productId, "{}不能为空", "商品ID");
Set<Long> shopIds = checkSourceShopInfo(sourceShopId, 1);
if (CollUtil.isEmpty(shopIds)) {
return;
}
}
Map<Long, Long> unitMap = new HashMap<>();
Set<Long> sysUnitMap = new HashSet<>();
if (mainEntity.getUnitId() != null) {
List<ShopProdUnit> 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<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));
}
for (ShopProdUnit shopProdUnit : unitService.queryChain().eq(ShopProdUnit::getIsSystem, 1).list()) {
sysUnitMap.add(shopProdUnit.getId());
}
}
Map<Long, Long> categoryMap = new HashMap<>();
if (mainEntity.getCategoryId() != null) {
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));
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));
}
}
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<Long, Long> unitMap = new HashMap<>();
Set<Long> sysUnitMap = new HashSet<>();
if (mainEntity.getUnitId() != null) {
List<ShopProdUnit> 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<Long, Long> categoryMap = new HashMap<>();
if (mainEntity.getCategoryId() != null) {
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());
buildNotice(mainMapList, "商品变动", 1, "product", dataMap);
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
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)) {
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("商品同步失败");
}
}