库存字段类型不一致问题处理
This commit is contained in:
parent
7e58b98e74
commit
394ee27525
@ -64,7 +64,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
* 缓存
|
||||
*/
|
||||
@Autowired
|
||||
private Cache<GoodsSku> cache;
|
||||
private Cache cache;
|
||||
/**
|
||||
* 分类
|
||||
*/
|
||||
@ -75,11 +75,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
*/
|
||||
@Autowired
|
||||
private GoodsGalleryService goodsGalleryService;
|
||||
/**
|
||||
* 缓存
|
||||
*/
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
/**
|
||||
* rocketMq
|
||||
*/
|
||||
@ -195,7 +190,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
@Override
|
||||
public GoodsSku getGoodsSkuByIdFromCache(String id) {
|
||||
//获取缓存中的sku
|
||||
GoodsSku goodsSku = cache.get(GoodsSkuService.getCacheKeys(id));
|
||||
GoodsSku goodsSku = (GoodsSku) cache.get(GoodsSkuService.getCacheKeys(id));
|
||||
//如果缓存中没有信息,则查询数据库,然后写入缓存
|
||||
if (goodsSku == null) {
|
||||
goodsSku = this.getById(id);
|
||||
@ -206,14 +201,14 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
}
|
||||
|
||||
//获取商品库存
|
||||
String quantity = stringRedisTemplate.opsForValue().get(GoodsSkuService.getStockCacheKey(id));
|
||||
Integer integer = (Integer) cache.get(GoodsSkuService.getStockCacheKey(id));
|
||||
|
||||
//库存不为空
|
||||
if (StrUtil.isNotEmpty(quantity)) {
|
||||
if (integer == null) {
|
||||
//库存与缓存中不一致,
|
||||
if (!goodsSku.getQuantity().equals(Convert.toInt(quantity))) {
|
||||
if (!goodsSku.getQuantity().equals(integer)) {
|
||||
//写入最新的库存信息
|
||||
goodsSku.setQuantity(Convert.toInt(quantity));
|
||||
goodsSku.setQuantity(integer);
|
||||
cache.put(GoodsSkuService.getCacheKeys(goodsSku.getId()), goodsSku);
|
||||
}
|
||||
}
|
||||
@ -408,7 +403,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
goodsSku.setQuantity(quantity);
|
||||
this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
|
||||
cache.put(GoodsSkuService.getCacheKeys(skuId), goodsSku);
|
||||
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(skuId), quantity.toString());
|
||||
cache.put(GoodsSkuService.getStockCacheKey(skuId), quantity);
|
||||
|
||||
//更新商品库存
|
||||
List<GoodsSku> goodsSkus = new ArrayList<>();
|
||||
@ -420,12 +415,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
@Override
|
||||
public Integer getStock(String skuId) {
|
||||
String cacheKeys = GoodsSkuService.getStockCacheKey(skuId);
|
||||
String stockStr = stringRedisTemplate.opsForValue().get(cacheKeys);
|
||||
if (stockStr != null) {
|
||||
return Convert.toInt(stockStr);
|
||||
Integer stock = (Integer) cache.get(cacheKeys);
|
||||
if (stock != null) {
|
||||
return stock;
|
||||
} else {
|
||||
GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId);
|
||||
stringRedisTemplate.opsForValue().set(cacheKeys, goodsSku.getQuantity().toString());
|
||||
cache.put(cacheKeys, goodsSku.getQuantity());
|
||||
return goodsSku.getQuantity();
|
||||
}
|
||||
}
|
||||
@ -536,7 +531,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
}
|
||||
goodsSku.setGoodsType(goods.getGoodsType());
|
||||
skus.add(goodsSku);
|
||||
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
|
||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
||||
}
|
||||
this.saveBatch(skus);
|
||||
return skus;
|
||||
|
@ -167,7 +167,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
|
||||
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
|
||||
index.setPromotionMap(goodsCurrentPromotionMap);
|
||||
esGoodsIndices.add(index);
|
||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
|
||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
||||
}
|
||||
//初始化商品索引
|
||||
this.initIndex(esGoodsIndices);
|
||||
|
@ -2,6 +2,7 @@ package cn.lili.test.elasticsearch;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.lili.cache.Cache;
|
||||
import cn.lili.common.vo.PageVO;
|
||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
||||
@ -52,7 +53,7 @@ class EsTest {
|
||||
private GoodsSkuService goodsSkuService;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
private Cache cache;
|
||||
|
||||
@Autowired
|
||||
private PromotionService promotionService;
|
||||
@ -161,7 +162,7 @@ class EsTest {
|
||||
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
|
||||
index.setPromotionMap(goodsCurrentPromotionMap);
|
||||
esGoodsIndices.add(index);
|
||||
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
|
||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
||||
}
|
||||
esGoodsIndexService.initIndex(esGoodsIndices);
|
||||
Assertions.assertTrue(true);
|
||||
|
Loading…
x
Reference in New Issue
Block a user