同步 关联关联问题

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