diff --git a/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java b/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java index e8e0b982..fea39510 100644 --- a/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java +++ b/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java @@ -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> getSku(@NotNull(message = "商品ID不能为空") @PathVariable("goodsId") String goodsId, @NotNull(message = "SKU ID不能为空") @PathVariable("skuId") String skuId) { + try { + // 读取选中的列表 + Map 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 map = goodsSkuService.getGoodsSkuDetail(goodsId, skuId); - return ResultUtil.data(map); } @ApiOperation(value = "获取商品分页列表") diff --git a/framework/src/main/java/cn/lili/common/enums/ResultCode.java b/framework/src/main/java/cn/lili/common/enums/ResultCode.java index d6e4b16b..5d28b696 100644 --- a/framework/src/main/java/cn/lili/common/enums/ResultCode.java +++ b/framework/src/main/java/cn/lili/common/enums/ResultCode.java @@ -50,6 +50,7 @@ public enum ResultCode { /** * 商品 */ + GOODS_ERROR(11010, "读取商品异常"), GOODS_NOT_EXIST(11001, "商品已下架"), GOODS_NAME_ERROR(11002, "商品名称不正确,名称应为2-50字符"), GOODS_UNDER_ERROR(11003, "商品下架失败"), diff --git a/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java b/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java index b7f14218..877f4f5d 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/GoodsSkuService.java @@ -94,6 +94,14 @@ public interface GoodsSkuService extends IService { */ List getGoodsListByGoodsId(String goodsId); + /** + * 获取goodsId下所有的goodsSku + * + * @param goodsId 商品id + * @return goodsSku列表 + */ + List getGoodsSkuListByGoodsId(String goodsId); + /** * 根据goodsSku组装goodsSkuVO * diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java index a628cdb0..c400c82d 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java @@ -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 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 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 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 i return this.getGoodsSkuVOList(list); } + /** + * 获取goodsId下所有的goodsSku + * + * @param goodsId 商品id + * @return goodsSku列表 + */ + @Override + public List getGoodsSkuListByGoodsId(String goodsId) { + return this.list(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goodsId)); + } + @Override public List getGoodsSkuVOList(List list) { List goodsSkuVOS = new ArrayList<>(); @@ -432,8 +454,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl i */ private void generateEsCheck(Goods goods) { //如果商品通过审核&&并且已上架 - if (goods.getIsAuth().equals(GoodsAuthEnum.PASS.name()) && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name())) { - List goodsSkuList = this.list(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goods.getId())); + List goodsSkuList = this.list(new LambdaQueryWrapper().eq(GoodsSku::getGoodsId, goods.getId())); + if (goods.getIsAuth().equals(GoodsAuthEnum.PASS.name()) && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name()) && Boolean.FALSE.equals(goods.getDeleteFlag())) { + List 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 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 goodsSkuList = this.list(new LambdaQueryWrapper().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 i */ private List addGoodsSku(List> skuList, Goods goods) { List skus = new ArrayList<>(); - List goodsIndices = new ArrayList<>(); for (Map skuVO : skuList) { Map resultMap = this.add(skuVO, goods); GoodsSku goodsSku = (GoodsSku) resultMap.get("goodsSku"); @@ -496,15 +520,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl 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 i } } + private void updateSkuCache() { + + } + @Autowired public void setGoodsService(GoodsService goodsService) {