修复新增编辑商品时,生成商品索引时,参数没有传递问题。优化商品新增编辑代码

This commit is contained in:
paulGao 2022-08-10 20:27:38 +08:00
parent 002163fbdc
commit 93dd2e06d8
4 changed files with 41 additions and 47 deletions

View File

@ -13,7 +13,6 @@ import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.entity.enums.GoodsTypeEnum; import cn.lili.modules.goods.entity.enums.GoodsTypeEnum;
import cn.lili.mybatis.BaseEntity; import cn.lili.mybatis.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xkcoding.http.util.StringUtil; import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -148,7 +147,6 @@ public class Goods extends BaseEntity {
private String goodsType; private String goodsType;
@ApiModelProperty(value = "商品参数json", hidden = true) @ApiModelProperty(value = "商品参数json", hidden = true)
@JsonIgnore
private String params; private String params;

View File

@ -181,13 +181,6 @@ public interface GoodsSkuService extends IService<GoodsSku> {
*/ */
void updateGoodsSkuStatusByStoreId(String storeId, String marketEnable, String authFlag); void updateGoodsSkuStatusByStoreId(String storeId, String marketEnable, String authFlag);
/**
* 发送生成ES商品索引
*
* @param goods 商品信息
*/
void generateEs(Goods goods);
/** /**
* 更新SKU库存 * 更新SKU库存
* *

View File

@ -7,6 +7,7 @@ import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache; import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix; import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.ResultCode; import cn.lili.common.enums.ResultCode;
import cn.lili.common.event.TransactionCommitSendMQEvent;
import cn.lili.common.exception.ServiceException; import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.AuthUser; import cn.lili.common.security.AuthUser;
@ -50,6 +51,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -106,6 +108,9 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
@Autowired @Autowired
private RocketmqCustomProperties rocketmqCustomProperties; private RocketmqCustomProperties rocketmqCustomProperties;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Autowired @Autowired
private FreightTemplateService freightTemplateService; private FreightTemplateService freightTemplateService;
@ -175,6 +180,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
if (goodsOperationDTO.getGoodsGalleryList() != null && !goodsOperationDTO.getGoodsGalleryList().isEmpty()) { if (goodsOperationDTO.getGoodsGalleryList() != null && !goodsOperationDTO.getGoodsGalleryList().isEmpty()) {
this.goodsGalleryService.add(goodsOperationDTO.getGoodsGalleryList(), goods.getId()); this.goodsGalleryService.add(goodsOperationDTO.getGoodsGalleryList(), goods.getId());
} }
this.generateEs(goods);
} }
@ -203,6 +209,7 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
this.deleteEsGoods(Collections.singletonList(goodsId)); this.deleteEsGoods(Collections.singletonList(goodsId));
} }
cache.remove(CachePrefix.GOODS.getPrefix() + goodsId); cache.remove(CachePrefix.GOODS.getPrefix() + goodsId);
this.generateEs(goods);
} }
@Override @Override
@ -478,17 +485,6 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
} }
/**
* 更新店铺商品数量
*
* @param storeId 信息体
*/
void updateGoodsNum(String storeId) {
Long num = goodsSkuService.countSkuNum(storeId);
storeService.updateStoreGoodsNum(storeId, num);
}
/** /**
* 更新商品状态 * 更新商品状态
* *
@ -506,19 +502,43 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
if (GoodsStatusEnum.DOWN.equals(goodsStatusEnum)) { if (GoodsStatusEnum.DOWN.equals(goodsStatusEnum)) {
this.deleteEsGoods(goodsIds); this.deleteEsGoods(goodsIds);
} else {
this.updateEsGoods(goodsIds);
} }
} }
/**
* 发送生成ES商品索引
*
* @param goods 商品信息
*/
@Transactional
public void generateEs(Goods goods) {
// 不生成没有审核通过且没有上架的商品
if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
return;
}
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("生成商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId()));
}
/**
* 发送生成ES商品索引
*
* @param goodsIds 商品id
*/
@Transactional
public void updateEsGoods(List<String> goodsIds) {
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("更新商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.UPDATE_GOODS_INDEX.name(), goodsIds));
}
/** /**
* 发送删除es索引的信息 * 发送删除es索引的信息
* *
* @param goodsIds 商品id * @param goodsIds 商品id
*/ */
private void deleteEsGoods(List<String> goodsIds) { @Transactional
//商品删除消息 public void deleteEsGoods(List<String> goodsIds) {
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name(); applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("删除商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GOODS_DELETE.name(), JSONUtil.toJsonStr(goodsIds)));
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIds), RocketmqSendCallbackBuilder.commonCallback());
} }
/** /**

View File

@ -43,6 +43,7 @@ import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.service.EsGoodsIndexService; import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.search.utils.EsIndexUtil; import cn.lili.modules.search.utils.EsIndexUtil;
import cn.lili.mybatis.BaseEntity;
import cn.lili.mybatis.util.PageUtil; import cn.lili.mybatis.util.PageUtil;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder; import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.GoodsTagsEnum; import cn.lili.rocketmq.tags.GoodsTagsEnum;
@ -137,7 +138,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
if (!goodsSkus.isEmpty()) { if (!goodsSkus.isEmpty()) {
this.saveOrUpdateBatch(goodsSkus); this.saveOrUpdateBatch(goodsSkus);
this.updateStock(goodsSkus); this.updateStock(goodsSkus);
this.generateEs(goods);
} }
} }
@ -161,7 +161,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId())); cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId()));
} }
this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
//删除sku相册 //删除sku相册
goodsGalleryService.removeByGoodsId(goods.getId()); goodsGalleryService.removeByGoodsId(goods.getId());
@ -182,10 +181,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
} }
} }
if (!skuList.isEmpty()) { if (!skuList.isEmpty()) {
this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId())); LambdaQueryWrapper<GoodsSku> unnecessarySkuIdsQuery = new LambdaQueryWrapper<>();
unnecessarySkuIdsQuery.eq(GoodsSku::getGoodsId, goods.getId());
unnecessarySkuIdsQuery.notIn(GoodsSku::getId, skuList.stream().map(BaseEntity::getId).collect(Collectors.toList()));
this.remove(unnecessarySkuIdsQuery);
this.saveOrUpdateBatch(skuList); this.saveOrUpdateBatch(skuList);
this.updateStock(skuList); this.updateStock(skuList);
this.generateEs(goods);
} }
} }
@ -354,9 +355,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
cache.remove(GoodsSkuService.getCacheKeys(sku.getId())); cache.remove(GoodsSkuService.getCacheKeys(sku.getId()));
cache.put(GoodsSkuService.getCacheKeys(sku.getId()), sku); cache.put(GoodsSkuService.getCacheKeys(sku.getId()), sku);
} }
if (!goodsSkus.isEmpty()) {
this.generateEs(goods);
}
} }
} }
@ -593,21 +591,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
return this.baseMapper.getGoodsSkuIdByGoodsId(goodsId); return this.baseMapper.getGoodsSkuIdByGoodsId(goodsId);
} }
/**
* 发送生成ES商品索引
*
* @param goods 商品信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void generateEs(Goods goods) {
// 不生成没有审核通过且没有上架的商品
if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
return;
}
applicationEventPublisher.publishEvent(new TransactionCommitSendMQEvent("生成商品", rocketmqCustomProperties.getGoodsTopic(), GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId()));
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean deleteAndInsertGoodsSkus(List<GoodsSku> goodsSkus) { public boolean deleteAndInsertGoodsSkus(List<GoodsSku> goodsSkus) {