同步 关联关联问题

This commit is contained in:
2025-04-12 16:51:24 +08:00
parent eb885016f5
commit 6838854209

View File

@@ -1125,51 +1125,57 @@ public class ShopSyncServiceImpl implements ShopSyncService {
@Override
@Transactional(rollbackFor = Exception.class)
public void syncConsProBySourceShop(Long sourceShopId, Long sourceProdId, Long sysUserId) {
assertAutoSync(sourceShopId);
AssertUtil.isNull(sourceProdId, "{}不能为空", "商品ID");
Set<Long> shopIds = checkSourceShopInfo(sourceShopId, 2);
if (CollUtil.isEmpty(shopIds)) {
return;
}
List<ProdConsRelation> newSaveList = new ArrayList<>();
List<ProdConsRelation> list = prodConsRelationService.queryChain()
.eq(ProdConsRelation::getShopId, sourceShopId).eq(ProdConsRelation::getProductId, sourceProdId).list();
for (Long shopId : shopIds) {
Product product = productService.queryChain().eq(Product::getSyncId, sourceProdId).eq(Product::getShopId, shopId).one();
if (product != null) {
List<ConsInfo> newCons = new ArrayList<>();
if (CollUtil.isEmpty(list)) {
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<Long, BigDecimal> map = list.stream().collect(Collectors.toMap(ProdConsRelation::getConsInfoId, ProdConsRelation::getSurplusStock));
newCons = consInfoService.queryChain()
.in(ConsInfo::getSyncId, map.keySet()).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())));
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);
try {
log.info("同步商品耗材关系开始");
assertAutoSync(sourceShopId);
AssertUtil.isNull(sourceProdId, "{}不能为空", "商品ID");
Set<Long> shopIds = checkSourceShopInfo(sourceShopId, 2);
if (CollUtil.isEmpty(shopIds)) {
return;
}
List<ProdConsRelation> newSaveList = new ArrayList<>();
List<ProdConsRelation> list = prodConsRelationService.queryChain()
.eq(ProdConsRelation::getShopId, sourceShopId).eq(ProdConsRelation::getProductId, sourceProdId).list();
for (Long shopId : shopIds) {
Product product = productService.queryChain().eq(Product::getSyncId, sourceProdId).eq(Product::getShopId, shopId).one();
if (product != null) {
List<ConsInfo> newCons = new ArrayList<>();
if (CollUtil.isEmpty(list)) {
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<Long, BigDecimal> map = list.stream().collect(Collectors.toMap(ProdConsRelation::getConsInfoId, ProdConsRelation::getSurplusStock));
newCons = consInfoService.queryChain()
.in(ConsInfo::getSyncId, map.keySet()).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())));
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.saveBatch(newSaveList, 100);
if (CollUtil.isNotEmpty(newSaveList)) {
prodConsRelationService.saveBatch(newSaveList, 100);
}
log.info("同步商品耗材关系结束");
} catch (Exception e) {
log.error("同步商品耗材关系异常", e);
}
}