fix: 优化更新商品索引,销量,评论方法

This commit is contained in:
misworga831 2023-10-16 11:48:52 +08:00
parent efe0bd4bad
commit 7c64e1f11d
7 changed files with 81 additions and 46 deletions

View File

@ -322,8 +322,6 @@ public class StockUpdateExecute implements OrderStatusChangeEvent {
for (int i = 0; i < skuStocks.size(); i++) {
goodsSkus.get(i).setQuantity(Convert.toInt(skuStocks.get(i).toString()));
}
//批量修改商品库存
goodsSkuService.updateBatchById(goodsSkus);
//促销库存处理
if (!promotionKey.isEmpty()) {
@ -336,7 +334,7 @@ public class StockUpdateExecute implements OrderStatusChangeEvent {
promotionGoodsService.updatePromotionGoodsStock(promotionGoods);
}
//商品库存包含sku库存集合批量更新商品库存相关
goodsSkuService.updateGoodsStuck(goodsSkus);
goodsSkuService.updateGoodsStock(goodsSkus);
log.info("订单确认,库存同步:商品信息--{};促销信息---{}", goodsSkus, promotionGoods);
@ -369,8 +367,7 @@ public class StockUpdateExecute implements OrderStatusChangeEvent {
}
log.info("订单取消,库存还原:{}", goodsSkus);
//批量修改商品库存
goodsSkuService.updateBatchById(goodsSkus);
goodsSkuService.updateGoodsStuck(goodsSkus);
goodsSkuService.updateGoodsStock(goodsSkus);
}
}

View File

@ -492,7 +492,7 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
}
int buyCount = goodsSku.getBuyCount() + goodsCompleteMessage.getBuyNum();
goodsSku.setBuyCount(buyCount);
goodsSkuService.update(goodsSku);
goodsSkuService.updateGoodsSkuBuyCount(goodsSku.getId(), buyCount);
this.goodsIndexService.updateIndex(MapUtil.builder(new HashMap<String, Object>()).put("id", goodsCompleteMessage.getSkuId()).build(), MapUtil.builder(new HashMap<String, Object>()).put("buyCount", buyCount).build());

View File

@ -1,5 +1,6 @@
package cn.lili.modules.goods.entity.dto;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
@ -9,6 +10,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import java.util.Arrays;
import java.util.List;
/**
* 商品查询条件
@ -34,6 +36,9 @@ public class GoodsSearchParams extends PageVO {
@ApiModelProperty(value = "商品编号")
private String id;
@ApiModelProperty(value = "商品编号")
private List<String> ids;
@ApiModelProperty(value = "商家ID")
private String storeId;
@ -133,6 +138,8 @@ public class GoodsSearchParams extends PageVO {
queryWrapper.eq("sales_model", salesModel);
}
queryWrapper.in(CollUtil.isNotEmpty(ids), "id", ids);
queryWrapper.eq("delete_flag", false);
this.betweenWrapper(queryWrapper);
return queryWrapper;

View File

@ -12,6 +12,9 @@ import lombok.Data;
@Data
public class GoodsSkuStockDTO {
@ApiModelProperty(value = "商品id")
private String goodsId;
@ApiModelProperty(value = "商品skuId")
private String skuId;

View File

@ -2,6 +2,7 @@ package cn.lili.modules.goods.mapper;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -114,4 +115,6 @@ public interface GoodsSkuMapper extends BaseMapper<GoodsSku> {
@Select("SELECT *,g.params as params FROM li_goods_sku gs inner join li_goods g on gs.goods_id = g.id ${ew.customSqlSegment}")
IPage<GoodsSkuDTO> queryByParams(IPage<GoodsSkuDTO> page, @Param(Constants.WRAPPER) Wrapper<GoodsSkuDTO> queryWrapper);
@Select("SELECT id as sku_id, quantity, goods_id FROM li_goods_sku ${ew.customSqlSegment}")
List<GoodsSkuStockDTO> queryStocks(@Param(Constants.WRAPPER) Wrapper<GoodsSku> queryWrapper);
}

View File

@ -209,7 +209,7 @@ public interface GoodsSkuService extends IService<GoodsSku> {
*
* @param goodsSkus
*/
void updateGoodsStuck(List<GoodsSku> goodsSkus);
void updateGoodsStock(List<GoodsSku> goodsSkus);
/**
* 更新SKU评价数量
@ -249,4 +249,21 @@ public interface GoodsSkuService extends IService<GoodsSku> {
* @param goodsOperationDTO 商品操作信息
*/
void renderGoodsSkuList(List<GoodsSku> goodsSkuList, GoodsOperationDTO goodsOperationDTO);
/**
* 更新商品sku购买数量
*
* @param skuId skuId
* @param buyCount 购买数量
*/
void updateGoodsSkuBuyCount(String skuId, int buyCount);
/**
* 更新商品sku评分
*
* @param skuId skuId
* @param grade 评分
* @param commentNum 评论数量
*/
void updateGoodsSkuGrade(String skuId, double grade,int commentNum);
}

View File

@ -3,7 +3,6 @@ package cn.lili.modules.goods.serviceimpl;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
@ -148,7 +147,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
if (!goodsSkus.isEmpty()) {
this.saveOrUpdateBatch(goodsSkus);
this.updateStock(goodsSkus);
this.updateGoodsStock(goodsSkus);
}
}
@ -199,7 +198,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
skuList.stream().map(BaseEntity::getId).collect(Collectors.toList()));
this.remove(unnecessarySkuIdsQuery);
this.saveOrUpdateBatch(skuList);
this.updateStock(skuList);
this.updateGoodsStock(skuList);
}
}
@ -385,10 +384,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
List<GoodsSku> goodsSkus = this.getGoodsSkuListByGoodsId(goods.getId());
for (GoodsSku sku : goodsSkus) {
cache.remove(GoodsSkuService.getCacheKeys(sku.getId()));
if (GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) && GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
cache.put(GoodsSkuService.getCacheKeys(sku.getId()), sku);
}
}
}
}
/**
* 更新商品sku状态根据店铺id
@ -520,8 +521,23 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Override
@Transactional(rollbackFor = Exception.class)
public void updateStocks(List<GoodsSkuStockDTO> goodsSkuStockDTOS) {
for (GoodsSkuStockDTO goodsSkuStockDTO : goodsSkuStockDTOS) {
this.updateStock(goodsSkuStockDTO.getSkuId(), goodsSkuStockDTO.getQuantity());
List<String> skuIds = goodsSkuStockDTOS.stream().map(GoodsSkuStockDTO::getSkuId).collect(Collectors.toList());
List<GoodsSkuStockDTO> goodsSkuStockList = this.baseMapper.queryStocks(GoodsSearchParams.builder().ids(skuIds).build().queryWrapper());
Map<String, List<GoodsSkuStockDTO>> groupByGoodsIds = goodsSkuStockList.stream().collect(Collectors.groupingBy(GoodsSkuStockDTO::getGoodsId));
//统计每个商品的库存
for (Map.Entry<String, List<GoodsSkuStockDTO>> entry : groupByGoodsIds.entrySet()) {
//库存
Integer quantity = 0;
for (GoodsSkuStockDTO goodsSku : entry.getValue()) {
goodsSkuStockDTOS.stream().filter(i -> i.getSkuId().equals(goodsSku.getSkuId())).findFirst().ifPresent(i -> goodsSku.setQuantity(i.getQuantity()));
if (entry.getKey().equals(goodsSku.getGoodsId())) {
quantity += goodsSku.getQuantity();
}
this.updateStock(goodsSku.getSkuId(), goodsSku.getQuantity());
}
//保存商品库存结果
goodsService.updateStock(entry.getKey(), quantity);
}
}
@ -531,28 +547,23 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId);
if (goodsSku != null) {
//判断商品sku是否已经下架(修改商品库存为0时 会自动下架商品),再次更新商品库存时 需更新商品索引
Boolean isFlag = goodsSku.getQuantity()<= 0;
boolean isFlag = goodsSku.getQuantity() <= 0;
goodsSku.setQuantity(quantity);
boolean update =
this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
boolean update = this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
if (update) {
cache.remove(CachePrefix.GOODS.getPrefix() + goodsSku.getGoodsId());
}
cache.put(GoodsSkuService.getCacheKeys(skuId), goodsSku);
cache.put(GoodsSkuService.getStockCacheKey(skuId), quantity);
//更新商品库存
List<GoodsSku> goodsSkus = new ArrayList<>();
goodsSkus.add(goodsSku);
this.updateGoodsStuck(goodsSkus);
this.promotionGoodsService.updatePromotionGoodsStock(goodsSku.getId(), quantity);
//商品库存为0是删除商品索引
if (quantity <= 0) {
goodsIndexService.deleteIndexById(goodsSku.getId());
}
//商品SKU库存为0并且商品sku状态为上架时更新商品库存
if(isFlag && StrUtil.equals(goodsSku.getMarketEnable(),GoodsStatusEnum.UPPER.name())) {
if (isFlag && CharSequenceUtil.equals(goodsSku.getMarketEnable(), GoodsStatusEnum.UPPER.name())) {
List<String> goodsIds = new ArrayList<>();
goodsIds.add(goodsSku.getGoodsId());
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("更新商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.UPDATE_GOODS_INDEX.name(), goodsIds));
@ -575,22 +586,18 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
@Override
@Transactional(rollbackFor = Exception.class)
public void updateGoodsStuck(List<GoodsSku> goodsSkus) {
Map<String, List<GoodsSku>> groupByGoodsIds =
goodsSkus.stream().collect(Collectors.groupingBy(GoodsSku::getGoodsId));
//获取相关的sku集合
LambdaQueryWrapper<GoodsSku> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(GoodsSku::getGoodsId, groupByGoodsIds.keySet());
List<GoodsSku> goodsSkuList = this.list(lambdaQueryWrapper);
public void updateGoodsStock(List<GoodsSku> goodsSkus) {
Map<String, List<GoodsSku>> groupByGoodsIds = goodsSkus.stream().collect(Collectors.groupingBy(GoodsSku::getGoodsId));
//统计每个商品的库存
for (String goodsId : groupByGoodsIds.keySet()) {
//库存
Integer quantity = 0;
for (GoodsSku goodsSku : goodsSkuList) {
for (GoodsSku goodsSku : goodsSkus) {
if (goodsId.equals(goodsSku.getGoodsId())) {
quantity += goodsSku.getQuantity();
}
this.updateStock(goodsSku.getId(), goodsSku.getQuantity());
}
//保存商品库存结果
goodsService.updateStock(goodsId, quantity);
@ -618,7 +625,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
double grade = NumberUtil.mul(NumberUtil.div(highPraiseNum, goodsSku.getCommentNum().doubleValue(), 2), 100);
goodsSku.setGrade(grade);
//修改规格
this.update(goodsSku);
this.updateGoodsSkuGrade(skuId, grade, goodsSku.getCommentNum());
//修改规格索引,发送mq消息
@ -667,22 +674,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
return this.count(queryWrapper);
}
/**
* 修改库存
*
* @param goodsSkus 商品SKU
*/
private void updateStock(List<GoodsSku> goodsSkus) {
//总库存数量
Integer quantity = 0;
for (GoodsSku sku : goodsSkus) {
this.updateStock(sku.getId(), sku.getQuantity());
quantity += sku.getQuantity();
}
//修改商品库存
goodsService.updateStock(goodsSkus.get(0).getGoodsId(), quantity);
}
/**
* 批量渲染商品sku
@ -700,6 +691,23 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
}
}
@Override
public void updateGoodsSkuBuyCount(String skuId, int buyCount) {
LambdaUpdateWrapper<GoodsSku> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(GoodsSku::getId, skuId);
updateWrapper.set(GoodsSku::getBuyCount, buyCount);
this.update(updateWrapper);
}
@Override
public void updateGoodsSkuGrade(String skuId, double grade, int commentNum) {
LambdaUpdateWrapper<GoodsSku> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(GoodsSku::getId, skuId);
updateWrapper.set(GoodsSku::getGrade, grade);
updateWrapper.set(GoodsSku::getCommentNum, commentNum);
this.update(updateWrapper);
}
/**
* 渲染商品sku
*