|
@@ -2,15 +2,15 @@ package com.qs.mp.user.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.qs.mp.admin.domain.Ticket;
|
|
|
import com.qs.mp.admin.domain.TicketBox;
|
|
|
import com.qs.mp.admin.service.ITicketBoxService;
|
|
|
import com.qs.mp.admin.service.ITicketService;
|
|
|
-import com.qs.mp.channel.domain.ChannelOrder;
|
|
|
import com.qs.mp.channel.domain.ChannelUserRel;
|
|
|
import com.qs.mp.channel.service.IChannelUserRelService;
|
|
|
-import com.qs.mp.common.enums.ChannelOrderStatusEnum;
|
|
|
import com.qs.mp.common.enums.TicketBoxStatusEnum;
|
|
|
import com.qs.mp.common.enums.TicketStatusEnum;
|
|
|
import com.qs.mp.common.enums.TicketTypeEnum;
|
|
@@ -18,6 +18,7 @@ import com.qs.mp.common.enums.UserCouponStatusEnum;
|
|
|
import com.qs.mp.common.enums.UserTicketOrderStatusEnum;
|
|
|
import com.qs.mp.common.exception.ServiceException;
|
|
|
import com.qs.mp.common.utils.LogUtil;
|
|
|
+import com.qs.mp.common.utils.RSAUtil;
|
|
|
import com.qs.mp.common.utils.StringUtils;
|
|
|
import com.qs.mp.pay.domain.PayOrder;
|
|
|
import com.qs.mp.system.service.id.BizIdGenerator;
|
|
@@ -30,8 +31,6 @@ import com.qs.mp.user.mapper.UserTicketOrderMapper;
|
|
|
import com.qs.mp.user.service.IUserCouponService;
|
|
|
import com.qs.mp.user.service.IUserTicketOrderItemService;
|
|
|
import com.qs.mp.user.service.IUserTicketOrderService;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
@@ -40,6 +39,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -50,7 +50,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
* @since 2022-03-07
|
|
|
*/
|
|
|
@Service
|
|
|
-public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMapper, UserTicketOrder> implements IUserTicketOrderService {
|
|
|
+public class UserTicketOrderServiceImpl extends
|
|
|
+ ServiceImpl<UserTicketOrderMapper, UserTicketOrder> implements IUserTicketOrderService {
|
|
|
|
|
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
@@ -71,10 +72,11 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
|
|
|
@Autowired
|
|
|
private BizIdGenerator bizIdGenerator;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public String submitOrder(Long userId, TicketOrderSettleVO orderSettleVO) {
|
|
|
- if (StringUtils.isNotBlank(orderSettleVO.getBoxId())){
|
|
|
+ if (StringUtils.isNotBlank(orderSettleVO.getBoxId())) {
|
|
|
return createOnlineOrder(userId, orderSettleVO);
|
|
|
}
|
|
|
|
|
@@ -85,17 +87,23 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
private String createOnlineOrder(Long userId, TicketOrderSettleVO orderSettleVO) {
|
|
|
// 线上票更新票组销量,防止超卖
|
|
|
TicketBox ticketBox = ticketBoxService.getById(orderSettleVO.getBoxId());
|
|
|
- if (ticketBox.getStatus() == TicketBoxStatusEnum.PUT_OFF || ticketBox.getSaleQty() + orderSettleVO.getOrderNum() > ticketBox.getQuantity()) {
|
|
|
- LogUtil.error(logger, "库存不足,boxId:{0},qty:{1},saleQty:{2},orderNum:{3}", new Object[]{ticketBox.getBoxId(), ticketBox.getQuantity(), ticketBox.getSaleQty(), orderSettleVO.getOrderNum()});
|
|
|
+ if (ticketBox.getStatus() == TicketBoxStatusEnum.PUT_OFF
|
|
|
+ || ticketBox.getSaleQty() + orderSettleVO.getOrderNum() > ticketBox.getQuantity()) {
|
|
|
+ LogUtil.error(logger, "库存不足,boxId:{0},qty:{1},saleQty:{2},orderNum:{3}",
|
|
|
+ ticketBox.getBoxId(), ticketBox.getQuantity(), ticketBox.getSaleQty(),
|
|
|
+ orderSettleVO.getOrderNum());
|
|
|
throw new ServiceException("库存不足");
|
|
|
}
|
|
|
- boolean rst = ticketBoxService.update(new LambdaUpdateWrapper<TicketBox>().set(TicketBox::getSaleQty, ticketBox.getSaleQty() + orderSettleVO.getOrderNum())
|
|
|
- .set(ticketBox.getSaleQty() + orderSettleVO.getOrderNum() == ticketBox.getQuantity(),TicketBox::getStatus, TicketBoxStatusEnum.PUT_OFF)
|
|
|
- .eq(TicketBox::getBoxId, ticketBox.getBoxId()).eq(TicketBox::getSaleQty, ticketBox.getSaleQty()));
|
|
|
- if (!rst) {
|
|
|
- LogUtil.error(logger, "盲票购买订单创建时,更新盲票销量失败。boxId:{0}, saleQty:{1}", new Object[]{ticketBox.getBoxId(), ticketBox.getSaleQty()});
|
|
|
- throw new ServiceException("盲票购买订单创建时,更新盲票销量失败");
|
|
|
- }
|
|
|
+ boolean rst = ticketBoxService.update(
|
|
|
+ new LambdaUpdateWrapper<TicketBox>().set(TicketBox::getSaleQty,
|
|
|
+ ticketBox.getSaleQty() + orderSettleVO.getOrderNum())
|
|
|
+ .set(ticketBox.getSaleQty() + orderSettleVO.getOrderNum() == ticketBox.getQuantity(),
|
|
|
+ TicketBox::getStatus, TicketBoxStatusEnum.PUT_OFF)
|
|
|
+ .eq(TicketBox::getBoxId, ticketBox.getBoxId())
|
|
|
+ .eq(TicketBox::getSaleQty, ticketBox.getSaleQty()));
|
|
|
+ Assert.isTrue(rst,
|
|
|
+ "盲票购买订单创建时,更新盲票销量失败。boxId:" + ticketBox.getBoxId() + "saleQty:" + ticketBox.getSaleQty());
|
|
|
+
|
|
|
// 核销优惠券
|
|
|
String couponIds = verifyUserCoupon(orderSettleVO.getCouponList());
|
|
|
|
|
@@ -116,8 +124,9 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
save(userTicketOrder);
|
|
|
|
|
|
// 查询用户的所属经销商,线上票直接查询
|
|
|
- ChannelUserRel channelUserRel = channelUserRelService.getOne(new LambdaQueryWrapper<ChannelUserRel>()
|
|
|
- .eq(ChannelUserRel::getUserId, userId));
|
|
|
+ ChannelUserRel channelUserRel = channelUserRelService.getOne(
|
|
|
+ new LambdaQueryWrapper<ChannelUserRel>()
|
|
|
+ .eq(ChannelUserRel::getUserId, userId));
|
|
|
for (int i = 0; i < orderSettleVO.getOrderNum(); i++) {
|
|
|
UserTicketOrderItem orderItem = new UserTicketOrderItem();
|
|
|
orderItem.setItemId(bizIdGenerator.newId());
|
|
@@ -133,9 +142,9 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
|
|
|
private String createOfflineOrder(Long userId, TicketOrderSettleVO orderSettleVO) {
|
|
|
Ticket ticket = ticketService.getById(orderSettleVO.getTicketId());
|
|
|
- if (ticket.getStatus() != TicketStatusEnum.NOT_PAY) {
|
|
|
- LogUtil.error(logger, "盲票已付款,ticketId:{0},status:{1}", new Object[]{ticket.getTicketId(), ticket.getStatus()});
|
|
|
- }
|
|
|
+ Assert.isTrue(ticket.getStatus() == TicketStatusEnum.NOT_PAY,
|
|
|
+ "盲票已付款,ticketId:" + ticket.getTicketId());
|
|
|
+
|
|
|
// 核销优惠券
|
|
|
String couponIds = verifyUserCoupon(orderSettleVO.getCouponList());
|
|
|
|
|
@@ -155,8 +164,9 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
save(ticketOrder);
|
|
|
|
|
|
// 查询用户的所属经销商,线上票直接查询
|
|
|
- ChannelUserRel channelUserRel = channelUserRelService.getOne(new LambdaQueryWrapper<ChannelUserRel>()
|
|
|
- .eq(ChannelUserRel::getUserId, userId));
|
|
|
+ ChannelUserRel channelUserRel = channelUserRelService.getOne(
|
|
|
+ new LambdaQueryWrapper<ChannelUserRel>()
|
|
|
+ .eq(ChannelUserRel::getUserId, userId));
|
|
|
UserTicketOrderItem orderItem = new UserTicketOrderItem();
|
|
|
orderItem.setItemId(bizIdGenerator.newId());
|
|
|
orderItem.setOrderId(ticketOrder.getOrderId());
|
|
@@ -168,21 +178,18 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
return ticketOrder.getOrderId();
|
|
|
}
|
|
|
|
|
|
- private String verifyUserCoupon (List<UserCouponVO> userCouponVOList) {
|
|
|
+ private String verifyUserCoupon(List<UserCouponVO> userCouponVOList) {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (UserCouponVO userCouponVO : userCouponVOList) {
|
|
|
UserCoupon userCoupon = userCouponService.getById(userCouponVO.getId());
|
|
|
userCoupon.setVerifyTime(new Date());
|
|
|
userCoupon.setStatus(UserCouponStatusEnum.USED);
|
|
|
boolean rst = userCouponService.updateById(userCoupon);
|
|
|
- if (!rst) {
|
|
|
- LogUtil.error(logger, "盲票购买订单创建时,核销优惠券失败。userCoupon:{0}", new Object[]{JSONObject.toJSONString(userCoupon)});
|
|
|
- throw new ServiceException("盲票购买订单创建时,核销优惠券失败");
|
|
|
- }
|
|
|
+ Assert.isTrue(rst, "盲票购买订单创建时,核销优惠券失败。userCoupon:" + JSONObject.toJSONString(userCoupon));
|
|
|
sb.append(userCouponVO.getId()).append(",");
|
|
|
}
|
|
|
if (sb.length() > 0) {
|
|
|
- return sb.substring(0, sb.length() -1);
|
|
|
+ return sb.substring(0, sb.length() - 1);
|
|
|
}
|
|
|
return sb.toString();
|
|
|
}
|
|
@@ -192,7 +199,8 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
public boolean cancelOrder(String orderId) {
|
|
|
UserTicketOrder userTicketOrder = getById(orderId);
|
|
|
if (userTicketOrder.getStatus() != UserTicketOrderStatusEnum.NOT_PAY) {
|
|
|
- LogUtil.warn(logger, "取消购票订单时,订单状态不是待支付,忽略。orderId:{0}, status:{1}", new Object[]{orderId, userTicketOrder.getStatus()});
|
|
|
+ LogUtil.warn(logger, "取消购票订单时,订单状态不是待支付,忽略。orderId:{0}, status:{1}",
|
|
|
+ orderId, userTicketOrder.getStatus());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -203,26 +211,28 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
.set(UserCoupon::getStatus, UserCouponStatusEnum.UNUSED)
|
|
|
.set(UserCoupon::getVerifyTime, null)
|
|
|
.in(UserCoupon::getId, userCouponIds));
|
|
|
- if (!rst) {
|
|
|
- LogUtil.error(logger, "盲票购买订单取消时,反核销优惠券失败。orderId:{0}, userCouponIds:{1}", new Object[]{orderId, userTicketOrder.getCouponInfo()});
|
|
|
- throw new ServiceException("盲票购买订单取消时,反核销优惠券失败");
|
|
|
- }
|
|
|
+ Assert.isTrue(rst, "盲票购买订单取消时,反核销优惠券失败。orderId:" + orderId + "userCouponIds:"
|
|
|
+ + userTicketOrder.getCouponInfo());
|
|
|
}
|
|
|
if (userTicketOrder.getResource() == TicketTypeEnum.ONLINE) {
|
|
|
// TODO 线上票库存回滚,此处需要按票组累计起来回滚
|
|
|
TicketBox ticketBox = ticketBoxService.getById(userTicketOrder.getBoxId());
|
|
|
- ticketBoxService.update(new LambdaUpdateWrapper<TicketBox>().set(TicketBox::getSaleQty, ticketBox.getSaleQty() - userTicketOrder.getTicketNum())
|
|
|
- .set(ticketBox.getStatus() == TicketBoxStatusEnum.PUT_OFF && ticketBox.getManualOff() != 1,TicketBox::getStatus, TicketBoxStatusEnum.PUT_ON)
|
|
|
- .eq(TicketBox::getBoxId, ticketBox.getBoxId()).eq(TicketBox::getSaleQty, ticketBox.getSaleQty()));
|
|
|
+ ticketBoxService.update(new LambdaUpdateWrapper<TicketBox>().set(TicketBox::getSaleQty,
|
|
|
+ ticketBox.getSaleQty() - userTicketOrder.getTicketNum())
|
|
|
+ .set(
|
|
|
+ ticketBox.getStatus() == TicketBoxStatusEnum.PUT_OFF && ticketBox.getManualOff() != 1,
|
|
|
+ TicketBox::getStatus, TicketBoxStatusEnum.PUT_ON)
|
|
|
+ .eq(TicketBox::getBoxId, ticketBox.getBoxId())
|
|
|
+ .eq(TicketBox::getSaleQty, ticketBox.getSaleQty()));
|
|
|
|
|
|
}
|
|
|
|
|
|
- boolean rst = update(new LambdaUpdateWrapper<UserTicketOrder>().set(UserTicketOrder::getStatus, UserTicketOrderStatusEnum.CANCELED)
|
|
|
- .eq(UserTicketOrder::getOrderId, orderId).eq(UserTicketOrder::getStatus, UserTicketOrderStatusEnum.NOT_PAY));
|
|
|
- if (!rst) {
|
|
|
- LogUtil.error(logger, "盲票购买订单取消时,更新订单状态失败。orderId:{0}", orderId);
|
|
|
- throw new ServiceException("盲票购买订单取消时,更新订单状态失败");
|
|
|
- }
|
|
|
+ boolean rst = update(new LambdaUpdateWrapper<UserTicketOrder>().set(UserTicketOrder::getStatus,
|
|
|
+ UserTicketOrderStatusEnum.CANCELED)
|
|
|
+ .eq(UserTicketOrder::getOrderId, orderId)
|
|
|
+ .eq(UserTicketOrder::getStatus, UserTicketOrderStatusEnum.NOT_PAY));
|
|
|
+ Assert.isTrue(rst, "盲票购买订单取消时,更新订单状态失败。orderId:" + orderId);
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -234,15 +244,33 @@ public class UserTicketOrderServiceImpl extends ServiceImpl<UserTicketOrderMappe
|
|
|
JSONObject.toJSONString(payOrder), JSONObject.toJSONString(ticketOrder));
|
|
|
throw new ServiceException("支付回调用户购票订单处理,订单状态异常,不是待支付。orderId:" + payOrder.getBizId());
|
|
|
}
|
|
|
- // 线上票,要先分配票ticketId
|
|
|
- if (ticketOrder.getResource() == TicketTypeEnum.ONLINE) {
|
|
|
- List<UserTicketOrderItem> orderItemList = userTicketOrderItemService.list(new LambdaQueryWrapper<UserTicketOrderItem>()
|
|
|
- .eq(UserTicketOrderItem::getOrderId, ticketOrder.getOrderId()));
|
|
|
- }
|
|
|
- // 更新订单状态为已完成
|
|
|
+ List<UserTicketOrderItem> orderItemList = userTicketOrderItemService.list(
|
|
|
+ new LambdaQueryWrapper<UserTicketOrderItem>()
|
|
|
+ .eq(UserTicketOrderItem::getOrderId, ticketOrder.getOrderId()));
|
|
|
|
|
|
- // 开幸运数字
|
|
|
+ for (UserTicketOrderItem orderItem : orderItemList) {
|
|
|
+ Ticket ticket;
|
|
|
+ if (ticketOrder.getResource() == TicketTypeEnum.ONLINE) {
|
|
|
+ // 线上票,要先分配票ticketId
|
|
|
+ ticket = ticketService.getRandOne(new QueryWrapper<Ticket>()
|
|
|
+ .eq("box_id", ticketOrder.getBoxId()).eq("status", TicketStatusEnum.NOT_PAY));
|
|
|
+ orderItem.setTicketId(ticket.getTicketId());
|
|
|
+ boolean rst = userTicketOrderItemService.updateById(orderItem);
|
|
|
+ Assert.isTrue(rst, "支付回调用户购票订单处理,更新订单明细的盲票ID失败,itemId:" + orderItem.getItemId());
|
|
|
+ } else {
|
|
|
+ ticket = ticketService.getById(orderItem.getTicketId());
|
|
|
+ }
|
|
|
+ // 开幸运数字,更新ticket状态为已激活
|
|
|
+ ticket.setPlainLuckyNum(Integer.valueOf(RSAUtil.decrypt(ticket.getCipherLuckyNum())));
|
|
|
+ ticket.setStatus(TicketStatusEnum.ACTIVATED);
|
|
|
+ boolean rtn = ticketService.updateById(ticket);
|
|
|
+ Assert.isTrue(rtn, "支付回调用户购票订单处理,更新盲票状态失败,ticketId:" + ticket.getTicketId());
|
|
|
+ }
|
|
|
|
|
|
- return false;
|
|
|
+ // 更新订单状态为已完成
|
|
|
+ ticketOrder.setStatus(UserTicketOrderStatusEnum.FINISHED);
|
|
|
+ boolean updateRst = updateById(ticketOrder);
|
|
|
+ Assert.isTrue(updateRst, "支付回调用户购票订单处理,更新订单状态失败,orderId:" + ticketOrder.getOrderId());
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|