!91 修复下架商品没有删除商品索引问题。修复下单使用优惠券使金额变为0元时消费服务获取数据不正确。

Merge pull request !91 from OceansDeep/feature/pg
This commit is contained in:
OceansDeep 2021-12-27 10:03:12 +00:00 committed by Gitee
commit bf2fdc2761
16 changed files with 142 additions and 64 deletions

View File

@ -21,6 +21,7 @@ public class MemberWalletExecute implements MemberRegisterEvent {
@Override
public void memberRegister(Member member) {
memberWalletService.save(member.getId(),member.getUsername());
// 有些情况下会同时创建一个member_id的两条数据
// memberWalletService.save(member.getId(),member.getUsername());
}
}

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,25 +33,23 @@ public class OrderCreateReceiptExecute implements TradeEvent {
//获取发票信息
ReceiptVO receiptVO = tradeDTO.getReceiptVO();
//如果需要获取发票则保存发票信息
if (tradeDTO.getNeedReceipt()) {
if (orderList.size() > 0) {
List<Receipt> receipts = new ArrayList<>();
for (OrderVO orderVO : orderList) {
Receipt receipt = new Receipt();
BeanUtil.copyProperties(receiptVO, receipt);
receipt.setMemberId(orderVO.getMemberId());
receipt.setMemberName(orderVO.getMemberName());
receipt.setStoreId(orderVO.getStoreId());
receipt.setStoreName(orderVO.getStoreName());
receipt.setOrderSn(orderVO.getSn());
receipt.setReceiptDetail(JSONUtil.toJsonStr(orderVO.getOrderItems()));
receipt.setReceiptPrice(orderVO.getFlowPrice());
receipt.setReceiptStatus(0);
receipts.add(receipt);
}
//保存发票
receiptService.saveBatch(receipts);
if (Boolean.TRUE.equals(tradeDTO.getNeedReceipt()) && !orderList.isEmpty()) {
List<Receipt> receipts = new ArrayList<>();
for (OrderVO orderVO : orderList) {
Receipt receipt = new Receipt();
BeanUtil.copyProperties(receiptVO, receipt);
receipt.setMemberId(orderVO.getMemberId());
receipt.setMemberName(orderVO.getMemberName());
receipt.setStoreId(orderVO.getStoreId());
receipt.setStoreName(orderVO.getStoreName());
receipt.setOrderSn(orderVO.getSn());
receipt.setReceiptDetail(JSONUtil.toJsonStr(orderVO.getOrderItems()));
receipt.setReceiptPrice(orderVO.getFlowPrice());
receipt.setReceiptStatus(0);
receipts.add(receipt);
}
//保存发票
receiptService.saveBatch(receipts);
}
}
}

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

@ -44,7 +44,7 @@ public class PromotionEverydayExecute implements EveryDayExecute {
*/
@Override
public void execute() {
//查询条件
//清除所以商品索引的无效促销活动
this.esGoodsIndexService.cleanInvalidPromotion();
//定时创建活动
addSeckill();

View File

@ -71,7 +71,7 @@ public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Bean("elasticsearchRestTemplate")
public ElasticsearchRestTemplate elasticsearchRestTemplate() {
return new ElasticsearchRestTemplate(elasticsearchClient());
return new ElasticsearchRestTemplate(this.client);
}
private HttpHost[] getHttpHosts() {

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,12 +359,13 @@ 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());
}
//商品删除消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_DELETE.name();
//发送mq消息
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

@ -93,8 +93,8 @@ public class CouponSearchParams extends BasePromotionsSearchParams implements Se
queryWrapper.nested(i -> i.gt(PromotionTools.START_TIME_COLUMN, new Date()).gt(PromotionTools.END_TIME_COLUMN, new Date()));
break;
case START:
queryWrapper.nested(i -> i.le(PromotionTools.START_TIME_COLUMN, new Date()).ge(PromotionTools.END_TIME_COLUMN, new Date()));
// .or(i -> i.gt("effective_days", 0).eq(RANGE_DAY_TYPE_COLUMN, CouponRangeDayEnum.DYNAMICTIME.name()));
queryWrapper.nested(i -> i.le(PromotionTools.START_TIME_COLUMN, new Date()).ge(PromotionTools.END_TIME_COLUMN, new Date()))
.or(i -> i.gt("effective_days", 0).eq(RANGE_DAY_TYPE_COLUMN, CouponRangeDayEnum.DYNAMICTIME.name()));
break;
case END:
queryWrapper.nested(i -> i.lt(PromotionTools.START_TIME_COLUMN, new Date()).lt(PromotionTools.END_TIME_COLUMN, new Date()));

View File

@ -15,8 +15,6 @@ import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum;
import cn.lili.modules.promotion.entity.vos.PromotionGoodsSearchParams;
import cn.lili.modules.promotion.entity.vos.SeckillSearchParams;
import cn.lili.modules.promotion.mapper.PromotionGoodsMapper;
import cn.lili.modules.promotion.service.CouponService;
import cn.lili.modules.promotion.service.FullDiscountService;
import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.promotion.service.SeckillApplyService;
import cn.lili.modules.promotion.tools.PromotionTools;
@ -46,6 +44,8 @@ import java.util.List;
@Transactional(rollbackFor = Exception.class)
public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsMapper, PromotionGoods> implements PromotionGoodsService {
private static final String SKU_ID_COLUMN = "sku_id";
/**
* Redis
*/
@ -62,12 +62,6 @@ public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsMapper,
@Autowired
private GoodsSkuService goodsSkuService;
@Autowired
private FullDiscountService fullDiscountService;
@Autowired
private CouponService couponService;
@Override
public List<PromotionGoods> findSkuValidPromotion(String skuId, String storeIds) {
@ -77,7 +71,7 @@ public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsMapper,
}
QueryWrapper<PromotionGoods> queryWrapper = new QueryWrapper<>();
queryWrapper.and(i -> i.or(j -> j.eq("sku_id", skuId))
queryWrapper.and(i -> i.or(j -> j.eq(SKU_ID_COLUMN, skuId))
.or(n -> n.eq("scope_type", PromotionsScopeTypeEnum.ALL.name()))
.or(n -> n.and(k -> k.eq("scope_type", PromotionsScopeTypeEnum.PORTION_GOODS_CATEGORY.name())
.and(l -> l.like("scope_id", sku.getCategoryPath())))));
@ -123,7 +117,7 @@ public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsMapper,
@Override
public PromotionGoods getValidPromotionsGoods(String skuId, List<String> promotionTypes) {
QueryWrapper<PromotionGoods> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sku_id", skuId);
queryWrapper.eq(SKU_ID_COLUMN, skuId);
queryWrapper.in("promotion_type", promotionTypes);
queryWrapper.and(PromotionTools.queryPromotionStatus(PromotionsStatusEnum.START));
return this.getOne(queryWrapper, false);
@ -139,7 +133,7 @@ public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsMapper,
@Override
public Double getValidPromotionsGoodsPrice(String skuId, List<String> promotionTypes) {
QueryWrapper<PromotionGoods> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sku_id", skuId);
queryWrapper.eq(SKU_ID_COLUMN, skuId);
queryWrapper.in("promotion_type", promotionTypes);
queryWrapper.and(PromotionTools.queryPromotionStatus(PromotionsStatusEnum.START));
return this.baseMapper.selectPromotionsGoodsPrice(queryWrapper);

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()) {
@ -480,17 +500,13 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
//获取商品索引
if (promotionMap != null && !promotionMap.isEmpty()) {
//促销不为空则进行清洗
for (Map.Entry<String, Object> entry : promotionMap.entrySet()) {
BasePromotions promotion = (BasePromotions) entry.getValue();
//判定条件为活动已结束
if (promotion.getEndTime() != null && promotion.getEndTime().getTime() < DateUtil.date().getTime()) {
if (entry.getKey().contains(PromotionTypeEnum.SECKILL.name()) || entry.getKey().contains(PromotionTypeEnum.PINTUAN.name())) {
goodsIndex.setPromotionPrice(goodsIndex.getPrice());
}
promotionMap.remove(entry.getKey());
promotionMap.entrySet().removeIf(i -> {
BasePromotions promotion = (BasePromotions) i.getValue();
if (i.getKey().contains(PromotionTypeEnum.SECKILL.name()) || i.getKey().contains(PromotionTypeEnum.PINTUAN.name())) {
goodsIndex.setPromotionPrice(goodsIndex.getPrice());
}
}
return promotion.getEndTime() != null && promotion.getEndTime().getTime() < DateUtil.date().getTime();
});
}
}
goodsIndexRepository.saveAll(all);

View File

@ -80,6 +80,12 @@ class EsTest {
}
@Test
void cleanInvalidPromotion() {
this.esGoodsIndexService.cleanInvalidPromotion();
Assertions.assertTrue(true);
}
@Test
void searchGoods() {
EsGoodsSearchDTO goodsSearchDTO = new EsGoodsSearchDTO();