OrderService.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.example.xiaoshiweixinback.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.example.xiaoshiweixinback.business.exception.BusinessException;
  6. import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
  7. import com.example.xiaoshiweixinback.business.utils.BatchNoUtil;
  8. import com.example.xiaoshiweixinback.domain.*;
  9. import com.example.xiaoshiweixinback.entity.dto.ticket.TicketProcessUpDTO;
  10. import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
  11. import com.example.xiaoshiweixinback.entity.vo.person.PersonVO;
  12. import com.example.xiaoshiweixinback.entity.weixinPay.GetPayTicketVO;
  13. import com.example.xiaoshiweixinback.entity.weixinPay.GoodVO;
  14. import com.example.xiaoshiweixinback.entity.weixinPay.JsApiDTO;
  15. import com.example.xiaoshiweixinback.entity.weixinPay.WeiXinPayDTO;
  16. import com.example.xiaoshiweixinback.factorys.goodFactory.GoodFactory;
  17. import com.example.xiaoshiweixinback.factorys.goodFactory.GoodImp;
  18. import com.example.xiaoshiweixinback.mapper.AssoPersonProductMapper;
  19. import com.example.xiaoshiweixinback.mapper.OrderMapper;
  20. import com.example.xiaoshiweixinback.service.weixinpay.AssoPayOrderGoodService;
  21. import com.example.xiaoshiweixinback.service.weixinpay.AssoPayOrderVoucherService;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.context.annotation.Lazy;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.util.List;
  27. /**
  28. * @author admin
  29. * @description
  30. * @createDate 2024-04-10 15:45:40
  31. */
  32. @Service
  33. public class OrderService extends ServiceImpl<OrderMapper, Order> {
  34. @Autowired
  35. @Lazy
  36. private VipService vipService;
  37. @Autowired
  38. private TicketService ticketService;
  39. @Autowired
  40. private AssoPayOrderGoodService assoPayOrderGoodService;
  41. @Autowired
  42. private AssoPayOrderVoucherService assoPayOrderVoucherService;
  43. @Autowired
  44. private GoodFactory goodFactory;
  45. @Autowired
  46. private AssoPersonVoucherService assoPersonVoucherService;
  47. @Transactional(rollbackFor = Exception.class)
  48. public Order addOrder(WeiXinPayDTO weiXinPayDTO, String tradeNo, PersonnelVO personVO) {
  49. this.verifyPrice(weiXinPayDTO,personVO);
  50. Integer type = weiXinPayDTO.getGoodType();
  51. Double price = weiXinPayDTO.getPrice();
  52. Double finalPrice = weiXinPayDTO.getFinalPrice();
  53. Double discount = weiXinPayDTO.getDiscount();
  54. List<GoodVO> goodVOS = weiXinPayDTO.getGoods();
  55. List<Integer> assoVoucherIds = weiXinPayDTO.getPersonVoucherIds();
  56. Order order = new Order();
  57. order.setOrderState(0);
  58. order.setTradeNo(tradeNo);
  59. order.setPersonUuid(personVO.getUuid());
  60. order.setOrgPrice(price);
  61. order.setTruePrice(finalPrice);
  62. order.setPersonUuid(personVO.getUuid());
  63. order.setDiscount(discount);
  64. order.insert();
  65. assoPayOrderGoodService.addAssoPayOrderGood(order.getId(), type, goodVOS);
  66. assoPayOrderVoucherService.addAssoPayOrderVoucher(order.getId(), assoVoucherIds);
  67. assoPersonVoucherService.useVoucher(assoVoucherIds, 2);
  68. return order;
  69. }
  70. public Order getOrderByTradeNo(String tradeNo) {
  71. LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
  72. queryWrapper.eq(Order::getTradeNo, tradeNo);
  73. Order order = this.getOne(queryWrapper);
  74. return order;
  75. }
  76. @Transactional(rollbackFor = Exception.class)
  77. public void payBack(String tradeNo) {
  78. Order order = this.getOrderByTradeNo(tradeNo);
  79. if (order.getOrderState() != 0) {
  80. return;
  81. }
  82. order.setOrderState(1);
  83. order.updateById();
  84. List<AssoPayOrderGood> assoPayOrderGoods = assoPayOrderGoodService.getPayOrderGoodByOrderId(order.getId());
  85. Integer orderType =assoPayOrderGoods.get(0).getGoodType();
  86. GoodImp goodImp = goodFactory.getClass(orderType);
  87. goodImp.activeGood(order, assoPayOrderGoods);
  88. List<Integer> assoPersonVoucherIds = assoPayOrderVoucherService.getAssoVoucherIdsByOrder(order.getId());
  89. assoPersonVoucherService.useVoucher(assoPersonVoucherIds, 1);
  90. }
  91. /**
  92. * 校验订单价格
  93. */
  94. private void verifyPrice(WeiXinPayDTO weiXinPayDTO, PersonnelVO personVO) {
  95. String personUuid = personVO.getUuid();
  96. Integer type = weiXinPayDTO.getGoodType();
  97. List<GoodVO> goodVOS = weiXinPayDTO.getGoods();
  98. List<Integer> assoVoucherIds = weiXinPayDTO.getPersonVoucherIds();
  99. Double price = weiXinPayDTO.getFinalPrice();
  100. Double discount = weiXinPayDTO.getDiscount();
  101. if(discount==null){
  102. discount=1d;
  103. }
  104. //校验优惠卷是否能使用
  105. Double voucherMon = assoPersonVoucherService.verifyVouchers(assoVoucherIds, personVO);
  106. //计算商品价格
  107. GoodImp goodImp = goodFactory.getClass(type);
  108. Double allPrice = goodImp.computePrice(goodVOS);
  109. Double withVocherPrice =allPrice-voucherMon;
  110. if(withVocherPrice<=0){
  111. withVocherPrice=0d;
  112. }
  113. //查看是否有折扣
  114. // 计算价格 (商品总价格-优惠券) *折扣
  115. Double truePrice =withVocherPrice*discount;
  116. if(!truePrice.equals(price)){
  117. throw new BusinessException(ExceptionEnum.BUSINESS_ERROR,"价格错误");
  118. }
  119. }
  120. }