!9 修复更新商品状态不会更新缓存商品数据问题
Merge pull request !9 from OceansDeep/feature/pg
This commit is contained in:
commit
6419d5b7e8
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取商品分页列表")
|
||||
|
@ -51,6 +51,7 @@ public enum ResultCode {
|
||||
/**
|
||||
* 商品
|
||||
*/
|
||||
GOODS_ERROR(11010, "读取商品异常"),
|
||||
GOODS_NOT_EXIST(11001, "商品已下架"),
|
||||
GOODS_NAME_ERROR(11002, "商品名称不正确,名称应为2-50字符"),
|
||||
GOODS_UNDER_ERROR(11003, "商品下架失败"),
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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,9 +257,17 @@ 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);
|
||||
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
|
||||
public List<GoodsSku> getGoodsSkuByIdFromCache(List<String> ids) {
|
||||
@ -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()));
|
||||
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,7 +658,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Autowired
|
||||
public void setGoodsService(GoodsService goodsService) {
|
||||
this.goodsService = goodsService;
|
||||
|
Loading…
x
Reference in New Issue
Block a user