Skip to content

Commit

Permalink
优化结算时获取用户优惠券信息(增加redis存储)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Jan 11, 2022
1 parent 459a736 commit 1c28e0b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.order.cart.entity.dto.MemberCouponDTO;
Expand All @@ -13,11 +12,8 @@
import cn.lili.modules.order.cart.render.util.PromotionPriceUtil;
import cn.lili.modules.order.order.entity.dto.PriceDetailDTO;
import cn.lili.modules.promotion.entity.dos.MemberCoupon;
import cn.lili.modules.promotion.entity.dto.search.MemberCouponSearchParams;
import cn.lili.modules.promotion.entity.enums.CouponTypeEnum;
import cn.lili.modules.promotion.entity.enums.MemberCouponStatusEnum;
import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum;
import cn.lili.modules.promotion.entity.enums.PromotionsStatusEnum;
import cn.lili.modules.promotion.entity.vos.MemberCouponVO;
import cn.lili.modules.promotion.service.MemberCouponService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -64,11 +60,7 @@ public void render(TradeDTO tradeDTO) {
* @param tradeDTO 交易dto
*/
private void renderCouponRule(TradeDTO tradeDTO) {
MemberCouponSearchParams searchParams = new MemberCouponSearchParams();
searchParams.setMemberId(UserContext.getCurrentUser().getId());
searchParams.setMemberCouponStatus(MemberCouponStatusEnum.NEW.name());
searchParams.setPromotionStatus(PromotionsStatusEnum.START.name());
List<MemberCoupon> memberCouponList = memberCouponService.getMemberCoupons(searchParams);
List<MemberCoupon> memberCouponList = memberCouponService.getMemberCoupons(tradeDTO.getMemberId());

if (!memberCouponList.isEmpty()) {
this.checkMemberExistCoupon(tradeDTO, memberCouponList);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package cn.lili.modules.order.order.serviceimpl;import cn.hutool.json.JSONUtil;import cn.lili.cache.Cache;import cn.lili.cache.CachePrefix;import cn.lili.common.enums.ResultCode;import cn.lili.common.exception.ServiceException;import cn.lili.common.properties.RocketmqCustomProperties;import cn.lili.modules.member.entity.dos.MemberAddress;import cn.lili.modules.member.entity.enums.PointTypeEnum;import cn.lili.modules.member.service.MemberService;import cn.lili.modules.order.cart.entity.dto.MemberCouponDTO;import cn.lili.modules.order.cart.entity.dto.TradeDTO;import cn.lili.modules.order.cart.entity.enums.CartTypeEnum;import cn.lili.modules.order.cart.entity.vo.CartVO;import cn.lili.modules.order.order.entity.dos.Order;import cn.lili.modules.order.order.entity.dos.Trade;import cn.lili.modules.order.order.entity.enums.PayStatusEnum;import cn.lili.modules.order.order.mapper.TradeMapper;import cn.lili.modules.order.order.service.OrderService;import cn.lili.modules.order.order.service.TradeService;import cn.lili.modules.promotion.entity.dos.KanjiaActivity;import cn.lili.modules.promotion.entity.enums.KanJiaStatusEnum;import cn.lili.modules.promotion.service.CouponService;import cn.lili.modules.promotion.service.KanjiaActivityService;import cn.lili.modules.promotion.service.MemberCouponService;import cn.lili.rocketmq.RocketmqSendCallbackBuilder;import cn.lili.rocketmq.tags.OrderTagsEnum;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import org.apache.rocketmq.spring.core.RocketMQTemplate;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.Collection;import java.util.List;import java.util.stream.Collectors;/** * 交易业务层实现 * * @author Chopper * @date 2020/11/17 7:39 下午 */@Service@Transactional(rollbackFor = Exception.class)public class TradeServiceImpl extends ServiceImpl<TradeMapper, Trade> implements TradeService { /** * 缓存 */ @Autowired private Cache<Object> cache; /** * 订单 */ @Autowired private OrderService orderService; /** * 会员 */ @Autowired private MemberService memberService; /** * 优惠券 */ @Autowired private CouponService couponService; /** * 会员优惠券 */ @Autowired private MemberCouponService memberCouponService; /** * 砍价 */ @Autowired private KanjiaActivityService kanjiaActivityService; /** * RocketMQ */ @Autowired private RocketMQTemplate rocketMQTemplate; /** * RocketMQ 配置 */ @Autowired private RocketmqCustomProperties rocketmqCustomProperties; @Override @Transactional(rollbackFor = Exception.class) public Trade createTrade(TradeDTO tradeDTO) { //创建订单预校验 createTradeCheck(tradeDTO); Trade trade = new Trade(tradeDTO); String key = CachePrefix.TRADE.getPrefix() + trade.getSn(); //优惠券预处理 couponPretreatment(tradeDTO); //积分预处理 pointPretreatment(tradeDTO); //添加交易 this.save(trade); //添加订单 orderService.intoDB(tradeDTO); //砍价订单处理 kanjiaPretreatment(tradeDTO); //写入缓存,给消费者调用 cache.put(key, JSONUtil.toJsonStr(tradeDTO)); //构建订单创建消息 String destination = rocketmqCustomProperties.getOrderTopic() + ":" + OrderTagsEnum.ORDER_CREATE.name(); //发送订单创建消息 rocketMQTemplate.asyncSend(destination, key, RocketmqSendCallbackBuilder.commonCallback()); return trade; } /** * 创建订单最后一步校验 * * @param tradeDTO */ private void createTradeCheck(TradeDTO tradeDTO) { //创建订单如果没有收获地址, MemberAddress memberAddress = tradeDTO.getMemberAddress(); if (memberAddress == null) { throw new ServiceException(ResultCode.MEMBER_ADDRESS_NOT_EXIST); } //订单配送区域校验 if (tradeDTO.getNotSupportFreight() != null && !tradeDTO.getNotSupportFreight().isEmpty()) { StringBuilder stringBuilder = new StringBuilder("包含商品有-"); tradeDTO.getNotSupportFreight().forEach(sku -> stringBuilder.append(sku.getGoodsSku().getGoodsName())); throw new ServiceException(ResultCode.ORDER_NOT_SUPPORT_DISTRIBUTION, stringBuilder.toString()); } } @Override public Trade getBySn(String sn) { LambdaQueryWrapper<Trade> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Trade::getSn, sn); return this.getOne(queryWrapper); } @Override public void payTrade(String tradeSn, String paymentName, String receivableNo) { LambdaQueryWrapper<Order> orderQueryWrapper = new LambdaQueryWrapper<>(); orderQueryWrapper.eq(Order::getTradeSn, tradeSn); List<Order> orders = orderService.list(orderQueryWrapper); for (Order order : orders) { orderService.payOrder(order.getSn(), paymentName, receivableNo); } Trade trade = this.getBySn(tradeSn); trade.setPayStatus(PayStatusEnum.PAID.name()); this.saveOrUpdate(trade); } /** * 优惠券预处理 * 下单同时,扣除优惠券 * * @param tradeDTO */ private void couponPretreatment(TradeDTO tradeDTO) { List<MemberCouponDTO> memberCouponDTOList = new ArrayList<>(); if (null != tradeDTO.getPlatformCoupon()) { memberCouponDTOList.add(tradeDTO.getPlatformCoupon()); } Collection<MemberCouponDTO> storeCoupons = tradeDTO.getStoreCoupons().values(); if (!storeCoupons.isEmpty()) { memberCouponDTOList.addAll(storeCoupons); } List<String> ids = memberCouponDTOList.stream().map(e -> e.getMemberCoupon().getId()).collect(Collectors.toList()); memberCouponService.used(ids); memberCouponDTOList.forEach(e -> couponService.usedCoupon(e.getMemberCoupon().getCouponId(), 1)); } /** * 创建交易,积分处理 * * @param tradeDTO */ private void pointPretreatment(TradeDTO tradeDTO) { //需要支付积分 if (tradeDTO.getPriceDetailDTO() != null && tradeDTO.getPriceDetailDTO().getPayPoint() != null && tradeDTO.getPriceDetailDTO().getPayPoint() > 0) { StringBuilder orderSns = new StringBuilder(); for (CartVO item : tradeDTO.getCartList()) { orderSns.append(item.getSn()); } boolean result = memberService.updateMemberPoint(tradeDTO.getPriceDetailDTO().getPayPoint().longValue(), PointTypeEnum.REDUCE.name(), tradeDTO.getMemberId(), "订单【" + orderSns + "】创建,积分扣减"); if (!result) { throw new ServiceException(ResultCode.PAY_POINT_ENOUGH); } } } /** * 创建交易、砍价处理 * * @param tradeDTO */ private void kanjiaPretreatment(TradeDTO tradeDTO) { if (tradeDTO.getCartTypeEnum().equals(CartTypeEnum.KANJIA)) { String kanjiaId = tradeDTO.getSkuList().get(0).getKanjiaId(); kanjiaActivityService.update(new LambdaUpdateWrapper<KanjiaActivity>() .eq(KanjiaActivity::getId, kanjiaId) .set(KanjiaActivity::getStatus, KanJiaStatusEnum.END.name())); } }}
package cn.lili.modules.order.order.serviceimpl;import cn.hutool.json.JSONUtil;import cn.lili.cache.Cache;import cn.lili.cache.CachePrefix;import cn.lili.common.enums.ResultCode;import cn.lili.common.exception.ServiceException;import cn.lili.common.properties.RocketmqCustomProperties;import cn.lili.modules.member.entity.dos.MemberAddress;import cn.lili.modules.member.entity.enums.PointTypeEnum;import cn.lili.modules.member.service.MemberService;import cn.lili.modules.order.cart.entity.dto.MemberCouponDTO;import cn.lili.modules.order.cart.entity.dto.TradeDTO;import cn.lili.modules.order.cart.entity.enums.CartTypeEnum;import cn.lili.modules.order.cart.entity.vo.CartVO;import cn.lili.modules.order.order.entity.dos.Order;import cn.lili.modules.order.order.entity.dos.Trade;import cn.lili.modules.order.order.entity.enums.PayStatusEnum;import cn.lili.modules.order.order.mapper.TradeMapper;import cn.lili.modules.order.order.service.OrderService;import cn.lili.modules.order.order.service.TradeService;import cn.lili.modules.promotion.entity.dos.KanjiaActivity;import cn.lili.modules.promotion.entity.enums.KanJiaStatusEnum;import cn.lili.modules.promotion.service.CouponService;import cn.lili.modules.promotion.service.KanjiaActivityService;import cn.lili.modules.promotion.service.MemberCouponService;import cn.lili.rocketmq.RocketmqSendCallbackBuilder;import cn.lili.rocketmq.tags.OrderTagsEnum;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import org.apache.rocketmq.spring.core.RocketMQTemplate;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.Collection;import java.util.List;import java.util.stream.Collectors;/** * 交易业务层实现 * * @author Chopper * @date 2020/11/17 7:39 下午 */@Service@Transactional(rollbackFor = Exception.class)public class TradeServiceImpl extends ServiceImpl<TradeMapper, Trade> implements TradeService { /** * 缓存 */ @Autowired private Cache<Object> cache; /** * 订单 */ @Autowired private OrderService orderService; /** * 会员 */ @Autowired private MemberService memberService; /** * 优惠券 */ @Autowired private CouponService couponService; /** * 会员优惠券 */ @Autowired private MemberCouponService memberCouponService; /** * 砍价 */ @Autowired private KanjiaActivityService kanjiaActivityService; /** * RocketMQ */ @Autowired private RocketMQTemplate rocketMQTemplate; /** * RocketMQ 配置 */ @Autowired private RocketmqCustomProperties rocketmqCustomProperties; @Override @Transactional(rollbackFor = Exception.class) public Trade createTrade(TradeDTO tradeDTO) { //创建订单预校验 createTradeCheck(tradeDTO); Trade trade = new Trade(tradeDTO); String key = CachePrefix.TRADE.getPrefix() + trade.getSn(); //优惠券预处理 couponPretreatment(tradeDTO); //积分预处理 pointPretreatment(tradeDTO); //添加交易 this.save(trade); //添加订单 orderService.intoDB(tradeDTO); //砍价订单处理 kanjiaPretreatment(tradeDTO); //写入缓存,给消费者调用 cache.put(key, JSONUtil.toJsonStr(tradeDTO)); //构建订单创建消息 String destination = rocketmqCustomProperties.getOrderTopic() + ":" + OrderTagsEnum.ORDER_CREATE.name(); //发送订单创建消息 rocketMQTemplate.asyncSend(destination, key, RocketmqSendCallbackBuilder.commonCallback()); return trade; } /** * 创建订单最后一步校验 * * @param tradeDTO */ private void createTradeCheck(TradeDTO tradeDTO) { //创建订单如果没有收获地址, MemberAddress memberAddress = tradeDTO.getMemberAddress(); if (memberAddress == null) { throw new ServiceException(ResultCode.MEMBER_ADDRESS_NOT_EXIST); } //订单配送区域校验 if (tradeDTO.getNotSupportFreight() != null && !tradeDTO.getNotSupportFreight().isEmpty()) { StringBuilder stringBuilder = new StringBuilder("包含商品有-"); tradeDTO.getNotSupportFreight().forEach(sku -> stringBuilder.append(sku.getGoodsSku().getGoodsName())); throw new ServiceException(ResultCode.ORDER_NOT_SUPPORT_DISTRIBUTION, stringBuilder.toString()); } } @Override public Trade getBySn(String sn) { LambdaQueryWrapper<Trade> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Trade::getSn, sn); return this.getOne(queryWrapper); } @Override public void payTrade(String tradeSn, String paymentName, String receivableNo) { LambdaQueryWrapper<Order> orderQueryWrapper = new LambdaQueryWrapper<>(); orderQueryWrapper.eq(Order::getTradeSn, tradeSn); List<Order> orders = orderService.list(orderQueryWrapper); for (Order order : orders) { orderService.payOrder(order.getSn(), paymentName, receivableNo); } Trade trade = this.getBySn(tradeSn); trade.setPayStatus(PayStatusEnum.PAID.name()); this.saveOrUpdate(trade); } /** * 优惠券预处理 * 下单同时,扣除优惠券 * * @param tradeDTO */ private void couponPretreatment(TradeDTO tradeDTO) { List<MemberCouponDTO> memberCouponDTOList = new ArrayList<>(); if (null != tradeDTO.getPlatformCoupon()) { memberCouponDTOList.add(tradeDTO.getPlatformCoupon()); } Collection<MemberCouponDTO> storeCoupons = tradeDTO.getStoreCoupons().values(); if (!storeCoupons.isEmpty()) { memberCouponDTOList.addAll(storeCoupons); } List<String> ids = memberCouponDTOList.stream().map(e -> e.getMemberCoupon().getId()).collect(Collectors.toList()); memberCouponService.used(tradeDTO.getMemberId() ,ids); memberCouponDTOList.forEach(e -> couponService.usedCoupon(e.getMemberCoupon().getCouponId(), 1)); } /** * 创建交易,积分处理 * * @param tradeDTO */ private void pointPretreatment(TradeDTO tradeDTO) { //需要支付积分 if (tradeDTO.getPriceDetailDTO() != null && tradeDTO.getPriceDetailDTO().getPayPoint() != null && tradeDTO.getPriceDetailDTO().getPayPoint() > 0) { StringBuilder orderSns = new StringBuilder(); for (CartVO item : tradeDTO.getCartList()) { orderSns.append(item.getSn()); } boolean result = memberService.updateMemberPoint(tradeDTO.getPriceDetailDTO().getPayPoint().longValue(), PointTypeEnum.REDUCE.name(), tradeDTO.getMemberId(), "订单【" + orderSns + "】创建,积分扣减"); if (!result) { throw new ServiceException(ResultCode.PAY_POINT_ENOUGH); } } } /** * 创建交易、砍价处理 * * @param tradeDTO */ private void kanjiaPretreatment(TradeDTO tradeDTO) { if (tradeDTO.getCartTypeEnum().equals(CartTypeEnum.KANJIA)) { String kanjiaId = tradeDTO.getSkuList().get(0).getKanjiaId(); kanjiaActivityService.update(new LambdaUpdateWrapper<KanjiaActivity>() .eq(KanjiaActivity::getId, kanjiaId) .set(KanjiaActivity::getStatus, KanJiaStatusEnum.END.name())); } }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import cn.lili.common.vo.PageVO;
import cn.lili.modules.promotion.entity.dos.MemberCoupon;
import cn.lili.modules.promotion.entity.dto.search.MemberCouponSearchParams;
import cn.lili.modules.promotion.entity.enums.MemberCouponStatusEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;

Expand Down Expand Up @@ -55,12 +54,19 @@ public interface MemberCouponService extends IService<MemberCoupon> {
/**
* 获取会员优惠券列表
*
* @param pageVo 分页参数
* @param param 查询参数
* @return 会员优惠券列表
*/
List<MemberCoupon> getMemberCoupons(MemberCouponSearchParams param);

/**
* 获取当前用户的优惠券列表(优先读取缓存)
*
* @param memberId 会员id
* @return 会员优惠券列表
*/
List<MemberCoupon> getMemberCoupons(String memberId);

/**
* 获取会员优惠券列表
*
Expand Down Expand Up @@ -97,27 +103,19 @@ public interface MemberCouponService extends IService<MemberCoupon> {
*/
long getMemberCouponsNum();

/**
* 更新会员优惠券状态
*
* @param status 要变更的状态
* @param id 会员优惠券id
*/
void updateMemberCouponStatus(MemberCouponStatusEnum status, String id);

/**
* 使用优惠券
*
* @param ids 会员优惠券id
*/
void used(List<String> ids);
void used(String memberId, List<String> ids);

/**
* 作废当前会员优惠券
*
* @param id id
*/
void cancellation(String id);
void cancellation(String memberId, String id);

/**
* 关闭会员优惠券
Expand Down
Loading

0 comments on commit 1c28e0b

Please sign in to comment.