消息通知

This commit is contained in:
2025-04-10 09:59:58 +08:00
parent 648dc97a49
commit bff34c152b
4 changed files with 133 additions and 9 deletions

View File

@@ -52,9 +52,25 @@ public class SyncNotice implements Serializable {
* 店铺id * 店铺id
*/ */
private Long shopId; private Long shopId;
/**
* 标题 数据同步/数据变动/库存预警
*/
private String title;
/** /**
* 消息内容 * 消息内容
* {
* "title": "XX同步/变动",//商品 单位 规格 分组 耗材 耗材分组
* "number": "XX个",
* "map": [
* {
* "id": 1,
* "name": ""
* }
* ]
* }
* {
* "title": "XX库存不足" //商品/耗材
* }
*/ */
private String content; private String content;

View File

@@ -8,7 +8,7 @@ import com.czg.account.entity.SyncNotice;
import java.util.List; import java.util.List;
/** /**
* 服务层。 * 服务层。
* *
* @author zs * @author zs
* @since 2025-04-07 * @since 2025-04-07
@@ -16,14 +16,39 @@ import java.util.List;
public interface SyncNoticeService extends IService<SyncNotice> { public interface SyncNoticeService extends IService<SyncNotice> {
/** /**
* 添加消息 * 添加消息
* @param shopId 店铺id *
* @param shopId 店铺id
* @param sysUserId 操作用户id * @param sysUserId 操作用户id
* @param name 商品/耗材名称 * @param name 商品/耗材名称
* @param id 商品/耗材id * @param id 商品/耗材id
* @param type 0 商品新增 1 商品编辑 2 耗材新增 3 耗材编辑 * @param type 0 商品新增 1 商品编辑 2 耗材新增 3 耗材编辑
*/ */
void addNotice(Long shopId, Long sysUserId, String name, Long id, Integer type); void addNotice(Long shopId, Long sysUserId, String name, Long id, Integer type);
/**
*
* @param shopId 店铺ID
* @param sysUserId 操作人Id
* @param title 标题 数据同步/数据变动/库存预警
* @param content 消息内容
* {
* "title": "XX同步/变动",//商品 单位 规格 分组 耗材 耗材分组
* "number": "XX个",
* "map": [
* {
* "id": 1,
* "name": ""
* }
* ]
* }
* {
* "title": "XX库存不足" //商品/耗材
* }
*
* @param extraJson 扩展数据
*/
void addNotice(Long shopId, Long sysUserId, String title, String content, String extraJson);
Page<SyncNotice> pageInfo(Long shopId, String name, String startTime, String endTime, Integer type, Integer isRead); Page<SyncNotice> pageInfo(Long shopId, String name, String startTime, String endTime, Integer type, Integer isRead);
Boolean read(Long shopId, SyncNoticeReadDTO syncNoticeReadDTO); Boolean read(Long shopId, SyncNoticeReadDTO syncNoticeReadDTO);

View File

@@ -49,6 +49,19 @@ public class SyncNoticeServiceImpl extends ServiceImpl<SyncNoticeMapper, SyncNot
save(syncNotice); save(syncNotice);
} }
@Override
public void addNotice(Long shopId, Long sysUserId, String title, String content, String extraJson) {
SyncNotice syncNotice = new SyncNotice();
syncNotice.setShopId(shopId);
syncNotice.setSysUserId(sysUserId);
syncNotice.setTitle(title);
syncNotice.setContent(content);
// syncNotice.setType(type);
// syncNotice.setExtraJson(extraJson);
save(syncNotice);
}
@Override @Override
public Page<SyncNotice> pageInfo(Long shopId, String name, String startTime, String endTime, Integer type, Integer isRead) { public Page<SyncNotice> pageInfo(Long shopId, String name, String startTime, String endTime, Integer type, Integer isRead) {
QueryWrapper queryWrapper = new QueryWrapper().eq(SyncNotice::getShopId, shopId); QueryWrapper queryWrapper = new QueryWrapper().eq(SyncNotice::getShopId, shopId);

View File

@@ -18,6 +18,7 @@ import com.czg.exception.CzgException;
import com.czg.product.entity.*; import com.czg.product.entity.*;
import com.czg.product.service.*; import com.czg.product.service.*;
import com.czg.product.vo.ProductGroupVo; import com.czg.product.vo.ProductGroupVo;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil; import com.czg.utils.AssertUtil;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@@ -125,6 +126,7 @@ public class ShopSyncServiceImpl implements ShopSyncService {
Map<Long, Long> proMap = new HashMap<>(); Map<Long, Long> proMap = new HashMap<>();
Map<Long, Long> skuMap = new HashMap<>(); Map<Long, Long> skuMap = new HashMap<>();
checkShopInfo(sourceShopId, targetShopId); checkShopInfo(sourceShopId, targetShopId);
List<Map<String, Object>> mainMapList = new ArrayList<>();
//商品 //商品
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<Map<Long, Long>> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId), executor); CompletableFuture<Map<Long, Long>> futureUnit = CompletableFuture.supplyAsync(() -> syncUnit(sourceShopId, targetShopId), executor);
@@ -135,22 +137,55 @@ public class ShopSyncServiceImpl implements ShopSyncService {
allFutures.join(); allFutures.join();
unitMap = futureUnit.join(); unitMap = futureUnit.join();
buildNotice(mainMapList, "单位同步", unitMap.size(), null);
specMap = futureSpec.join(); specMap = futureSpec.join();
buildNotice(mainMapList, "规格同步", specMap.size(), null);
categoryMap = futureCategory.join(); categoryMap = futureCategory.join();
buildNotice(mainMapList, "分类同步", categoryMap.size(), null);
} catch (Exception e) { } catch (Exception e) {
log.error("同步异常", e); log.error("同步异常", e);
throw new CzgException("同步失败"); throw new CzgException("同步失败");
} }
proMap = syncProduct(sourceShopId, targetShopId, unitMap, specMap, categoryMap); proMap = syncProduct(sourceShopId, targetShopId, unitMap, specMap, categoryMap);
buildNotice(mainMapList, "商品同步", proMap.size(), null);
skuMap = syncSku(sourceShopId, targetShopId, proMap); skuMap = syncSku(sourceShopId, targetShopId, proMap);
syncGroup(sourceShopId, targetShopId, proMap); Map<Long, Long> groupMap = syncGroup(sourceShopId, targetShopId, proMap);
buildNotice(mainMapList, "分组同步", groupMap.size(), null);
syncProductPackage(targetShopId, proMap, skuMap); syncProductPackage(targetShopId, proMap, skuMap);
//耗材 //耗材
Map<Long, Long> conGroupMap = syncConsGroup(sourceShopId, targetShopId); Map<Long, Long> conGroupMap = syncConsGroup(sourceShopId, targetShopId);
buildNotice(mainMapList, "耗材分组同步", conGroupMap.size(), null);
Map<Long, Long> consInfoMap = syncConsInfo(sourceShopId, targetShopId, conGroupMap); Map<Long, Long> consInfoMap = syncConsInfo(sourceShopId, targetShopId, conGroupMap);
buildNotice(mainMapList, "耗材信息同步", consInfoMap.size(), null);
syncConsPro(sourceShopId, targetShopId, consInfoMap, proMap); syncConsPro(sourceShopId, targetShopId, consInfoMap, proMap);
syncNoticeService.addNotice(targetShopId, StpKit.USER.getLoginIdAsLong(), "数据同步", JSON.toJSONString(mainMapList), null);
}
/**
* 构建消息
*
* @param mainMapList 主消息集合
* @param title 内容title "XX同步/变动",//商品 单位 规格 分组 耗材 耗材分组
* @param size 变动数量
* @param dataMap 变动数据集合<Id,名称>
*/
private void buildNotice(List<Map<String, Object>> mainMapList, String title, int size, Map<Long, String> dataMap) {
Map<String, Object> mainJsonMap = new HashMap<>();
mainJsonMap.put("title", title);
mainJsonMap.put("number", size + "");
if (CollUtil.isNotEmpty(dataMap)) {
List<Map<String, Object>> mapList = new ArrayList<>();
dataMap.forEach((k, v) -> {
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("id", k);
innerMap.put("name", v);
mapList.add(innerMap);
});
mainJsonMap.put("map", mapList);
}
mainMapList.add(mainJsonMap);
} }
public Map<Long, Long> syncUnit(Long sourceShopId, Long pointShopId) { public Map<Long, Long> syncUnit(Long sourceShopId, Long pointShopId) {
@@ -191,6 +226,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
Map<Long, ShopProdUnit> finalMap = map; Map<Long, ShopProdUnit> finalMap = map;
shopIds.forEach(shopId -> { shopIds.forEach(shopId -> {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> unitDataMap = new HashMap<>();
ShopProdUnit newUnit = BeanUtil.copyProperties(mainUnit, ShopProdUnit.class); ShopProdUnit newUnit = BeanUtil.copyProperties(mainUnit, ShopProdUnit.class);
newUnit.setSyncId(mainUnit.getId()); newUnit.setSyncId(mainUnit.getId());
@@ -203,6 +240,9 @@ public class ShopSyncServiceImpl implements ShopSyncService {
newUnit.setId(null); newUnit.setId(null);
unitService.save(newUnit); unitService.save(newUnit);
} }
unitDataMap.put(newUnit.getId(), newUnit.getName());
buildNotice(mainMapList, "单位变动", 1, unitDataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newUnit));
}); });
} }
@@ -246,6 +286,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
Map<Long, ShopProdSpec> finalMap = map; Map<Long, ShopProdSpec> finalMap = map;
shopIds.forEach(shopId -> { shopIds.forEach(shopId -> {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> dataMap = new HashMap<>();
ShopProdSpec newEntity = BeanUtil.copyProperties(mainEntity, ShopProdSpec.class); ShopProdSpec newEntity = BeanUtil.copyProperties(mainEntity, ShopProdSpec.class);
newEntity.setSyncId(mainEntity.getId()); newEntity.setSyncId(mainEntity.getId());
newEntity.setShopId(shopId); newEntity.setShopId(shopId);
@@ -257,6 +299,9 @@ public class ShopSyncServiceImpl implements ShopSyncService {
newEntity.setId(null); newEntity.setId(null);
specService.save(newEntity); specService.save(newEntity);
} }
dataMap.put(newEntity.getId(), newEntity.getName());
buildNotice(mainMapList, "规格变动", 1, dataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
}); });
} }
@@ -332,6 +377,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
Map<Long, ShopProdCategory> finalMap = map; Map<Long, ShopProdCategory> finalMap = map;
for (Long shopId : shopIds) { for (Long shopId : shopIds) {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> dataMap = new HashMap<>();
ShopProdCategory newEntity = BeanUtil.copyProperties(mainEntity, ShopProdCategory.class); ShopProdCategory newEntity = BeanUtil.copyProperties(mainEntity, ShopProdCategory.class);
newEntity.setSyncId(mainEntity.getId()); newEntity.setSyncId(mainEntity.getId());
newEntity.setShopId(shopId); newEntity.setShopId(shopId);
@@ -344,6 +391,9 @@ public class ShopSyncServiceImpl implements ShopSyncService {
newEntity.setId(null); newEntity.setId(null);
categoryService.save(newEntity); categoryService.save(newEntity);
} }
dataMap.put(newEntity.getId(), newEntity.getName());
buildNotice(mainMapList, "分类变动", 1, dataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
} }
} }
@@ -410,6 +460,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
Map<Long, Product> finalMap = map; Map<Long, Product> finalMap = map;
for (Long shopId : shopIds) { for (Long shopId : shopIds) {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> dataMap = new HashMap<>();
Product newEntity = BeanUtil.copyProperties(mainEntity, Product.class); Product newEntity = BeanUtil.copyProperties(mainEntity, Product.class);
newEntity.setSyncId(mainEntity.getSyncId()); newEntity.setSyncId(mainEntity.getSyncId());
newEntity.setShopId(shopId); newEntity.setShopId(shopId);
@@ -430,6 +482,9 @@ public class ShopSyncServiceImpl implements ShopSyncService {
if ("package".equals(newEntity.getType()) || "coupon".equals(newEntity.getType())) { if ("package".equals(newEntity.getType()) || "coupon".equals(newEntity.getType())) {
syncProductPackageBySourceShop(newEntity, shopId); syncProductPackageBySourceShop(newEntity, shopId);
} }
dataMap.put(newEntity.getId(), newEntity.getName());
buildNotice(mainMapList, "商品变动", 1, dataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
} }
} }
@@ -485,6 +540,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
List<ProdGroupRelation> groupRelationList = new ArrayList<>(); List<ProdGroupRelation> groupRelationList = new ArrayList<>();
for (Long shopId : shopIds) { for (Long shopId : shopIds) {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> dataMap = new HashMap<>();
ProdGroup newEntity = BeanUtil.copyProperties(mainEntity, ProdGroup.class); ProdGroup newEntity = BeanUtil.copyProperties(mainEntity, ProdGroup.class);
newEntity.setSyncId(mainEntity.getId()); newEntity.setSyncId(mainEntity.getId());
newEntity.setShopId(shopId); newEntity.setShopId(shopId);
@@ -496,15 +553,18 @@ public class ShopSyncServiceImpl implements ShopSyncService {
newEntity.setId(null); newEntity.setId(null);
groupService.save(newEntity); groupService.save(newEntity);
} }
dataMap.put(newEntity.getId(), newEntity.getName());
buildNotice(mainMapList, "分组变动", 1, dataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
if (CollUtil.isEmpty(groupRelationMap)) { if (CollUtil.isEmpty(groupRelationMap)) {
List<ProdGroupRelation> oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list(); List<ProdGroupRelation> oldProdGroup = prodGroupRelationService.queryChain().eq(ProdGroupRelation::getProdGroupId, newEntity.getId()).list();
if(CollUtil.isEmpty(oldProdGroup)){ if (CollUtil.isEmpty(oldProdGroup)) {
continue; continue;
} }
Set<Long> oldProds = oldProdGroup.stream().map(ProdGroupRelation::getProductId).collect(Collectors.toSet()); Set<Long> oldProds = oldProdGroup.stream().map(ProdGroupRelation::getProductId).collect(Collectors.toSet());
List<Long> rmIds = productService.queryChain().select(Product::getId).eq(Product::getShopId, shopId) List<Long> rmIds = productService.queryChain().select(Product::getId).eq(Product::getShopId, shopId)
.in(Product::getId, oldProds).isNotNull(Product::getSyncId).listAs(Long.class); .in(Product::getId, oldProds).isNotNull(Product::getSyncId).listAs(Long.class);
if(CollUtil.isEmpty(rmIds)){ if (CollUtil.isEmpty(rmIds)) {
continue; continue;
} }
QueryWrapper queryWrapper = new QueryWrapper(); QueryWrapper queryWrapper = new QueryWrapper();
@@ -682,6 +742,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
Map<Long, ConsGroup> finalMap = map; Map<Long, ConsGroup> finalMap = map;
shopIds.forEach(shopId -> { shopIds.forEach(shopId -> {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> dataMap = new HashMap<>();
ConsGroup newEntity = BeanUtil.copyProperties(mainEntity, ConsGroup.class); ConsGroup newEntity = BeanUtil.copyProperties(mainEntity, ConsGroup.class);
newEntity.setSyncId(mainEntity.getId()); newEntity.setSyncId(mainEntity.getId());
newEntity.setShopId(shopId); newEntity.setShopId(shopId);
@@ -693,6 +755,9 @@ public class ShopSyncServiceImpl implements ShopSyncService {
newEntity.setId(null); newEntity.setId(null);
consGroupService.save(newEntity); consGroupService.save(newEntity);
} }
dataMap.put(newEntity.getId(), newEntity.getName());
buildNotice(mainMapList, "耗材分组变动", 1, dataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
}); });
} }
@@ -737,6 +802,8 @@ public class ShopSyncServiceImpl implements ShopSyncService {
} }
Map<Long, ConsInfo> finalMap = map; Map<Long, ConsInfo> finalMap = map;
for (Long shopId : shopIds) { for (Long shopId : shopIds) {
List<Map<String, Object>> mainMapList = new ArrayList<>();
Map<Long, String> dataMap = new HashMap<>();
ConsInfo newEntity = BeanUtil.copyProperties(mainEntity, ConsInfo.class); ConsInfo newEntity = BeanUtil.copyProperties(mainEntity, ConsInfo.class);
newEntity.setSyncId(mainEntity.getId()); newEntity.setSyncId(mainEntity.getId());
newEntity.setShopId(shopId); newEntity.setShopId(shopId);
@@ -754,6 +821,9 @@ public class ShopSyncServiceImpl implements ShopSyncService {
newEntity.setStockNumber(BigDecimal.ZERO); newEntity.setStockNumber(BigDecimal.ZERO);
consInfoService.save(newEntity); consInfoService.save(newEntity);
} }
dataMap.put(newEntity.getId(), newEntity.getConName());
buildNotice(mainMapList, "耗材信息变动", 1, dataMap);
syncNoticeService.addNotice(shopId, StpKit.USER.getLoginIdAsLong(), "数据变动", JSON.toJSONString(mainMapList), JSON.toJSONString(newEntity));
} }
} }