增加商品库存redis缓存 & 库存变动发送rabbit消息
This commit is contained in:
@@ -2,6 +2,8 @@ package com.czg.service.product.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import com.czg.config.RabbitPublisher;
|
||||
import com.czg.enums.YesNoEnum;
|
||||
@@ -129,7 +131,7 @@ public class ProductRpcServiceImpl implements ProductRpcService {
|
||||
consStockFlowService.saveFlow(consStockFlow);
|
||||
}
|
||||
}
|
||||
//ThreadUtil.execAsync(() -> rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId)));
|
||||
ThreadUtil.execAsync(() -> rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId)));
|
||||
log.info("ProductService.--------------------------------------------库存更新成功");
|
||||
}
|
||||
|
||||
@@ -202,7 +204,7 @@ public class ProductRpcServiceImpl implements ProductRpcService {
|
||||
consStockFlowService.saveFlow(consStockFlow);
|
||||
}
|
||||
}
|
||||
//ThreadUtil.execAsync(() -> rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId)));
|
||||
ThreadUtil.execAsync(() -> rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId)));
|
||||
log.info("ProductService.--------------------------------------------库存更新成功");
|
||||
}
|
||||
|
||||
@@ -278,7 +280,7 @@ public class ProductRpcServiceImpl implements ProductRpcService {
|
||||
consStockFlowService.saveFlow(consStockFlow);
|
||||
}
|
||||
}
|
||||
//ThreadUtil.execAsync(() -> rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId)));
|
||||
ThreadUtil.execAsync(() -> rabbitPublisher.sendProductInfoChangeMsg(Convert.toStr(shopId)));
|
||||
log.info("ProductService.--------------------------------------------库存更新成功");
|
||||
}
|
||||
|
||||
|
||||
@@ -770,6 +770,11 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
||||
Map<Long, Integer> stock = list.stream().collect(Collectors.toMap(Product::getId, Product::getStockNumber));
|
||||
records.parallelStream().forEach(record -> {
|
||||
record.setStockNumber(stock.getOrDefault(record.getId(), 0));
|
||||
refreshRedisStock(record.getShopId(), record.getId(), record.getStockNumber());
|
||||
});
|
||||
}
|
||||
private void refreshRedisStock(Long shopId, Long productId, Integer stockNumber) {
|
||||
String key = StrUtil.format(CacheConstant.SHOP_PRODUCT_STOCK, shopId, productId);
|
||||
redisService.set(key, stockNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.czg.product.vo.ShopProductInfoVo;
|
||||
import com.czg.product.vo.ShopProductSkuInfoVo;
|
||||
import com.czg.product.vo.ShopProductVo;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.service.product.mapper.ProdGroupMapper;
|
||||
import com.czg.service.product.mapper.ProdGroupRelationMapper;
|
||||
import com.czg.service.product.mapper.ProdSkuMapper;
|
||||
@@ -56,6 +57,7 @@ public class UProductServiceImpl extends ServiceImpl<ProductMapper, Product> imp
|
||||
private final ProdSkuMapper prodSkuMapper;
|
||||
private final ProdGroupMapper prodGroupMapper;
|
||||
private final ProdGroupRelationMapper prodGroupRelationMapper;
|
||||
private final RedisService redisService;
|
||||
|
||||
@Override
|
||||
@Cacheable(value = CacheConstant.USER_CLIENT_HOTS_PRODUCT, key = "#shopId", unless = "#result.isEmpty()")
|
||||
@@ -103,6 +105,7 @@ public class UProductServiceImpl extends ServiceImpl<ProductMapper, Product> imp
|
||||
for (Long productId : productIdList) {
|
||||
ShopProductVo product = productKv.get(productId);
|
||||
ShopProductVo prod = BeanUtil.copyProperties(product, ShopProductVo.class);
|
||||
prod.setShopId(shopId);
|
||||
if (product != null) {
|
||||
productList.add(prod);
|
||||
}
|
||||
@@ -174,6 +177,7 @@ public class UProductServiceImpl extends ServiceImpl<ProductMapper, Product> imp
|
||||
}
|
||||
productList.parallelStream().forEach(record -> {
|
||||
record.setStockNumber(stock.getOrDefault(record.getId(), 0));
|
||||
refreshRedisStock(record.getShopId(), record.getId(), record.getStockNumber());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -184,9 +188,15 @@ public class UProductServiceImpl extends ServiceImpl<ProductMapper, Product> imp
|
||||
}
|
||||
productList.parallelStream().forEach(record -> {
|
||||
record.setStockNumber(productStock.getOrDefault(record.getId(), 0));
|
||||
refreshRedisStock(record.getShopId(), record.getId(), record.getStockNumber());
|
||||
});
|
||||
}
|
||||
|
||||
private void refreshRedisStock(Long shopId, Long productId, Integer stockNumber) {
|
||||
String key = StrUtil.format(CacheConstant.SHOP_PRODUCT_STOCK, shopId, productId);
|
||||
redisService.set(key, stockNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算是否在可售时间内
|
||||
*
|
||||
|
||||
@@ -92,7 +92,8 @@
|
||||
t1.select_spec_info,
|
||||
t1.group_snap,
|
||||
t1.pack_fee,
|
||||
ifnull(t4.sales_volume, 0) as sales_volume
|
||||
ifnull(t4.sales_volume, 0) as sales_volume,
|
||||
t1.shop_id
|
||||
from tb_product t1
|
||||
left join (select x.product_id,
|
||||
x.id as sku_id,
|
||||
|
||||
Reference in New Issue
Block a user