使用优惠券处理

This commit is contained in:
wuchunlei
2024-11-01 15:59:08 +08:00
parent f31342fb0a
commit b5467bb011
4 changed files with 70 additions and 63 deletions

View File

@@ -201,9 +201,6 @@ public class BuyOrderController {
@RequestMapping(value = "/placeOrder", method = RequestMethod.POST)
@Transactional
public R placeOrder(@RequestBody BuyOrder buyOrder) {
String orderSn = IdWorker.getTimeId().substring(0, 32);
buyOrder.setOrderSn(orderSn);
buyOrderService.save(buyOrder);
List<BuyOrderProduct> buyOrderProductList = buyOrder.getProductList();
// 订单实收金额
BigDecimal totalPrice = new BigDecimal(0);
@@ -245,21 +242,27 @@ public class BuyOrderController {
totalPrice = totalPrice.add(price.multiply(BigDecimal.valueOf(quantity)));
}
//商品价格减去优惠券的优惠金额
if (buyOrder!=null&&buyOrder.getCouponId()!=null&&buyOrder.getCouponId()!=0){
if (buyOrder.getCouponId()!=null&&buyOrder.getCouponId()!=0){
CouponHistory couponHistory = couponHistoryService.getById(buyOrder.getCouponId());
if (couponHistory!=null){
if (couponHistory.getEffectType()!=0){
if (couponHistory.getStartTime().getTime()<new Date().getTime()&&
couponHistory.getEndTime().getTime()>new Date().getTime()){
}else {
return R.error("优惠券使用时间未到");
if (couponHistory.getStatus()==0){
if (couponHistory.getEffectType()!=0){
if (couponHistory.getStartTime().getTime()<new Date().getTime()&&
couponHistory.getEndTime().getTime()>new Date().getTime()){
}else {
return R.error("优惠券使用时间未到");
}
}
CouponEntity coupon = couponService.getById(couponHistory.getCouponId());
if (coupon != null){
totalPrice = totalPrice.subtract(coupon.getCouponAmount());
}
}else {
if (couponHistory.getStatus()==1){
return R.error("优惠券已被使用");
}else if (couponHistory.getStatus() == 2){
return R.error("优惠券已过期");
}
}
CouponEntity coupon = couponService.getById(couponHistory.getCouponId());
if (coupon != null){
couponHistory.setOrderId(buyOrder.getOrderId());
couponService.useCouponAmount(couponHistory);
totalPrice = totalPrice.subtract(coupon.getCouponAmount());
}
}
}
@@ -276,6 +279,8 @@ public class BuyOrderController {
return R.error("系统错误订单金额异常!");
}
String orderSn = IdWorker.getTimeId().substring(0, 32);
buyOrder.setOrderSn(orderSn);
if(buyOrder.getAddressId()!=0){
QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", buyOrder.getAddressId());
@@ -292,7 +297,7 @@ public class BuyOrderController {
}
}
buyOrder.setOrderStatus("0");
buyOrderService.updateById(buyOrder);
buyOrderService.save(buyOrder);
//解决购物车相关问题
for (BuyOrderProduct buyOrderProduct : buyOrderProductList) {
@@ -492,7 +497,6 @@ public class BuyOrderController {
}
//回滚优惠卷
if (buyOrder.getCouponId()!=null&&buyOrder.getCouponId()!=0){
couponService.rollbackCoupon(buyOrder.getCouponId());
buyOrder.setCouponId(null);
buyOrderService.updateById(buyOrder);
}
@@ -542,7 +546,6 @@ public class BuyOrderController {
if (buyOrder != null) {
//回滚优惠卷
if (buyOrder.getCouponId()!=null&&buyOrder.getCouponId()!=0){
couponService.rollbackCoupon(buyOrder.getCouponId());
buyOrder.setCouponId(null);
buyOrderService.updateById(buyOrder);
}

View File

@@ -42,7 +42,6 @@ public class OrderCancelConsumer {
buyOrder.setOrderStatus(Constants.ORDER_STATUS_OUT_OF_TIME);
//回滚优惠卷
if (buyOrder.getCouponId()!=null&&buyOrder.getCouponId()!=0){
couponService.rollbackCoupon(buyOrder.getCouponId());
buyOrder.setCouponId(null);
}
//回滚库存

View File

@@ -12,6 +12,7 @@ import com.peanut.common.utils.OrderUtils;
import com.peanut.modules.common.dao.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.CouponHistoryService;
import com.peanut.modules.common.service.CouponService;
import com.peanut.modules.master.service.UserCourseBuyService;
import com.peanut.modules.pay.alipay.config.AliPayConfig;
@@ -75,6 +76,8 @@ public class AliPayServiceImpl implements AliPayService {
private UserVipDao userVipDao;
@Autowired
private CouponService couponService;
@Autowired
private CouponHistoryService couponHistoryService;
@Override
public String pay(AlipayDTO payDto) {
@@ -158,25 +161,31 @@ public class AliPayServiceImpl implements AliPayService {
String body = oldPayZfbOrderEntity.getBody();
String customerid = oldPayZfbOrderEntity.getCustomerid();
BuyOrder order = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>()
.eq("order_sn", oldPayZfbOrderEntity.getRelevanceoid()));
//使用优惠券
if (order.getCouponId()!=null&&order.getCouponId()!=0){
CouponHistory couponHistory = couponHistoryService.getById(order.getCouponId());
couponHistory.setOrderId(order.getOrderId());
couponService.useCouponAmount(couponHistory);
}
if("relearn".equals(subject)){
BuyOrder orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>()
.eq("order_sn", oldPayZfbOrderEntity.getRelevanceoid()));
//更新 订单 记录
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),"2");
//插入复读记录
userCourseBuyService.addUserCourseBuyRelearn(orderEntity,"支付宝购买:"+orderEntity.getOrderSn());
userCourseBuyService.addUserCourseBuyRelearn(order,"支付宝购买:"+order.getOrderSn());
}
if ("vip".equals(subject)) {
BuyOrder orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>().eq("order_sn", oldPayZfbOrderEntity.getRelevanceoid()));
//更新 订单 记录
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),"2");
//处理抵扣积分
if(orderEntity.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(orderEntity);
if(order.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(order);
}
//开通vip
openVipForUser(orderEntity);
openVipForUser(order);
//获取会员开通 日期
// BookBuyConfigEntity bookBuyConfigEntity = bookBuyConfigService.getById(Integer.valueOf(body));
// String month = bookBuyConfigEntity.getMonth();
@@ -219,42 +228,36 @@ public class AliPayServiceImpl implements AliPayService {
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),"2");
}
if ("order".equals(subject)) {
BuyOrder orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>().eq("order_sn", oldPayZfbOrderEntity.getRelevanceoid()));
//更新 订单 记录
String ActString = buyOrderService.checkWlOrder(orderEntity.getOrderSn())?"0":"2";
String ActString = buyOrderService.checkWlOrder(order.getOrderSn())?"0":"2";
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),ActString);
//处理抵扣积分
if(orderEntity.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(orderEntity);
if(order.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(order);
}
/* 记录用户购买的书籍 */
// 查询订单的所有 book_id
List<Integer> orderBookIdList = shopProductBookDao.getOrderBookId(orderEntity.getOrderSn());
List<Integer> orderBookIdList = shopProductBookDao.getOrderBookId(order.getOrderSn());
// 去重
Set<Integer> set = new HashSet<>(orderBookIdList);
orderBookIdList.clear();
orderBookIdList.addAll(set);
// 查询用户的所有 book_id
List<Integer> userBookIdList = userEbookBuyDao.getUserBookId(orderEntity.getUserId());
List<Integer> userBookIdList = userEbookBuyDao.getUserBookId(order.getUserId());
// 取差集
orderBookIdList.removeAll(userBookIdList);
// 为用户添加书籍
for (Integer bookId : orderBookIdList) {
UserEbookBuyEntity entity = new UserEbookBuyEntity();
entity.setUserId(orderEntity.getUserId());
entity.setUserId(order.getUserId());
entity.setBookId(bookId);
userEbookBuyDao.insert(entity);
}
//开通coursestart
List<ShopProductCourseEntity> orderCourse = buyOrderService.getOrderCourse(orderEntity.getOrderSn());
List<ShopProductCourseEntity> orderCourse = buyOrderService.getOrderCourse(order.getOrderSn());
for ( ShopProductCourseEntity s : orderCourse){
LambdaQueryWrapper<UserCourseBuyEntity> wrapper2 = new LambdaQueryWrapper<>();
wrapper2.eq(UserCourseBuyEntity::getUserId,orderEntity.getUserId());
wrapper2.eq(UserCourseBuyEntity::getUserId,order.getUserId());
wrapper2.eq(UserCourseBuyEntity::getCatalogueId,s.getCatalogueId());
wrapper2.and(r->r.isNull(UserCourseBuyEntity::getEndTime).or().gt(UserCourseBuyEntity::getEndTime,new Date()));
List<UserCourseBuyEntity> userCourseBuyEntities = userCourseBuyDao.selectList(wrapper2);
@@ -267,11 +270,11 @@ public class AliPayServiceImpl implements AliPayService {
calendar.add(Calendar.DAY_OF_MONTH,s.getDays());
userCourseBuyEntity.setEndTime(calendar.getTime());
}
userCourseBuyEntity.setCome(userCourseBuyEntity.getCome()+";延期(支付宝购买:"+orderEntity.getOrderSn()+")");
userCourseBuyEntity.setCome(userCourseBuyEntity.getCome()+";延期(支付宝购买:"+order.getOrderSn()+")");
userCourseBuyDao.updateById(userCourseBuyEntity);
}else{
UserCourseBuyEntity userCourseBuyEntity = new UserCourseBuyEntity();
userCourseBuyEntity.setUserId(orderEntity.getUserId());
userCourseBuyEntity.setUserId(order.getUserId());
userCourseBuyEntity.setCourseId(s.getCourseId());
userCourseBuyEntity.setCatalogueId(s.getCatalogueId());
userCourseBuyEntity.setDays(s.getDays());
@@ -281,34 +284,34 @@ public class AliPayServiceImpl implements AliPayService {
// cal.setTime(new Date());
// cal.add(Calendar.DAY_OF_MONTH,s.getDays());
// userCourseBuyEntity.setEndTime(cal.getTime());
userCourseBuyEntity.setCome("支付宝购买:"+orderEntity.getOrderSn());
userCourseBuyEntity.setCome("支付宝购买:"+order.getOrderSn());
userCourseBuyDao.insert(userCourseBuyEntity);
}
}
//开通courseend
List<Integer> collect = buyOrderProductDao.selectList(new LambdaQueryWrapper<BuyOrderProduct>().eq(BuyOrderProduct::getOrderId, orderEntity.getOrderId())).stream().map(BuyOrderProduct::getProductId).collect(Collectors.toList());
List<Integer> collect = buyOrderProductDao.selectList(new LambdaQueryWrapper<BuyOrderProduct>().eq(BuyOrderProduct::getOrderId, order.getOrderId())).stream().map(BuyOrderProduct::getProductId).collect(Collectors.toList());
//发放优惠卷
couponService.insertCouponHistoryByProductId(collect);
//手摸脚模购买后会开启用户的脉穴的功能
if(collect.contains(128)||collect.contains(129)||collect.contains(130)||collect.contains(131)||collect.contains(136)||collect.contains(137)){
MyUserEntity userInfo = userService.getById(orderEntity.getUserId());
MyUserEntity userInfo = userService.getById(order.getUserId());
userInfo.setPointPower(1);
userService.updateById(userInfo);
}
if(collect.contains(133)||collect.contains(134)||collect.contains(135)){
MyUserEntity userInfo = userService.getById(orderEntity.getUserId());
MyUserEntity userInfo = userService.getById(order.getUserId());
userInfo.setTgdzPower(1);
userService.updateById(userInfo);
}
if(collect.contains(39)||collect.contains(62)||collect.contains(123)||collect.contains(127)){
MyUserEntity userInfo = userService.getById(orderEntity.getUserId());
MyUserEntity userInfo = userService.getById(order.getUserId());
userInfo.setWylqPower(1);
userService.updateById(userInfo);
}
if(collect.contains(43)||collect.contains(62)||collect.contains(124)){
MyUserEntity userInfo = userService.getById(orderEntity.getUserId());
MyUserEntity userInfo = userService.getById(order.getUserId());
userInfo.setPrescriptBPower(1);
userService.updateById(userInfo);
}

View File

@@ -10,6 +10,7 @@ import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.modules.common.dao.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.CouponHistoryService;
import com.peanut.modules.common.service.CouponService;
import com.peanut.modules.master.service.UserCourseBuyService;
import com.peanut.modules.pay.refund.entity.PayRefundOrder;
@@ -83,6 +84,8 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
private UserVipDao userVipDao;
@Autowired
private CouponService couponService;
@Autowired
private CouponHistoryService couponHistoryService;
@Override
public void prepay(WechatPaymentInfo paymentInfo){
@@ -144,35 +147,34 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
payWechatOrderService.updateById(payWechatOrderEntity);
// 根据订单号,做幂等处理,并且在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱
BuyOrder order = this.buyOrderService.getOne(new QueryWrapper<BuyOrder>().eq("order_sn", orderNo));
//使用优惠券
if (order.getCouponId()!=null&&order.getCouponId()!=0){
CouponHistory couponHistory = couponHistoryService.getById(order.getCouponId());
couponHistory.setOrderId(order.getOrderId());
couponService.useCouponAmount(couponHistory);
}
if("relearn".equals(order.getOrderType())){
BuyOrder orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>()
.eq("order_sn", orderNo));
//更新 订单 记录
buyOrderService.updateOrderStatus(orderEntity.getUserId(),orderNo,"2");
buyOrderService.updateOrderStatus(order.getUserId(),orderNo,"2");
//插入复读记录
userCourseBuyService.addUserCourseBuyRelearn(orderEntity,"微信购买:"+orderEntity.getOrderSn());
userCourseBuyService.addUserCourseBuyRelearn(order,"微信购买:"+order.getOrderSn());
}
if("vip".equals(order.getOrderType())){
BuyOrder orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>().eq("order_sn", orderNo));
//更新 订单 记录
buyOrderService.updateOrderStatus(orderEntity.getUserId(),orderNo,"2");
buyOrderService.updateOrderStatus(order.getUserId(),orderNo,"2");
//处理抵扣积分
if(orderEntity.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(orderEntity);
if(order.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(order);
}
//开通vip
openVipForUser(orderEntity);
openVipForUser(order);
}
// 1.根据订单id获取订单信息
if ("order".equals(order.getOrderType())) {
BuyOrder orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrder>().eq("order_sn", orderNo));
BigDecimal realMoney = orderEntity.getRealMoney();
if(orderEntity.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(orderEntity);
if(order.getJfDeduction().compareTo(BigDecimal.ZERO)>0){
userCoinJf(order);
}
//开通bookstart