From 6e58b1e1e6f10e66cf592b6420253a7072128a37 Mon Sep 17 00:00:00 2001 From: paulGao Date: Wed, 20 Oct 2021 10:01:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=9B=B4=E6=94=B9=E5=95=86?= =?UTF-8?q?=E5=93=81=E7=B4=A2=E5=BC=95=E9=83=A8=E5=88=86=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F=E3=80=82=E6=96=B0=E5=A2=9E=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=90=8D=E7=A7=B0=E4=BF=AE=E6=94=B9=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E5=90=8C=E6=AD=A5=E5=88=B0=E5=8F=82=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E4=B8=8B=E6=89=80=E6=9C=89=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lili/listener/GoodsMessageListener.java | 23 +++++++++++++ .../modules/goods/service/GoodsService.java | 8 +++++ .../goods/serviceimpl/BrandServiceImpl.java | 12 +++---- .../CategoryParameterGroupServiceImpl.java | 34 +++++++++++++++++-- .../goods/serviceimpl/GoodsServiceImpl.java | 19 +++++++++-- .../serviceimpl/GoodsSkuServiceImpl.java | 13 +++++-- .../serviceimpl/ParametersServiceImpl.java | 7 +--- .../serviceimpl/EsGoodsIndexServiceImpl.java | 2 +- 8 files changed, 96 insertions(+), 22 deletions(-) diff --git a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java index 845967f0..1ae8826f 100644 --- a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java @@ -1,9 +1,11 @@ package cn.lili.listener; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.event.GoodsCommentCompleteEvent; import cn.lili.modules.distribution.entity.dos.DistributionGoods; import cn.lili.modules.distribution.entity.dos.DistributionSelectedGoods; @@ -27,9 +29,11 @@ import cn.lili.modules.member.service.FootprintService; import cn.lili.modules.member.service.GoodsCollectionService; import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.service.EsGoodsIndexService; +import cn.lili.modules.search.utils.EsIndexUtil; import cn.lili.modules.store.entity.dos.StoreGoodsLabel; import cn.lili.modules.store.service.StoreGoodsLabelService; import cn.lili.modules.store.service.StoreService; +import cn.lili.rocketmq.RocketmqSendCallbackBuilder; import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; @@ -37,6 +41,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.rocketmq.common.message.MessageExt; import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; import org.apache.rocketmq.spring.core.RocketMQListener; +import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -117,6 +122,17 @@ public class GoodsMessageListener implements RocketMQListener { @Autowired private StoreGoodsLabelService storeGoodsLabelService; + /** + * rocketMq + */ + @Autowired + private RocketMQTemplate rocketMQTemplate; + /** + * rocketMq配置 + */ + @Autowired + private RocketmqCustomProperties rocketmqCustomProperties; + @Override public void onMessage(MessageExt messageExt) { @@ -396,6 +412,13 @@ public class GoodsMessageListener implements RocketMQListener { int buyCount = goodsSku.getBuyCount() + goodsCompleteMessage.getBuyNum(); goodsSku.setBuyCount(buyCount); goodsSkuService.update(goodsSku); + + //修改规格索引,发送mq消息 + Map updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap( + MapUtil.builder().put("id", goodsCompleteMessage.getSkuId()).build(), + MapUtil.builder().put("buyCount", buyCount).build()); + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name(); + rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback()); goodsIndexService.updateIndex(goodsCompleteMessage.getSkuId(), new EsGoodsIndex().setBuyCount(buyCount)); } else { log.error("商品SkuId为[" + goodsCompleteMessage.getGoodsId() + "的商品不存在,更新商品失败!"); diff --git a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java index 1853f7c1..2c6e03fb 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java @@ -27,6 +27,14 @@ public interface GoodsService extends IService { */ void underStoreGoods(String storeId); + /** + * 更新商品参数 + * + * @param goodsId 商品id + * @param params 商品参数 + */ + void updateGoodsParams(String goodsId, String params); + /** * 获取某分类下的商品数量 * diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java index f7b76d8f..05612b92 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/BrandServiceImpl.java @@ -3,8 +3,6 @@ package cn.lili.modules.goods.serviceimpl; import cn.hutool.json.JSONUtil; import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; -import cn.lili.modules.goods.service.CategoryService; -import cn.lili.mybatis.util.PageUtil; import cn.lili.modules.goods.entity.dos.Brand; import cn.lili.modules.goods.entity.dos.CategoryBrand; import cn.lili.modules.goods.entity.dto.BrandPageDTO; @@ -12,6 +10,8 @@ import cn.lili.modules.goods.entity.vos.BrandVO; import cn.lili.modules.goods.mapper.BrandMapper; import cn.lili.modules.goods.service.BrandService; import cn.lili.modules.goods.service.CategoryBrandService; +import cn.lili.modules.goods.service.CategoryService; +import cn.lili.mybatis.util.PageUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -96,9 +96,7 @@ public class BrandServiceImpl extends ServiceImpl implements @Override public void deleteBrands(List ids) { - ids.forEach(id -> { - checkoutCategory(id); - }); + ids.forEach(this::checkoutCategory); this.removeByIds(ids); } @@ -112,9 +110,7 @@ public class BrandServiceImpl extends ServiceImpl implements //分了绑定关系查询 List categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandId); if (!categoryBrands.isEmpty()) { - List brandIds = categoryBrands.stream().map(categoryBrand -> { - return categoryBrand.getCategoryId(); - }).collect(Collectors.toList()); + List brandIds = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList()); throw new ServiceException(ResultCode.BRAND_USE_DISABLE_ERROR, JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(brandIds))); } diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryParameterGroupServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryParameterGroupServiceImpl.java index 1e3161ac..c1b877c6 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryParameterGroupServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/CategoryParameterGroupServiceImpl.java @@ -1,17 +1,23 @@ package cn.lili.modules.goods.serviceimpl; +import cn.hutool.json.JSONUtil; import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; -import cn.lili.modules.goods.entity.dos.CategoryBrand; +import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.modules.goods.entity.dos.CategoryParameterGroup; +import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dos.Parameters; +import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; import cn.lili.modules.goods.entity.vos.ParameterGroupVO; import cn.lili.modules.goods.mapper.CategoryParameterGroupMapper; import cn.lili.modules.goods.service.CategoryParameterGroupService; +import cn.lili.modules.goods.service.GoodsService; import cn.lili.modules.goods.service.ParametersService; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,6 +26,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * 分类绑定参数组接口实现 @@ -38,6 +45,15 @@ public class CategoryParameterGroupServiceImpl extends ServiceImpl getCategoryParams(String categoryId) { //根据id查询参数组 @@ -65,8 +81,22 @@ public class CategoryParameterGroupServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.select(Goods::getId, Goods::getParams); + queryWrapper.like(Goods::getParams, origin.getId()); + List> goodsList = this.goodsService.listMaps(queryWrapper); - return false; + for (Map goods : goodsList) { + String params = (String) goods.get("params"); + List goodsParamsDTOS = JSONUtil.toList(params, GoodsParamsDTO.class); + List goodsParamsDTOList = goodsParamsDTOS.stream().filter(i -> i.getGroupId() != null && i.getGroupId().equals(origin.getId())).collect(Collectors.toList()); + for (GoodsParamsDTO goodsParamsDTO : goodsParamsDTOList) { + goodsParamsDTO.setGroupName(categoryParameterGroup.getGroupName()); + } + this.goodsService.updateGoodsParams(goods.get("id").toString(), JSONUtil.toJsonStr(goodsParamsDTOS)); + } + + return this.updateById(categoryParameterGroup); } @Override diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java index c6a82272..af7d8365 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java @@ -13,7 +13,6 @@ import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.security.AuthUser; import cn.lili.common.security.context.UserContext; import cn.lili.common.security.enums.UserEnums; -import cn.lili.common.utils.StringUtils; import cn.lili.modules.goods.entity.dos.Category; import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dos.GoodsGallery; @@ -128,9 +127,23 @@ public class GoodsServiceImpl extends ServiceImpl implements updateGoodsMarketAble(list, GoodsStatusEnum.DOWN, "店铺关闭"); } + /** + * 更新商品参数 + * + * @param goodsId 商品id + * @param params 商品参数 + */ + @Override + public void updateGoodsParams(String goodsId, String params) { + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.eq(Goods::getId, goodsId); + updateWrapper.set(Goods::getParams, params); + this.update(updateWrapper); + } + @Override public final Integer getGoodsCountByCategory(String categoryId) { - QueryWrapper queryWrapper = Wrappers.query(); + QueryWrapper queryWrapper = Wrappers.query(); queryWrapper.like("category_path", categoryId); queryWrapper.eq("delete_flag", false); return this.count(queryWrapper); @@ -223,7 +236,7 @@ public class GoodsServiceImpl extends ServiceImpl implements goodsVO.setCategoryName(categoryName); //参数非空则填写参数 - if (StringUtils.isNotEmpty(goods.getParams())) { + if (CharSequenceUtil.isNotEmpty(goods.getParams())) { goodsVO.setGoodsParamsDTOList(JSONUtil.toList(goods.getParams(), GoodsParamsDTO.class)); } 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 01ab7711..72fd03a7 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 @@ -1,6 +1,7 @@ package cn.lili.modules.goods.serviceimpl; import cn.hutool.core.convert.Convert; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; @@ -32,6 +33,7 @@ import cn.lili.modules.member.service.MemberEvaluationService; import cn.lili.modules.search.entity.dos.EsGoodsAttribute; import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.service.EsGoodsIndexService; +import cn.lili.modules.search.utils.EsIndexUtil; import cn.lili.mybatis.util.PageUtil; import cn.lili.rocketmq.RocketmqSendCallbackBuilder; import cn.lili.rocketmq.tags.GoodsTagsEnum; @@ -476,8 +478,15 @@ public class GoodsSkuServiceImpl extends ServiceImpl i goodsSku.setGrade(grade); //修改规格 this.update(goodsSku); - //修改规格索引 - goodsIndexService.updateIndex(goodsSku.getId(), new EsGoodsIndex().setCommentNum(goodsSku.getCommentNum()).setHighPraiseNum(highPraiseNum).setGrade(grade)); + + + //修改规格索引,发送mq消息 + Map updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap( + MapUtil.builder().put("id", goodsSku.getId()).build(), + MapUtil.builder().put("commentNum", goodsSku.getCommentNum()).put("highPraiseNum", highPraiseNum) + .put("grade", grade).build()); + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name(); + rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback()); //修改商品的评价数量 goodsService.updateGoodsCommentNum(goodsSku.getGoodsId()); diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/ParametersServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/ParametersServiceImpl.java index 2d7e6c9f..6f4476ff 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/ParametersServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/ParametersServiceImpl.java @@ -15,7 +15,6 @@ import cn.lili.modules.goods.service.ParametersService; import cn.lili.rocketmq.RocketmqSendCallbackBuilder; import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -39,7 +38,6 @@ public class ParametersServiceImpl extends ServiceImpl goodsParamsDTOS = JSONUtil.toList(params, GoodsParamsDTO.class); List goodsParamsDTOList = goodsParamsDTOS.stream().filter(i -> i.getGroupId() != null && i.getGroupId().equals(parameters.getGroupId())).collect(Collectors.toList()); this.setGoodsItemDTOList(goodsParamsDTOList, parameters); - LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); - updateWrapper.eq(Goods::getId, goods.get("id")); - updateWrapper.set(Goods::getParams, JSONUtil.toJsonStr(goodsParamsDTOS)); - this.goodsService.update(updateWrapper); + this.goodsService.updateGoodsParams(goods.get("id").toString(), JSONUtil.toJsonStr(goodsParamsDTOS)); goodsIds.add(goods.get("id").toString()); } diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index af57ba99..65b3254a 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -128,7 +128,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements ReflectUtil.setFieldValue(goodsIndex, entry.getValue(), fieldValue); } } - goodsIndexRepository.save(goods); + goodsIndexRepository.save(goodsIndex); } /**