修复下架商品没有删除商品索引问题。修复下单使用优惠券使金额变为0元时消费服务获取数据不正确导致没有及时更新订单状态问题。

This commit is contained in:
paulGao 2021-12-27 18:01:58 +08:00
parent e7563203fe
commit 168c630a13
10 changed files with 119 additions and 38 deletions

View File

@ -8,7 +8,6 @@ import cn.lili.modules.order.order.entity.dos.Receipt;
import cn.lili.modules.order.order.entity.vo.OrderVO;
import cn.lili.modules.order.order.entity.vo.ReceiptVO;
import cn.lili.modules.order.order.service.ReceiptService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -34,8 +33,7 @@ public class OrderCreateReceiptExecute implements TradeEvent {
//获取发票信息
ReceiptVO receiptVO = tradeDTO.getReceiptVO();
//如果需要获取发票则保存发票信息
if (tradeDTO.getNeedReceipt()) {
if (orderList.size() > 0) {
if (Boolean.TRUE.equals(tradeDTO.getNeedReceipt()) && !orderList.isEmpty()) {
List<Receipt> receipts = new ArrayList<>();
for (OrderVO orderVO : orderList) {
Receipt receipt = new Receipt();
@ -55,4 +53,3 @@ public class OrderCreateReceiptExecute implements TradeEvent {
}
}
}
}

View File

@ -186,12 +186,28 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
break;
//审核商品
case GOODS_AUDIT:
updateGoodsNum(messageExt);
Goods goods = JSONUtil.toBean(new String(messageExt.getBody()), Goods.class);
updateGoodsNum(goods);
break;
//删除商品
case GOODS_DELETE:
deleteGoods(messageExt);
updateGoodsNum(messageExt);
try {
String goodsIdsJsonStr = new String(messageExt.getBody());
for (String goodsId : JSONUtil.toList(goodsIdsJsonStr, String.class)) {
Goods goodsById = this.goodsService.getById(goodsId);
if (goodsById != null) {
this.deleteGoods(goodsById);
this.updateGoodsNum(goodsById);
List<String> skuIdsByGoodsId = this.goodsSkuService.getSkuIdsByGoodsId(goodsId);
if (skuIdsByGoodsId != null && !skuIdsByGoodsId.isEmpty()) {
this.goodsIndexService.deleteIndexByIds(skuIdsByGoodsId);
}
}
}
} catch (Exception e) {
log.error("删除商品索引事件执行异常,商品信息 {}", new String(messageExt.getBody()));
}
break;
//规格删除
case SKU_DELETE:
@ -380,10 +396,9 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
* 2.删除分销员-分销商品绑定关系
* 3.删除分销商品
*
* @param messageExt 消息
* @param goods 消息
*/
private void deleteGoods(MessageExt messageExt) {
Goods goods = JSONUtil.toBean(new String(messageExt.getBody()), Goods.class);
private void deleteGoods(Goods goods) {
DistributionGoodsSearchParams searchParams = new DistributionGoodsSearchParams();
searchParams.setGoodsId(goods.getId());
@ -403,17 +418,15 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
/**
* 修改商品数量
*
* @param messageExt 信息体
* @param goods 信息体
*/
private void updateGoodsNum(MessageExt messageExt) {
private void updateGoodsNum(Goods goods) {
try {
Goods goods = JSONUtil.toBean(new String(messageExt.getBody()), Goods.class);
//更新店铺商品数量
assert goods != null;
storeService.updateStoreGoodsNum(goods.getStoreId());
} catch (Exception e) {
log.error("商品MQ信息错误{}", messageExt.toString());
log.error("修改商品数量错误");
}
}

View File

@ -62,7 +62,7 @@ public class OrderMessageListener implements RocketMQListener<MessageExt> {
//订单创建
case ORDER_CREATE:
String key = new String(messageExt.getBody());
TradeDTO tradeDTO = (TradeDTO) cache.get(key);
TradeDTO tradeDTO = JSONUtil.toBean(cache.getString(key), TradeDTO.class);
boolean result = true;
for (TradeEvent event : tradeEvent) {
try {

View File

@ -2,6 +2,9 @@ package cn.lili.modules.goods.mapper;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 规格项数据处理层
@ -11,4 +14,13 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/
public interface GoodsSkuMapper extends BaseMapper<GoodsSku> {
/**
* 根据商品id获取全部skuId的集合
*
* @param goodsId goodsId
* @return 全部skuId的集合
*/
@Select("SELECT id FROM li_goods_sku WHERE goods_id = #{goodsId}")
List<String> getGoodsSkuIdByGoodsId(String goodsId);
}

View File

@ -202,4 +202,12 @@ public interface GoodsSkuService extends IService<GoodsSku> {
* @param promotionPrice 促销价格
*/
void updateGoodsSkuPromotion(String skuId, Double promotionPrice);
/**
* 根据商品id获取全部skuId的集合
*
* @param goodsId goodsId
* @return 全部skuId的集合
*/
List<String> getSkuIdsByGoodsId(String goodsId);
}

View File

@ -304,6 +304,14 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
for (Goods goods : goodsList) {
goodsSkuService.updateGoodsSkuStatus(goods);
}
if (GoodsStatusEnum.DOWN.equals(goodsStatusEnum)) {
//商品删除消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIds), RocketmqSendCallbackBuilder.commonCallback());
}
return result;
}
@ -351,11 +359,12 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
for (Goods goods : goodsList) {
//修改SKU状态
goodsSkuService.updateGoodsSkuStatus(goods);
}
//商品删除消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goods), RocketmqSendCallbackBuilder.commonCallback());
}
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIds), RocketmqSendCallbackBuilder.commonCallback());
return true;
}

View File

@ -2,6 +2,7 @@ package cn.lili.modules.goods.serviceimpl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
@ -13,7 +14,6 @@ import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
@ -221,7 +221,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
//获取商品VO
GoodsVO goodsVO = goodsService.getGoodsVO(goodsId);
//如果skuid为空则使用商品VO中sku信息获取
if (StringUtils.isEmpty(skuId) || "undefined".equals(skuId)) {
if (CharSequenceUtil.isEmpty(skuId) || "undefined".equals(skuId)) {
skuId = goodsVO.getSkuList().get(0).getId();
}
//从缓存拿商品Sku
@ -532,6 +532,17 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
cache.remove(GoodsSkuService.getCacheKeys(skuId));
}
/**
* 根据商品id获取全部skuId的集合
*
* @param goodsId goodsId
* @return 全部skuId的集合
*/
@Override
public List<String> getSkuIdsByGoodsId(String goodsId) {
return this.baseMapper.getGoodsSkuIdByGoodsId(goodsId);
}
/**
* 发送生成ES商品索引
*
@ -539,6 +550,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
*/
@Override
public void generateEs(Goods goods) {
// 不生成没有审核通过且没有上架的商品
if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
return;
}
ThreadUtil.execAsync(() -> {
try {
// 延时执行防止商品未保存完成就去生成商品索引导致生成索引时找不到商品问题

File diff suppressed because one or more lines are too long

View File

@ -79,6 +79,13 @@ public interface EsGoodsIndexService {
*/
void deleteIndexById(String id);
/**
* 删除索引
*
* @param ids 商品索引id集合
*/
void deleteIndexByIds(List<String> ids);
/**
* 初始化商品索引
*

View File

@ -49,8 +49,11 @@ import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.script.Script;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@ -100,6 +103,10 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Autowired
private Cache<Object> cache;
@Autowired
@Qualifier("elasticsearchRestTemplate")
private ElasticsearchRestTemplate restTemplate;
@Override
public void init() {
//获取索引任务标识
@ -286,6 +293,19 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
goodsIndexRepository.deleteById(id);
}
/**
* 删除索引
*
* @param ids 商品索引id集合
*/
@Override
public void deleteIndexByIds(List<String> ids) {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withQuery(QueryBuilders.termsQuery("id", ids.toArray()));
this.restTemplate.delete(queryBuilder.build(), EsGoodsIndex.class);
}
@Override
public void initIndex(List<EsGoodsIndex> goodsIndexList) {
if (goodsIndexList == null || goodsIndexList.isEmpty()) {