库存字段类型不一致问题处理

This commit is contained in:
Chopper 2021-11-02 12:45:52 +08:00
parent 7e58b98e74
commit 394ee27525
3 changed files with 16 additions and 20 deletions

View File

@ -64,7 +64,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
* 缓存 * 缓存
*/ */
@Autowired @Autowired
private Cache<GoodsSku> cache; private Cache cache;
/** /**
* 分类 * 分类
*/ */
@ -75,11 +75,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
*/ */
@Autowired @Autowired
private GoodsGalleryService goodsGalleryService; private GoodsGalleryService goodsGalleryService;
/**
* 缓存
*/
@Autowired
private StringRedisTemplate stringRedisTemplate;
/** /**
* rocketMq * rocketMq
*/ */
@ -195,7 +190,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Override @Override
public GoodsSku getGoodsSkuByIdFromCache(String id) { public GoodsSku getGoodsSkuByIdFromCache(String id) {
//获取缓存中的sku //获取缓存中的sku
GoodsSku goodsSku = cache.get(GoodsSkuService.getCacheKeys(id)); GoodsSku goodsSku = (GoodsSku) cache.get(GoodsSkuService.getCacheKeys(id));
//如果缓存中没有信息则查询数据库然后写入缓存 //如果缓存中没有信息则查询数据库然后写入缓存
if (goodsSku == null) { if (goodsSku == null) {
goodsSku = this.getById(id); 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); cache.put(GoodsSkuService.getCacheKeys(goodsSku.getId()), goodsSku);
} }
} }
@ -408,7 +403,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
goodsSku.setQuantity(quantity); goodsSku.setQuantity(quantity);
this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity)); this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
cache.put(GoodsSkuService.getCacheKeys(skuId), goodsSku); 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<>(); List<GoodsSku> goodsSkus = new ArrayList<>();
@ -420,12 +415,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Override @Override
public Integer getStock(String skuId) { public Integer getStock(String skuId) {
String cacheKeys = GoodsSkuService.getStockCacheKey(skuId); String cacheKeys = GoodsSkuService.getStockCacheKey(skuId);
String stockStr = stringRedisTemplate.opsForValue().get(cacheKeys); Integer stock = (Integer) cache.get(cacheKeys);
if (stockStr != null) { if (stock != null) {
return Convert.toInt(stockStr); return stock;
} else { } else {
GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId); GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId);
stringRedisTemplate.opsForValue().set(cacheKeys, goodsSku.getQuantity().toString()); cache.put(cacheKeys, goodsSku.getQuantity());
return goodsSku.getQuantity(); return goodsSku.getQuantity();
} }
} }
@ -536,7 +531,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
} }
goodsSku.setGoodsType(goods.getGoodsType()); goodsSku.setGoodsType(goods.getGoodsType());
skus.add(goodsSku); skus.add(goodsSku);
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
} }
this.saveBatch(skus); this.saveBatch(skus);
return skus; return skus;

View File

@ -167,7 +167,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
index.setPromotionMap(goodsCurrentPromotionMap); index.setPromotionMap(goodsCurrentPromotionMap);
esGoodsIndices.add(index); esGoodsIndices.add(index);
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
} }
//初始化商品索引 //初始化商品索引
this.initIndex(esGoodsIndices); this.initIndex(esGoodsIndices);

View File

@ -2,6 +2,7 @@ package cn.lili.test.elasticsearch;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.common.vo.PageVO; import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum; import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
@ -52,7 +53,7 @@ class EsTest {
private GoodsSkuService goodsSkuService; private GoodsSkuService goodsSkuService;
@Autowired @Autowired
private StringRedisTemplate stringRedisTemplate; private Cache cache;
@Autowired @Autowired
private PromotionService promotionService; private PromotionService promotionService;
@ -161,7 +162,7 @@ class EsTest {
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
index.setPromotionMap(goodsCurrentPromotionMap); index.setPromotionMap(goodsCurrentPromotionMap);
esGoodsIndices.add(index); esGoodsIndices.add(index);
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
} }
esGoodsIndexService.initIndex(esGoodsIndices); esGoodsIndexService.initIndex(esGoodsIndices);
Assertions.assertTrue(true); Assertions.assertTrue(true);