优化促销相关代码,修复bug

This commit is contained in:
paulGao 2021-12-16 15:02:06 +08:00
parent f0b43ddd32
commit 7dcdb43acf
15 changed files with 231 additions and 137 deletions

View File

@ -504,8 +504,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
//修改规格索引,发送mq消息
Map<String, Object> updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap(
MapUtil.builder().put("id", goodsSku.getId()).build(),
MapUtil.builder().put("commentNum", goodsSku.getCommentNum()).put("highPraiseNum", highPraiseNum)
MapUtil.builder(new HashMap<String, Object>()).put("id", goodsSku.getId()).build(),
MapUtil.builder(new HashMap<String, Object>()).put("commentNum", goodsSku.getCommentNum()).put("highPraiseNum", highPraiseNum)
.put("grade", grade).build());
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback());

View File

@ -274,4 +274,14 @@ public interface OrderService extends IService<Order> {
* @return 订单支付记录分页
*/
IPage<PaymentLog> queryPaymentLogs(IPage<PaymentLog> page, Wrapper<PaymentLog> queryWrapper);
/**
* 检查是否开始虚拟成团
*
* @param pintuanId 拼团活动id
* @param requiredNum 成团人数
* @param fictitious 是否开启成团
* @return 是否成功
*/
boolean checkFictitiousOrder(String pintuanId, Integer requiredNum, Boolean fictitious);
}

View File

@ -74,10 +74,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
* 子订单业务层实现
@ -684,6 +682,77 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
/**
* 检查是否开始虚拟成团
*
* @param pintuanId 拼团活动id
* @param requiredNum 成团人数
* @param fictitious 是否开启成团
* @return 是否成功
*/
@Override
public boolean checkFictitiousOrder(String pintuanId, Integer requiredNum, Boolean fictitious) {
Map<String, List<Order>> collect = this.queryListByPromotion(pintuanId)
.stream().filter(i -> CharSequenceUtil.isNotEmpty(i.getParentOrderSn()))
.collect(Collectors.groupingBy(Order::getParentOrderSn));
for (Map.Entry<String, List<Order>> entry : collect.entrySet()) {
//是否开启虚拟成团
if (Boolean.FALSE.equals(fictitious) && entry.getValue().size() < requiredNum) {
//如果未开启虚拟成团且已参团人数小于成团人数则自动取消订单
String reason = "拼团活动结束订单未付款,系统自动取消订单";
if (CharSequenceUtil.isNotEmpty(entry.getKey())) {
this.systemCancel(entry.getKey(), reason);
} else {
for (Order order : entry.getValue()) {
this.systemCancel(order.getSn(), reason);
}
}
} else if (Boolean.TRUE.equals(fictitious)) {
this.fictitiousPintuan(entry, requiredNum);
}
}
return false;
}
/**
* 虚拟成团
*
* @param entry 订单列表
* @param requiredNum 必须参团人数
*/
private void fictitiousPintuan(Map.Entry<String, List<Order>> entry, Integer requiredNum) {
Map<String, List<Order>> listMap = entry.getValue().stream().collect(Collectors.groupingBy(Order::getPayStatus));
//未付款订单
List<Order> unpaidOrders = listMap.get(PayStatusEnum.UNPAID.name());
//未付款订单自动取消
if (unpaidOrders != null && !unpaidOrders.isEmpty()) {
for (Order unpaidOrder : unpaidOrders) {
this.systemCancel(unpaidOrder.getSn(), "拼团活动结束订单未付款,系统自动取消订单");
}
}
List<Order> paidOrders = listMap.get(PayStatusEnum.PAID.name());
//如待参团人数大于0并已开启虚拟成团
if (!paidOrders.isEmpty()) {
//待参团人数
int waitNum = requiredNum - paidOrders.size();
//添加虚拟成团
for (int i = 0; i < waitNum; i++) {
Order order = new Order();
BeanUtil.copyProperties(paidOrders.get(0), order);
order.setMemberId("-1");
order.setMemberName("参团人员");
order.setDeleteFlag(true);
this.save(order);
paidOrders.add(order);
}
for (Order paidOrder : paidOrders) {
paidOrder.setOrderStatus(OrderStatusEnum.UNDELIVERED.name());
orderStatusMessage(paidOrder);
}
}
}
/**
* 订单状态变更消息
*

View File

@ -42,15 +42,31 @@ public interface SeckillApplyService extends IService<SeckillApply> {
* @param pageVo 分页参数
* @return 限时请购申请列表
*/
IPage<SeckillApply> getSeckillApply(SeckillSearchParams queryParam, PageVO pageVo);
IPage<SeckillApply> getSeckillApplyPage(SeckillSearchParams queryParam, PageVO pageVo);
/**
* 分页查询限时请购申请列表
* 查询限时请购申请列表
*
* @param queryParam 秒杀活动申请查询参数
* @return 限时请购申请列表
*/
List<SeckillApply> getSeckillApply(SeckillSearchParams queryParam);
List<SeckillApply> getSeckillApplyList(SeckillSearchParams queryParam);
/**
* 查询限时请购申请列表总数
*
* @param queryParam 查询条件
* @return 限时请购申请列表总数
*/
long getSeckillApplyCount(SeckillSearchParams queryParam);
/**
* 查询限时请购申请
*
* @param queryParam 秒杀活动申请查询参数
* @return 限时请购申请
*/
SeckillApply getSeckillApply(SeckillSearchParams queryParam);
/**
* 添加秒杀活动申请
@ -72,4 +88,13 @@ public interface SeckillApplyService extends IService<SeckillApply> {
*/
void removeSeckillApply(String seckillId, String id);
/**
* 更新秒杀商品库存
*
* @param seckillId 秒杀活动id
* @param skuId 商品skuId
* @param quantity 库存
*/
void updateSeckillApplyQuantity(String seckillId, String skuId, Integer quantity);
}

View File

@ -2,7 +2,6 @@ package cn.lili.modules.promotion.serviceimpl;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties;
@ -32,7 +31,7 @@ import java.util.*;
* @author paulG
* @since 2021/11/30
**/
public class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T extends BasePromotions> extends ServiceImpl<M, T> implements AbstractPromotionsService<T> {
public abstract class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T extends BasePromotions> extends ServiceImpl<M, T> implements AbstractPromotionsService<T> {
/**
* 促销商品
@ -264,13 +263,4 @@ public class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T extends Ba
}
}
/**
* 当前促销类型
*
* @return 当前促销类型
*/
@Override
public PromotionTypeEnum getPromotionType() {
return null;
}
}

View File

@ -1,6 +1,5 @@
package cn.lili.modules.promotion.serviceimpl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.PromotionTypeEnum;
@ -34,8 +33,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 拼团业务层实现
@ -201,10 +198,7 @@ public class PintuanServiceImpl extends AbstractPromotionsServiceImpl<PintuanMap
}
if (promotions.getEndTime() == null && promotions.getStartTime() == null) {
//过滤父级拼团订单根据父级拼团订单分组
Map<String, List<Order>> collect = orderService.queryListByPromotion(promotions.getId())
.stream().filter(i -> CharSequenceUtil.isNotEmpty(i.getParentOrderSn()))
.collect(Collectors.groupingBy(Order::getParentOrderSn));
this.isOpenFictitiousPintuan(promotions, collect);
this.orderService.checkFictitiousOrder(promotions.getId(), promotions.getRequiredNum(), promotions.getFictitious());
}
}
@ -268,71 +262,6 @@ public class PintuanServiceImpl extends AbstractPromotionsServiceImpl<PintuanMap
memberVO.setToBeGroupedNum(toBoGrouped);
}
/**
* 从指定订单列表中检查是否开始虚拟成团
*
* @param pintuan 拼团活动信息
* @param collect 检查的订单列表
*/
private void isOpenFictitiousPintuan(Pintuan pintuan, Map<String, List<Order>> collect) {
//成团人数
Integer requiredNum = pintuan.getRequiredNum();
for (Map.Entry<String, List<Order>> entry : collect.entrySet()) {
//是否开启虚拟成团
if (Boolean.FALSE.equals(pintuan.getFictitious()) && entry.getValue().size() < requiredNum) {
//如果未开启虚拟成团且已参团人数小于成团人数则自动取消订单
String reason = "拼团活动结束订单未付款,系统自动取消订单";
if (CharSequenceUtil.isNotEmpty(entry.getKey())) {
this.orderService.systemCancel(entry.getKey(), reason);
} else {
for (Order order : entry.getValue()) {
this.orderService.systemCancel(order.getSn(), reason);
}
}
} else if (Boolean.TRUE.equals(pintuan.getFictitious())) {
this.fictitiousPintuan(entry, requiredNum);
}
}
}
/**
* 虚拟成团
*
* @param entry 订单列表
* @param requiredNum 必须参团人数
*/
private void fictitiousPintuan(Map.Entry<String, List<Order>> entry, Integer requiredNum) {
Map<String, List<Order>> listMap = entry.getValue().stream().collect(Collectors.groupingBy(Order::getPayStatus));
//未付款订单
List<Order> unpaidOrders = listMap.get(PayStatusEnum.UNPAID.name());
//未付款订单自动取消
if (unpaidOrders != null && !unpaidOrders.isEmpty()) {
for (Order unpaidOrder : unpaidOrders) {
orderService.systemCancel(unpaidOrder.getSn(), "拼团活动结束订单未付款,系统自动取消订单");
}
}
List<Order> paidOrders = listMap.get(PayStatusEnum.PAID.name());
//如待参团人数大于0并已开启虚拟成团
if (!paidOrders.isEmpty()) {
//待参团人数
int waitNum = requiredNum - paidOrders.size();
//添加虚拟成团
for (int i = 0; i < waitNum; i++) {
Order order = new Order();
BeanUtil.copyProperties(paidOrders.get(0), order);
order.setMemberId("-1");
order.setMemberName("参团人员");
orderService.save(order);
paidOrders.add(order);
}
for (Order paidOrder : paidOrders) {
paidOrder.setOrderStatus(OrderStatusEnum.UNDELIVERED.name());
}
orderService.updateBatchById(paidOrders);
}
}
/**
* 更新记录的促销商品信息
*

View File

@ -2,9 +2,11 @@ package cn.lili.modules.promotion.serviceimpl;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.promotion.entity.dos.PointsGoods;
@ -15,10 +17,13 @@ import cn.lili.modules.promotion.mapper.PointsGoodsMapper;
import cn.lili.modules.promotion.service.PointsGoodsService;
import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.promotion.tools.PromotionTools;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.search.utils.EsIndexUtil;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.GoodsTagsEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -51,14 +56,22 @@ public class PointsGoodsServiceImpl extends AbstractPromotionsServiceImpl<Points
@Autowired
private GoodsSkuService goodsSkuService;
/**
* rocketMq配置
*/
@Autowired
private EsGoodsIndexService goodsIndexService;
private RocketmqCustomProperties rocketmqCustomProperties;
/**
* rocketMq
*/
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Override
public boolean savePointsGoodsBatch(List<PointsGoods> promotionsList) {
List<PromotionGoods> promotionGoodsList = new ArrayList<>();
Map<String, Long> skuPoints = new HashMap<>();
for (PointsGoods pointsGoods : promotionsList) {
this.initPromotion(pointsGoods);
this.checkPromotions(pointsGoods);
@ -73,16 +86,13 @@ public class PointsGoodsServiceImpl extends AbstractPromotionsServiceImpl<Points
PromotionGoods promotionGoods = new PromotionGoods(pointsGoods, goodsSku);
promotionGoods.setPromotionType(PromotionTypeEnum.POINTS_GOODS.name());
promotionGoodsList.add(promotionGoods);
skuPoints.put(pointsGoods.getSkuId(), pointsGoods.getPoints());
}
boolean saveBatch = this.saveBatch(promotionsList);
if (saveBatch) {
this.promotionGoodsService.saveOrUpdateBatch(promotionGoodsList);
for (Map.Entry<String, Long> entry : skuPoints.entrySet()) {
Map<String, Object> query = MapUtil.builder(new HashMap<String, Object>()).put("id", entry.getKey()).build();
Map<String, Object> update = MapUtil.builder(new HashMap<String, Object>()).put("points", entry.getValue()).build();
this.goodsIndexService.updateIndex(query, update);
for (PointsGoods pointsGoods : promotionsList) {
this.updateEsGoodsIndex(pointsGoods);
}
}
@ -205,7 +215,10 @@ public class PointsGoodsServiceImpl extends AbstractPromotionsServiceImpl<Points
public void updateEsGoodsIndex(PointsGoods promotions) {
Map<String, Object> query = MapUtil.builder(new HashMap<String, Object>()).put("id", promotions.getSkuId()).build();
Map<String, Object> update = MapUtil.builder(new HashMap<String, Object>()).put("points", promotions.getPoints()).build();
this.goodsIndexService.updateIndex(query, update);
//修改规格索引,发送mq消息
Map<String, Object> updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap(query, update);
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback());
}

View File

@ -18,6 +18,7 @@ import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum;
import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum;
import cn.lili.modules.promotion.entity.vos.BasePromotionsSearchParams;
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;
@ -264,16 +265,14 @@ public class PromotionGoodsServiceImpl extends ServiceImpl<PromotionGoodsMapper,
public void updatePromotionGoodsStock(PromotionTypeEnum typeEnum, String promotionId, String skuId, Integer quantity) {
String promotionStockKey = PromotionGoodsService.getPromotionGoodsStockCacheKey(typeEnum, promotionId, skuId);
if (typeEnum.equals(PromotionTypeEnum.SECKILL)) {
LambdaQueryWrapper<SeckillApply> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SeckillApply::getSeckillId, promotionId).eq(SeckillApply::getSkuId, skuId);
SeckillApply seckillApply = this.seckillApplyService.getOne(queryWrapper, false);
SeckillSearchParams searchParams = new SeckillSearchParams();
searchParams.setSeckillId(promotionId);
searchParams.setSkuId(skuId);
SeckillApply seckillApply = this.seckillApplyService.getSeckillApply(searchParams);
if (seckillApply == null) {
throw new ServiceException(ResultCode.SECKILL_NOT_EXIST_ERROR);
}
LambdaUpdateWrapper<SeckillApply> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SeckillApply::getSeckillId, promotionId).eq(SeckillApply::getSkuId, skuId);
updateWrapper.set(SeckillApply::getQuantity, quantity);
seckillApplyService.update(updateWrapper);
seckillApplyService.updateSeckillApplyQuantity(promotionId, skuId, quantity);
} else {
LambdaUpdateWrapper<PromotionGoods> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(PromotionGoods::getPromotionType, typeEnum.name()).eq(PromotionGoods::getPromotionId, promotionId).eq(PromotionGoods::getSkuId, skuId);

View File

@ -1,7 +1,6 @@
package cn.lili.modules.promotion.serviceimpl;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.promotion.entity.dos.*;
import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum;
import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum;
@ -64,9 +63,6 @@ public class PromotionServiceImpl implements PromotionService {
@Autowired
private PointsGoodsService pointsGoodsService;
@Autowired
private GoodsSkuService goodsSkuService;
/**
* 获取当前进行的所有促销活动信息
@ -176,7 +172,7 @@ public class PromotionServiceImpl implements PromotionService {
SeckillSearchParams searchParams = new SeckillSearchParams();
searchParams.setSeckillId(promotionGoods.getPromotionId());
searchParams.setSkuId(promotionGoods.getSkuId());
List<SeckillApply> seckillApplyList = seckillApplyService.getSeckillApply(searchParams);
List<SeckillApply> seckillApplyList = seckillApplyService.getSeckillApplyList(searchParams);
if (seckillApplyList != null && !seckillApplyList.isEmpty()) {
SeckillApply seckillApply = seckillApplyList.get(0);
int nextHour = 23;

View File

@ -27,6 +27,7 @@ import cn.lili.modules.promotion.tools.PromotionCacheKeys;
import cn.lili.modules.promotion.tools.PromotionTools;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
@ -100,7 +101,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
}
@Override
public IPage<SeckillApply> getSeckillApply(SeckillSearchParams queryParam, PageVO pageVo) {
public IPage<SeckillApply> getSeckillApplyPage(SeckillSearchParams queryParam, PageVO pageVo) {
IPage<SeckillApply> seckillApplyPage = this.page(PageUtil.initPage(pageVo), queryParam.queryWrapper());
if (seckillApplyPage != null && !seckillApplyPage.getRecords().isEmpty()) {
@ -126,10 +127,32 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
* @return 限时请购申请列表
*/
@Override
public List<SeckillApply> getSeckillApply(SeckillSearchParams queryParam) {
public List<SeckillApply> getSeckillApplyList(SeckillSearchParams queryParam) {
return this.list(queryParam.queryWrapper());
}
/**
* 查询限时请购申请列表总数
*
* @param queryParam 查询条件
* @return 限时请购申请列表总数
*/
@Override
public long getSeckillApplyCount(SeckillSearchParams queryParam) {
return this.count(queryParam.queryWrapper());
}
/**
* 查询限时请购申请
*
* @param queryParam 秒杀活动申请查询参数
* @return 限时请购申请
*/
@Override
public SeckillApply getSeckillApply(SeckillSearchParams queryParam) {
return this.getOne(queryParam.queryWrapper(), false);
}
@Override
public void addSeckillApply(String seckillId, String storeId, List<SeckillApplyVO> seckillApplyList) {
Seckill seckill = this.seckillService.getById(seckillId);
@ -187,7 +210,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
* 批量删除秒杀活动申请
*
* @param seckillId 秒杀活动活动id
* @param id id
* @param id id
*/
@Override
public void removeSeckillApply(String seckillId, String id) {
@ -210,6 +233,21 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
this.promotionGoodsService.deletePromotionGoods(seckillId, Collections.singletonList(seckillApply.getSkuId()));
}
/**
* 更新秒杀商品库存
*
* @param seckillId 秒杀活动id
* @param skuId 商品skuId
* @param quantity 库存
*/
@Override
public void updateSeckillApplyQuantity(String seckillId, String skuId, Integer quantity) {
LambdaUpdateWrapper<SeckillApply> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SeckillApply::getSeckillId, seckillId).eq(SeckillApply::getSkuId, skuId);
updateWrapper.set(SeckillApply::getQuantity, quantity);
this.update(updateWrapper);
}
/**
* 检查秒杀活动申请列表参数信息
*

View File

@ -4,34 +4,41 @@ import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.modules.promotion.entity.dos.Seckill;
import cn.lili.modules.promotion.entity.dos.SeckillApply;
import cn.lili.modules.promotion.entity.enums.PromotionsApplyStatusEnum;
import cn.lili.modules.promotion.entity.vos.SeckillSearchParams;
import cn.lili.modules.promotion.entity.vos.SeckillVO;
import cn.lili.modules.promotion.mapper.SeckillMapper;
import cn.lili.modules.promotion.service.SeckillApplyService;
import cn.lili.modules.promotion.service.SeckillService;
import cn.lili.modules.promotion.tools.PromotionTools;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.SeckillSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import cn.lili.rocketmq.RocketmqSendCallbackBuilder;
import cn.lili.rocketmq.tags.GoodsTagsEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 秒杀活动业务层实现
@ -44,11 +51,6 @@ import java.util.List;
@Slf4j
public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMapper, Seckill> implements SeckillService {
/**
* 商品索引
*/
@Autowired
private EsGoodsIndexService goodsIndexService;
/**
* 设置
*/
@ -58,13 +60,27 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
@Autowired
private SeckillApplyService seckillApplyService;
/**
* rocketMq配置
*/
@Autowired
private RocketmqCustomProperties rocketmqCustomProperties;
/**
* rocketMq
*/
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Override
public SeckillVO getSeckillDetail(String id) {
Seckill seckill = this.checkSeckillExist(id);
SeckillVO seckillVO = new SeckillVO();
BeanUtils.copyProperties(seckill, seckillVO);
seckillVO.setSeckillApplyList(this.seckillApplyService.list(new LambdaQueryWrapper<SeckillApply>().eq(SeckillApply::getSeckillId, id)));
SeckillSearchParams searchParams = new SeckillSearchParams();
searchParams.setSeckillId(id);
seckillVO.setSeckillApplyList(this.seckillApplyService.getSeckillApplyList(searchParams));
return seckillVO;
}
@ -76,7 +92,7 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
for (Seckill seckill : seckillList) {
seckill.setStartTime(null);
seckill.setEndTime(null);
this.goodsIndexService.updateEsGoodsIndexAllByList(seckill, PromotionTypeEnum.SECKILL.name() + "-" + seckill.getId());
this.updateEsGoodsIndex(seckill);
}
this.remove(new QueryWrapper<>());
@ -103,10 +119,12 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
public void updateSeckillGoodsNum(String seckillId) {
Seckill seckill = this.getById(seckillId);
if (seckill != null) {
SeckillSearchParams searchParams = new SeckillSearchParams();
searchParams.setSeckillId(seckillId);
LambdaUpdateWrapper<Seckill> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(Seckill::getId, seckillId);
updateWrapper.set(Seckill::getGoodsNum,
this.seckillApplyService.count(new LambdaQueryWrapper<SeckillApply>().eq(SeckillApply::getSeckillId, seckillId)));
this.seckillApplyService.getSeckillApplyCount(searchParams));
this.update(updateWrapper);
}
@ -126,7 +144,17 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
this.setSeckillApplyTime(seckill, seckillApply);
log.info("更新限时抢购商品状态:{}", seckill);
String promotionKey = PromotionTypeEnum.SECKILL.name() + "-" + seckillApply.getTimeLine();
this.goodsIndexService.updateEsGoodsIndexPromotions(seckillApply.getSkuId(), seckill, promotionKey, seckillApply.getPrice());
Map<String, Object> map = new HashMap<>();
// es促销key
map.put("esPromotionKey", promotionKey);
// 促销类型全路径名
map.put("promotionsType", Seckill.class.getName());
// 促销实体
map.put("promotions", seckill);
//更新商品促销消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_PROMOTIONS.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(map), RocketmqSendCallbackBuilder.commonCallback());
}
}
}

View File

@ -16,8 +16,8 @@ public class EsIndexUtil {
private static final String IGNORE_FIELD = "serialVersionUID,promotionMap,id,goodsId";
public static Map<String, Object> getUpdateIndexFieldsMap(EsGoodsIndex queryGoodsIndex, EsGoodsIndex updateGoodsIndex) {
Map<Object, Object> queryFieldsMap = new HashMap<>();
Map<Object, Object> updateFieldsMap = new HashMap<>();
Map<String, Object> queryFieldsMap = new HashMap<>();
Map<String, Object> updateFieldsMap = new HashMap<>();
for (Map.Entry<String, Field> entry : ReflectUtil.getFieldMap(EsGoodsIndex.class).entrySet()) {
Object queryFieldValue = ReflectUtil.getFieldValue(queryGoodsIndex, entry.getValue());
@ -33,7 +33,7 @@ public class EsIndexUtil {
return getUpdateIndexFieldsMap(queryFieldsMap, updateFieldsMap);
}
public static Map<String, Object> getUpdateIndexFieldsMap(Map<Object, Object> queryFieldsMap, Map<Object, Object> updateFieldsMap) {
public static Map<String, Object> getUpdateIndexFieldsMap(Map<String, Object> queryFieldsMap, Map<String, Object> updateFieldsMap) {
Map<String, Object> updateIndexMap = new HashMap<>();
updateIndexMap.put("queryFields", queryFieldsMap);

View File

@ -32,10 +32,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
/**
* 店铺详细业务层实现
@ -103,8 +100,8 @@ public class StoreDetailServiceImpl extends ServiceImpl<StoreDetailMapper, Store
goodsService.updateStoreDetail(store);
Map<String, Object> updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap(
MapUtil.builder().put("storeId", store.getId()).build(),
MapUtil.builder().put("storeName", store.getStoreName()).put("selfOperated", store.getSelfOperated()).build());
MapUtil.builder(new HashMap<String, Object>()).put("storeId", store.getId()).build(),
MapUtil.builder(new HashMap<String, Object>()).put("storeName", store.getStoreName()).put("selfOperated", store.getSelfOperated()).build());
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback());

View File

@ -81,7 +81,7 @@ public class SeckillManagerController {
@ApiOperation(value = "获取秒杀活动申请列表")
@GetMapping("/apply")
public ResultMessage<IPage<SeckillApply>> getSeckillApply(SeckillSearchParams param, PageVO pageVo) {
IPage<SeckillApply> seckillApply = seckillApplyService.getSeckillApply(param, pageVo);
IPage<SeckillApply> seckillApply = seckillApplyService.getSeckillApplyPage(param, pageVo);
return ResultUtil.data(seckillApply);
}

View File

@ -47,7 +47,7 @@ public class SeckillStoreController {
public ResultMessage<IPage<SeckillApply>> getSeckillApplyPage(SeckillSearchParams queryParam, PageVO pageVo) {
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
queryParam.setStoreId(storeId);
IPage<SeckillApply> seckillPage = seckillApplyService.getSeckillApply(queryParam, pageVo);
IPage<SeckillApply> seckillPage = seckillApplyService.getSeckillApplyPage(queryParam, pageVo);
return ResultUtil.data(seckillPage);
}