fix: 优化更新商品评价为所有Sku的评价
This commit is contained in:
parent
cfb70eff8a
commit
1422b6fdbc
@ -7,6 +7,7 @@ import cn.lili.event.GoodsCommentCompleteEvent;
|
||||
import cn.lili.event.StoreSettingChangeEvent;
|
||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
||||
import cn.lili.modules.goods.service.GoodsService;
|
||||
import cn.lili.modules.goods.service.GoodsSkuService;
|
||||
import cn.lili.modules.member.entity.dos.MemberEvaluation;
|
||||
import cn.lili.modules.store.entity.dos.Store;
|
||||
@ -31,12 +32,15 @@ public class GoodsSkuExecute implements GoodsCommentCompleteEvent, StoreSettingC
|
||||
@Autowired
|
||||
private GoodsSkuService goodsSkuService;
|
||||
|
||||
@Autowired
|
||||
private GoodsService goodsService;
|
||||
|
||||
@Autowired
|
||||
private Cache cache;
|
||||
|
||||
@Override
|
||||
public void goodsComment(MemberEvaluation memberEvaluation) {
|
||||
goodsSkuService.updateGoodsSkuCommentNum(memberEvaluation.getSkuId());
|
||||
goodsService.updateGoodsCommentNum(memberEvaluation.getGoodsId(), memberEvaluation.getSkuId());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -211,13 +211,6 @@ public interface GoodsSkuService extends IService<GoodsSku> {
|
||||
*/
|
||||
void updateGoodsStock(List<GoodsSku> goodsSkus);
|
||||
|
||||
/**
|
||||
* 更新SKU评价数量
|
||||
*
|
||||
* @param skuId SKUId
|
||||
*/
|
||||
void updateGoodsSkuCommentNum(String skuId);
|
||||
|
||||
/**
|
||||
* 根据商品id获取全部skuId的集合
|
||||
*
|
||||
@ -261,9 +254,10 @@ public interface GoodsSkuService extends IService<GoodsSku> {
|
||||
/**
|
||||
* 更新商品sku评分
|
||||
*
|
||||
* @param goodsId goodsId
|
||||
* @param skuId skuId
|
||||
* @param grade 评分
|
||||
* @param commentNum 评论数量
|
||||
*/
|
||||
void updateGoodsSkuGrade(String skuId, double grade,int commentNum);
|
||||
void updateGoodsSkuGrade(String goodsId, String skuId, double grade,int commentNum);
|
||||
}
|
@ -14,10 +14,7 @@ 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.modules.goods.entity.dos.Category;
|
||||
import cn.lili.modules.goods.entity.dos.Goods;
|
||||
import cn.lili.modules.goods.entity.dos.GoodsGallery;
|
||||
import cn.lili.modules.goods.entity.dos.Wholesale;
|
||||
import cn.lili.modules.goods.entity.dos.*;
|
||||
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
|
||||
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
|
||||
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
||||
@ -444,11 +441,20 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateGoodsCommentNum(String goodsId, String skuId) {
|
||||
GoodsSku goodsSku = goodsSkuService.getGoodsSkuByIdFromCache(skuId);
|
||||
if (goodsSku == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
//获取商品信息
|
||||
Goods goods = this.getById(goodsId);
|
||||
|
||||
if (goods == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
//修改商品评价数量
|
||||
long commentNum = memberEvaluationService.getEvaluationCount(EvaluationQueryParams.builder().goodsId(goodsId).status("OPEN").build());
|
||||
goods.setCommentNum((int) (commentNum));
|
||||
@ -458,10 +464,18 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
|
||||
//好评率
|
||||
double grade = NumberUtil.mul(NumberUtil.div(highPraiseNum, goods.getCommentNum().doubleValue(), 2), 100);
|
||||
goods.setGrade(grade);
|
||||
this.updateById(goods);
|
||||
LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
|
||||
updateWrapper.eq(Goods::getId, goodsId);
|
||||
updateWrapper.set(Goods::getCommentNum, goods.getCommentNum());
|
||||
updateWrapper.set(Goods::getGrade, goods.getGrade());
|
||||
this.update(updateWrapper);
|
||||
|
||||
cache.remove(CachePrefix.GOODS.getPrefix() + goodsId);
|
||||
|
||||
|
||||
// 修改商品sku评价数量
|
||||
this.goodsSkuService.updateGoodsSkuGrade(goodsId, skuId, grade, goods.getCommentNum());
|
||||
|
||||
Map<String, Object> updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap(MapUtil.builder(new HashMap<String, Object>()).put("id", skuId).build(), MapUtil.builder(new HashMap<String, Object>()).put("commentNum", goods.getCommentNum()).put("highPraiseNum", highPraiseNum).put("grade", grade).build());
|
||||
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("更新商品索引信息", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name(), JSONUtil.toJsonStr(updateIndexFieldsMap)));
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package cn.lili.modules.goods.serviceimpl;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.lili.cache.Cache;
|
||||
@ -36,8 +35,6 @@ import cn.lili.modules.goods.service.WholesaleService;
|
||||
import cn.lili.modules.goods.sku.GoodsSkuBuilder;
|
||||
import cn.lili.modules.goods.sku.render.SalesModelRender;
|
||||
import cn.lili.modules.member.entity.dos.FootPrint;
|
||||
import cn.lili.modules.member.entity.dto.EvaluationQueryParams;
|
||||
import cn.lili.modules.member.entity.enums.EvaluationGradeEnum;
|
||||
import cn.lili.modules.member.service.MemberEvaluationService;
|
||||
import cn.lili.modules.promotion.entity.dos.Coupon;
|
||||
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
|
||||
@ -604,30 +601,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateGoodsSkuCommentNum(String skuId) {
|
||||
//获取商品信息
|
||||
GoodsSku goodsSku = this.getGoodsSkuByIdFromCache(skuId);
|
||||
|
||||
//好评数量
|
||||
long highPraiseNum = memberEvaluationService.getEvaluationCount(EvaluationQueryParams.builder().status("OPEN").grade(EvaluationGradeEnum.GOOD.name()).skuId(skuId).build());
|
||||
|
||||
//更新商品评价数量
|
||||
long commentNum = memberEvaluationService.getEvaluationCount(EvaluationQueryParams.builder().status("OPEN").skuId(skuId).build());
|
||||
goodsSku.setCommentNum((int) commentNum);
|
||||
|
||||
//好评率
|
||||
double grade = NumberUtil.mul(NumberUtil.div(highPraiseNum, goodsSku.getCommentNum().doubleValue(), 2), 100);
|
||||
goodsSku.setGrade(grade);
|
||||
//修改规格
|
||||
this.updateGoodsSkuGrade(skuId, grade, goodsSku.getCommentNum());
|
||||
|
||||
//修改商品的评价数量
|
||||
goodsService.updateGoodsCommentNum(goodsSku.getGoodsId(), skuId);
|
||||
clearCache(skuId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据商品id获取全部skuId的集合
|
||||
*
|
||||
@ -686,12 +659,13 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateGoodsSkuGrade(String skuId, double grade, int commentNum) {
|
||||
public void updateGoodsSkuGrade(String goodsId, String skuId, double grade, int commentNum) {
|
||||
LambdaUpdateWrapper<GoodsSku> updateWrapper = new LambdaUpdateWrapper<>();
|
||||
updateWrapper.eq(GoodsSku::getId, skuId);
|
||||
updateWrapper.eq(GoodsSku::getGoodsId, goodsId);
|
||||
updateWrapper.set(GoodsSku::getGrade, grade);
|
||||
updateWrapper.set(GoodsSku::getCommentNum, commentNum);
|
||||
this.update(updateWrapper);
|
||||
clearCache(skuId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user