修复更新商品状态不会更新缓存商品数据问题

This commit is contained in:
paulGao 2021-06-22 22:33:10 +08:00
parent 41a86ff7dc
commit 12238d3989
4 changed files with 62 additions and 17 deletions

View File

@ -1,6 +1,8 @@
package cn.lili.controller.goods;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.distribution.service.DistributionService;
@ -20,6 +22,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
@ -37,6 +40,7 @@ import java.util.Map;
* @author Chopper
* @date 2020/11/16 10:06 下午
*/
@Slf4j
@Api(tags = "买家端,商品接口")
@RestController
@RequestMapping("/buyer/goods")
@ -80,9 +84,18 @@ public class GoodsBuyerController {
@PageViewPoint(type = PageViewEnum.SKU, id = "#id")
public ResultMessage<Map<String, Object>> getSku(@NotNull(message = "商品ID不能为空") @PathVariable("goodsId") String goodsId,
@NotNull(message = "SKU ID不能为空") @PathVariable("skuId") String skuId) {
try {
// 读取选中的列表
Map<String, Object> map = goodsSkuService.getGoodsSkuDetail(goodsId, skuId);
return ResultUtil.data(map);
} catch (ServiceException se) {
log.error(se.getMsg(), se);
return ResultUtil.error(se.getResultCode().code(), se.getResultCode().message());
} catch (Exception e) {
log.error(ResultCode.GOODS_ERROR.message(), e);
return ResultUtil.error(ResultCode.GOODS_ERROR);
}
Map<String, Object> map = goodsSkuService.getGoodsSkuDetail(goodsId, skuId);
return ResultUtil.data(map);
}
@ApiOperation(value = "获取商品分页列表")

View File

@ -50,6 +50,7 @@ public enum ResultCode {
/**
* 商品
*/
GOODS_ERROR(11010, "读取商品异常"),
GOODS_NOT_EXIST(11001, "商品已下架"),
GOODS_NAME_ERROR(11002, "商品名称不正确名称应为2-50字符"),
GOODS_UNDER_ERROR(11003, "商品下架失败"),

View File

@ -94,6 +94,14 @@ public interface GoodsSkuService extends IService<GoodsSku> {
*/
List<GoodsSkuVO> getGoodsListByGoodsId(String goodsId);
/**
* 获取goodsId下所有的goodsSku
*
* @param goodsId 商品id
* @return goodsSku列表
*/
List<GoodsSku> getGoodsSkuListByGoodsId(String goodsId);
/**
* 根据goodsSku组装goodsSkuVO
*

View File

@ -5,6 +5,7 @@ import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.lili.common.cache.Cache;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.common.rocketmq.tags.GoodsTagsEnum;
@ -195,8 +196,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
goodsSku = this.getGoodsSkuByIdFromCache(skuId);
//如果使用商品ID无法查询SKU则返回错误
if (goodsSku == null) {
throw new ServiceException("商品已下架");
throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
}
} else if (!goodsSku.getMarketEnable().equals(GoodsStatusEnum.UPPER.name()) || !goodsVO.getIsAuth().equals(GoodsAuthEnum.PASS.name()) || Boolean.TRUE.equals(goodsSku.getDeleteFlag())) {
throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
}
// 获取当前商品的索引信息
EsGoodsIndex goodsIndex = goodsIndexService.findById(skuId);
@ -254,8 +257,16 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
updateWrapper.eq(GoodsSku::getGoodsId, goods.getId());
updateWrapper.set(GoodsSku::getMarketEnable, goods.getMarketEnable());
updateWrapper.set(GoodsSku::getIsAuth, goods.getIsAuth());
this.update(updateWrapper);
generateEsCheck(goods);
updateWrapper.set(GoodsSku::getDeleteFlag, goods.getDeleteFlag());
boolean update = this.update(updateWrapper);
if (Boolean.TRUE.equals(update)) {
List<GoodsSku> goodsSkus = this.getGoodsSkuListByGoodsId(goods.getId());
for (GoodsSku sku : goodsSkus) {
cache.remove(GoodsSkuService.getCacheKeys(sku.getId()));
cache.put(GoodsSkuService.getCacheKeys(sku.getId()), sku);
}
generateEsCheck(goods);
}
}
@Override
@ -282,6 +293,17 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
return this.getGoodsSkuVOList(list);
}
/**
* 获取goodsId下所有的goodsSku
*
* @param goodsId 商品id
* @return goodsSku列表
*/
@Override
public List<GoodsSku> getGoodsSkuListByGoodsId(String goodsId) {
return this.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goodsId));
}
@Override
public List<GoodsSkuVO> getGoodsSkuVOList(List<GoodsSku> list) {
List<GoodsSkuVO> goodsSkuVOS = new ArrayList<>();
@ -432,8 +454,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
*/
private void generateEsCheck(Goods goods) {
//如果商品通过审核&&并且已上架
if (goods.getIsAuth().equals(GoodsAuthEnum.PASS.name()) && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name())) {
List<GoodsSku> goodsSkuList = this.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
List<GoodsSku> goodsSkuList = this.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
if (goods.getIsAuth().equals(GoodsAuthEnum.PASS.name()) && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name()) && Boolean.FALSE.equals(goods.getDeleteFlag())) {
List<EsGoodsIndex> goodsIndices = new ArrayList<>();
for (GoodsSku goodsSku : goodsSkuList) {
EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId());
EsGoodsIndex goodsIndex = new EsGoodsIndex(goodsSku);
@ -443,17 +466,19 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
}
//如果商品库存不为0并且es中有数据
if (goodsSku.getQuantity() > 0 && esGoodsOld == null) {
goodsIndexService.addIndex(goodsIndex);
goodsIndices.add(goodsIndex);
} else if (goodsSku.getQuantity() > 0 && esGoodsOld != null) {
goodsIndexService.updateIndex(goodsIndex);
}
//删除sku缓存
cache.remove(GoodsSkuService.getCacheKeys(goodsSku.getId()));
}
if (!goodsIndices.isEmpty()) {
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback());
}
}
//如果商品状态值不支持es搜索那么将商品信息做下架处理
else {
List<GoodsSku> goodsSkuList = this.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
for (GoodsSku goodsSku : goodsSkuList) {
EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId());
if (esGoodsOld != null) {
@ -488,7 +513,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
*/
private List<GoodsSku> addGoodsSku(List<Map<String, Object>> skuList, Goods goods) {
List<GoodsSku> skus = new ArrayList<>();
List<EsGoodsIndex> goodsIndices = new ArrayList<>();
for (Map<String, Object> skuVO : skuList) {
Map<String, Object> resultMap = this.add(skuVO, goods);
GoodsSku goodsSku = (GoodsSku) resultMap.get("goodsSku");
@ -496,15 +520,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
goodsSku.setSelfOperated(goods.getSelfOperated());
}
goodsSku.setGoodsType(goods.getGoodsType());
EsGoodsIndex goodsIndex = (EsGoodsIndex) resultMap.get("goodsIndex");
skus.add(goodsSku);
goodsIndices.add(goodsIndex);
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
}
this.saveBatch(skus);
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback());
return skus;
}
@ -639,6 +658,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
}
}
private void updateSkuCache() {
}
@Autowired
public void setGoodsService(GoodsService goodsService) {