重复同步问题
This commit is contained in:
parent
60ca657f89
commit
e17a2ee5d1
|
|
@ -130,36 +130,29 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
List<Map<String, Object>> mainMapList = new ArrayList<>();
|
||||
//商品
|
||||
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
|
||||
CompletableFuture<Map<Long, Long>> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId), executor);
|
||||
CompletableFuture<Map<Long, Long>> futureSpec = CompletableFuture.supplyAsync(() -> syncSpec(sourceShopId, targetShopId), executor);
|
||||
CompletableFuture<Map<Long, Long>> futureCategory = CompletableFuture.supplyAsync(() -> syncCategory(sourceShopId, targetShopId), executor);
|
||||
CompletableFuture<Map<Long, Long>> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId, mainMapList), executor);
|
||||
CompletableFuture<Map<Long, Long>> futureSpec = CompletableFuture.supplyAsync(() -> syncSpec(sourceShopId, targetShopId, mainMapList), executor);
|
||||
CompletableFuture<Map<Long, Long>> futureCategory = CompletableFuture.supplyAsync(() -> syncCategory(sourceShopId, targetShopId, mainMapList), executor);
|
||||
|
||||
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futureUnit, futureSpec, futureCategory);
|
||||
allFutures.join();
|
||||
|
||||
unitMap = futureUnit.join();
|
||||
buildNotice(mainMapList, "单位同步", unitMap.size(), null);
|
||||
specMap = futureSpec.join();
|
||||
buildNotice(mainMapList, "规格同步", specMap.size(), null);
|
||||
categoryMap = futureCategory.join();
|
||||
buildNotice(mainMapList, "分类同步", categoryMap.size(), null);
|
||||
} catch (Exception e) {
|
||||
log.error("同步异常", e);
|
||||
throw new CzgException("同步失败");
|
||||
}
|
||||
proMap = syncProduct(sourceShopId, targetShopId, unitMap, specMap, categoryMap);
|
||||
buildNotice(mainMapList, "商品同步", proMap.size(), null);
|
||||
proMap = syncProduct(sourceShopId, targetShopId, unitMap, specMap, categoryMap, mainMapList);
|
||||
skuMap = syncSku(sourceShopId, targetShopId, proMap);
|
||||
Map<Long, Long> groupMap = syncGroup(sourceShopId, targetShopId, proMap);
|
||||
buildNotice(mainMapList, "分组同步", groupMap.size(), null);
|
||||
syncGroup(sourceShopId, targetShopId, proMap, mainMapList);
|
||||
syncProductPackage(targetShopId, proMap, skuMap);
|
||||
|
||||
|
||||
//耗材
|
||||
Map<Long, Long> conGroupMap = syncConsGroup(sourceShopId, targetShopId);
|
||||
buildNotice(mainMapList, "耗材分组同步", conGroupMap.size(), null);
|
||||
Map<Long, Long> consInfoMap = syncConsInfo(sourceShopId, targetShopId, conGroupMap);
|
||||
buildNotice(mainMapList, "耗材信息同步", consInfoMap.size(), null);
|
||||
Map<Long, Long> conGroupMap = syncConsGroup(sourceShopId, targetShopId, mainMapList);
|
||||
Map<Long, Long> consInfoMap = syncConsInfo(sourceShopId, targetShopId, conGroupMap, mainMapList);
|
||||
syncConsPro(sourceShopId, targetShopId, consInfoMap, proMap);
|
||||
syncNoticeService.addNotice(targetShopId, sysUserId, "数据同步", JSON.toJSONString(mainMapList), null);
|
||||
log.info("同步结束,源店铺ID:{},目标店铺ID:{}", sourceShopId, targetShopId);
|
||||
|
|
@ -174,10 +167,13 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
* @param size 变动数量
|
||||
* @param dataMap 变动数据集合<Id,名称>
|
||||
*/
|
||||
private void buildNotice(List<Map<String, Object>> mainMapList, String title, int size, Map<Long, String> dataMap) {
|
||||
private void buildNotice(List<Map<String, Object>> mainMapList, String title, int size, String type, Map<Long, String> dataMap) {
|
||||
Map<String, Object> mainJsonMap = new HashMap<>();
|
||||
mainJsonMap.put("title", title);
|
||||
mainJsonMap.put("number", size + "个");
|
||||
if (StrUtil.isNotBlank(type)) {
|
||||
mainJsonMap.put("type", type);
|
||||
}
|
||||
if (CollUtil.isNotEmpty(dataMap)) {
|
||||
List<Map<String, Object>> mapList = new ArrayList<>();
|
||||
dataMap.forEach((k, v) -> {
|
||||
|
|
@ -191,12 +187,17 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
mainMapList.add(mainJsonMap);
|
||||
}
|
||||
|
||||
public Map<Long, Long> syncUnit(Long sourceShopId, Long pointShopId) {
|
||||
public Map<Long, Long> syncUnit(Long sourceShopId, Long pointShopId, List<Map<String, Object>> mainMapList) {
|
||||
Map<Long, Long> unitMap = new HashMap<>();
|
||||
List<Long> pointShopUnits = unitService.queryChain().select(ShopProdUnit::getSyncId)
|
||||
List<Long> pointShopUnits = new ArrayList<>();
|
||||
List<ShopProdUnit> pointUnits = unitService.queryChain().select(ShopProdUnit::getSyncId)
|
||||
.eq(ShopProdUnit::getShopId, pointShopId)
|
||||
.isNotNull(ShopProdUnit::getSyncId)
|
||||
.listAs(Long.class);
|
||||
.list();
|
||||
pointUnits.stream().forEach(tbShopUnit -> {
|
||||
unitMap.put(tbShopUnit.getSyncId(), tbShopUnit.getId());
|
||||
pointShopUnits.add(tbShopUnit.getSyncId());
|
||||
});
|
||||
List<ShopProdUnit> list = unitService.queryChain().eq(ShopProdUnit::getShopId, sourceShopId).list();
|
||||
list.forEach(tbShopUnit -> {
|
||||
if (CollUtil.isEmpty(pointShopUnits) || !pointShopUnits.contains(tbShopUnit.getId())) {
|
||||
|
|
@ -208,7 +209,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
unitMap.put(tbShopUnit.getId(), unitNew.getId());
|
||||
}
|
||||
});
|
||||
log.info("单位同步,源{}个,已有{}个,同步{}个", list.size(), pointShopUnits.size(), unitMap.size());
|
||||
buildNotice(mainMapList, "单位同步", unitMap.size() - pointShopUnits.size(), null, null);
|
||||
log.info("单位同步,源{}个,已有{}个,同步{}个", list.size(), pointShopUnits.size(), unitMap.size() - pointShopUnits.size());
|
||||
return unitMap;
|
||||
}
|
||||
|
||||
|
|
@ -244,18 +246,23 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
unitService.save(newUnit);
|
||||
}
|
||||
unitDataMap.put(newUnit.getId(), newUnit.getName());
|
||||
buildNotice(mainMapList, "单位变动", 1, unitDataMap);
|
||||
buildNotice(mainMapList, "单位变动", 1, "unit", unitDataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newUnit));
|
||||
});
|
||||
}
|
||||
|
||||
//规格
|
||||
public Map<Long, Long> syncSpec(Long sourceShopId, Long pointShopId) {
|
||||
public Map<Long, Long> syncSpec(Long sourceShopId, Long pointShopId, List<Map<String, Object>> mainMapList) {
|
||||
Map<Long, Long> specMap = new HashMap<>();
|
||||
List<Long> pointShopSpecs = specService.queryChain().select(ShopProdSpec::getSyncId)
|
||||
List<Long> pointShopSpecs = new ArrayList<>();
|
||||
List<ShopProdSpec> pointList = specService.queryChain().select(ShopProdSpec::getSyncId)
|
||||
.eq(ShopProdSpec::getShopId, pointShopId)
|
||||
.isNotNull(ShopProdSpec::getSyncId)
|
||||
.listAs(Long.class);
|
||||
.list();
|
||||
for (ShopProdSpec shopProdSpec : pointList) {
|
||||
pointShopSpecs.add(shopProdSpec.getSyncId());
|
||||
specMap.put(shopProdSpec.getSyncId(), shopProdSpec.getId());
|
||||
}
|
||||
List<ShopProdSpec> list = specService.queryChain().eq(ShopProdSpec::getShopId, sourceShopId.toString()).list();
|
||||
list.forEach(spec -> {
|
||||
if (CollUtil.isEmpty(pointShopSpecs) || !pointShopSpecs.contains(spec.getId())) {
|
||||
|
|
@ -267,7 +274,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
specMap.put(spec.getId(), newSpec.getId());
|
||||
}
|
||||
});
|
||||
log.info("规格同步,源{}个,已有{}个,同步{}个", list.size(), pointShopSpecs.size(), specMap.size());
|
||||
log.info("规格同步,源{}个,已有{}个,同步{}个", list.size(), pointShopSpecs.size(), specMap.size() - pointShopSpecs.size());
|
||||
buildNotice(mainMapList, "规格同步", specMap.size() - pointShopSpecs.size(), null, null);
|
||||
return specMap;
|
||||
}
|
||||
|
||||
|
|
@ -303,18 +311,23 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
specService.save(newEntity);
|
||||
}
|
||||
dataMap.put(newEntity.getId(), newEntity.getName());
|
||||
buildNotice(mainMapList, "规格变动", 1, dataMap);
|
||||
buildNotice(mainMapList, "规格变动", 1, "spec", dataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
|
||||
});
|
||||
}
|
||||
|
||||
// 分类
|
||||
public Map<Long, Long> syncCategory(Long sourceShopId, Long pointShopId) {
|
||||
public Map<Long, Long> syncCategory(Long sourceShopId, Long pointShopId, List<Map<String, Object>> mainMapList) {
|
||||
Map<Long, Long> categoryMap = new HashMap<>();
|
||||
List<Long> pointShopCateGory = categoryService.queryChain().select(ShopProdCategory::getSyncId)
|
||||
List<Long> pointShopCateGory = new ArrayList<>();
|
||||
List<ShopProdCategory> pointList = categoryService.queryChain().select(ShopProdCategory::getSyncId)
|
||||
.eq(ShopProdCategory::getShopId, pointShopId)
|
||||
.isNotNull(ShopProdCategory::getSyncId)
|
||||
.listAs(Long.class);
|
||||
.list();
|
||||
for (ShopProdCategory shopProdCategory : pointList) {
|
||||
pointShopCateGory.add(shopProdCategory.getSyncId());
|
||||
categoryMap.put(shopProdCategory.getSyncId(), shopProdCategory.getId());
|
||||
}
|
||||
List<ShopProdCategory> tbShopCategories = categoryService.queryChain()
|
||||
.eq(ShopProdCategory::getShopId, sourceShopId)
|
||||
.eq(ShopProdCategory::getPid, 0)
|
||||
|
|
@ -353,6 +366,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
}
|
||||
log.info("分类同步,源 父类分类{}个,同步{}个", tbShopCategories.size(), syncSize);
|
||||
log.info("分类同步,源 子类分类{}个,同步{}个", childrenSize, categoryMap.size() - syncSize);
|
||||
buildNotice(mainMapList, "分类同步", categoryMap.size(), null, null);
|
||||
return categoryMap;
|
||||
}
|
||||
|
||||
|
|
@ -395,14 +409,15 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
categoryService.save(newEntity);
|
||||
}
|
||||
dataMap.put(newEntity.getId(), newEntity.getName());
|
||||
buildNotice(mainMapList, "分类变动", 1, dataMap);
|
||||
buildNotice(mainMapList, "分类变动", 1, "category", dataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long, Long> syncProduct(Long sourceShopId, Long pointShopId, Map<Long, Long> unitMap,
|
||||
Map<Long, Long> specMap, Map<Long, Long> cateGoryMap) {
|
||||
Map<Long, Long> specMap, Map<Long, Long> cateGoryMap, List<Map<String, Object>> mainMapList) {
|
||||
List<Long> pointProducts = productService.queryChain()
|
||||
.select(Product::getSyncId)
|
||||
.eq(Product::getShopId, pointShopId)
|
||||
.isNotNull(Product::getSyncId)
|
||||
.listAs(Long.class);
|
||||
|
|
@ -428,6 +443,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
}
|
||||
}
|
||||
log.info("商品同步,源{}个,已有{}个,同步{}个", products.size(), pointProducts.size(), proMap.size());
|
||||
buildNotice(mainMapList, "商品同步", proMap.size(), null, null);
|
||||
return proMap;
|
||||
}
|
||||
|
||||
|
|
@ -486,12 +502,12 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
syncProductPackageBySourceShop(newEntity, shopId);
|
||||
}
|
||||
dataMap.put(newEntity.getId(), newEntity.getName());
|
||||
buildNotice(mainMapList, "商品变动", 1, dataMap);
|
||||
buildNotice(mainMapList, "商品变动", 1, "product", dataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long, Long> syncGroup(Long sourceShopId, Long pointShopId, Map<Long, Long> pros) {
|
||||
public Map<Long, Long> syncGroup(Long sourceShopId, Long pointShopId, Map<Long, Long> pros, List<Map<String, Object>> mainMapList) {
|
||||
Map<Long, Long> groupMap = new HashMap<>();
|
||||
List<Long> pointGroup = groupService.queryChain().select(ProdGroup::getSyncId).eq(ProdGroup::getShopId, pointShopId)
|
||||
.isNotNull(ProdGroup::getSyncId).listAs(Long.class);
|
||||
|
|
@ -518,6 +534,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
}
|
||||
prodGroupRelationService.saveOrUpdateBatch(groupRelations, 100);
|
||||
log.info("分组同步,源{}个,已有{}个,同步{}个", list.size(), pointGroup.size(), groupMap.size());
|
||||
buildNotice(mainMapList, "分组同步", groupMap.size(), null, null);
|
||||
return groupMap;
|
||||
}
|
||||
|
||||
|
|
@ -557,7 +574,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
groupService.save(newEntity);
|
||||
}
|
||||
dataMap.put(newEntity.getId(), newEntity.getName());
|
||||
buildNotice(mainMapList, "分组变动", 1, dataMap);
|
||||
buildNotice(mainMapList, "分组变动", 1, "group", dataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
|
||||
if (CollUtil.isEmpty(groupRelationMap)) {
|
||||
List<ProdGroupRelation> oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list();
|
||||
|
|
@ -707,12 +724,17 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
skuService.saveOrUpdateBatch(newSaveSkus, 100);
|
||||
}
|
||||
|
||||
public Map<Long, Long> syncConsGroup(Long sourceShopId, Long pointShopId) {
|
||||
public Map<Long, Long> syncConsGroup(Long sourceShopId, Long pointShopId, List<Map<String, Object>> mainMapList) {
|
||||
Map<Long, Long> consGroupMap = new HashMap<>();
|
||||
List<Long> pointConsGroup = consGroupService.queryChain().select(ConsGroup::getSyncId)
|
||||
List<Long> pointConsGroup = new ArrayList<>();
|
||||
List<ConsGroup> consGroupList = consGroupService.queryChain()
|
||||
.eq(ConsGroup::getShopId, pointShopId)
|
||||
.isNotNull(ConsGroup::getSyncId)
|
||||
.listAs(Long.class);
|
||||
.list();
|
||||
for (ConsGroup consGroup : consGroupList) {
|
||||
pointConsGroup.add(consGroup.getSyncId());
|
||||
consGroupMap.put(consGroup.getSyncId(), consGroup.getId());
|
||||
}
|
||||
List<ConsGroup> list = consGroupService.queryChain().eq(ConsGroup::getShopId, sourceShopId).list();
|
||||
for (ConsGroup consGroup : list) {
|
||||
if (CollUtil.isEmpty(pointConsGroup) || !pointConsGroup.contains(consGroup.getId())) {
|
||||
|
|
@ -724,7 +746,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
consGroupMap.put(consGroup.getId(), newConsGroup.getId());
|
||||
}
|
||||
}
|
||||
log.info("耗材分组同步,源{}个,已有{}个,同步{}个", list.size(), pointConsGroup.size(), consGroupMap.size());
|
||||
log.info("耗材分组同步,源{}个,已有{}个,同步{}个", list.size(), pointConsGroup.size(), consGroupMap.size() - pointConsGroup.size());
|
||||
buildNotice(mainMapList, "耗材分组同步", consGroupMap.size() - pointConsGroup.size(), null, null);
|
||||
return consGroupMap;
|
||||
}
|
||||
|
||||
|
|
@ -759,13 +782,13 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
consGroupService.save(newEntity);
|
||||
}
|
||||
dataMap.put(newEntity.getId(), newEntity.getName());
|
||||
buildNotice(mainMapList, "耗材分组变动", 1, dataMap);
|
||||
buildNotice(mainMapList, "耗材分组变动", 1, "consGroup", dataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
|
||||
});
|
||||
}
|
||||
|
||||
// 耗材
|
||||
public Map<Long, Long> syncConsInfo(Long sourceShopId, Long pointShopId, Map<Long, Long> consGroupMap) {
|
||||
public Map<Long, Long> syncConsInfo(Long sourceShopId, Long pointShopId, Map<Long, Long> consGroupMap, List<Map<String, Object>> mainMapList) {
|
||||
Map<Long, Long> consMap = new HashMap<>();
|
||||
List<Long> pointConsInfo = consInfoService.queryChain().select(ConsInfo::getSyncId)
|
||||
.eq(ConsInfo::getShopId, pointShopId)
|
||||
|
|
@ -785,6 +808,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
}
|
||||
}
|
||||
log.info("耗材同步,源{}个,已有{}个,同步{}个", list.size(), pointConsInfo.size(), consMap.size());
|
||||
buildNotice(mainMapList, "耗材信息同步", consMap.size(), null, null);
|
||||
return consMap;
|
||||
}
|
||||
|
||||
|
|
@ -825,7 +849,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
|
|||
consInfoService.save(newEntity);
|
||||
}
|
||||
dataMap.put(newEntity.getId(), newEntity.getConName());
|
||||
buildNotice(mainMapList, "耗材信息变动", 1, dataMap);
|
||||
buildNotice(mainMapList, "耗材信息变动", 1, "consInfo", dataMap);
|
||||
syncNoticeService.addNotice(shopId, sysUserId, "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue