123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.example.xiaoshiweixinback.service;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.example.xiaoshiweixinback.business.exception.BusinessException;
- import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
- import com.example.xiaoshiweixinback.business.utils.BatchNoUtil;
- import com.example.xiaoshiweixinback.domain.*;
- import com.example.xiaoshiweixinback.entity.dto.ticket.TicketProcessUpDTO;
- import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
- import com.example.xiaoshiweixinback.entity.vo.person.PersonVO;
- import com.example.xiaoshiweixinback.entity.weixinPay.GetPayTicketVO;
- import com.example.xiaoshiweixinback.entity.weixinPay.GoodVO;
- import com.example.xiaoshiweixinback.entity.weixinPay.JsApiDTO;
- import com.example.xiaoshiweixinback.entity.weixinPay.WeiXinPayDTO;
- import com.example.xiaoshiweixinback.factorys.goodFactory.GoodFactory;
- import com.example.xiaoshiweixinback.factorys.goodFactory.GoodImp;
- import com.example.xiaoshiweixinback.mapper.AssoPersonProductMapper;
- import com.example.xiaoshiweixinback.mapper.OrderMapper;
- import com.example.xiaoshiweixinback.service.weixinpay.AssoPayOrderGoodService;
- import com.example.xiaoshiweixinback.service.weixinpay.AssoPayOrderVoucherService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.List;
- /**
- * @author admin
- * @description
- * @createDate 2024-04-10 15:45:40
- */
- @Service
- public class OrderService extends ServiceImpl<OrderMapper, Order> {
- @Autowired
- @Lazy
- private VipService vipService;
- @Autowired
- private TicketService ticketService;
- @Autowired
- private AssoPayOrderGoodService assoPayOrderGoodService;
- @Autowired
- private AssoPayOrderVoucherService assoPayOrderVoucherService;
- @Autowired
- private GoodFactory goodFactory;
- @Autowired
- private AssoPersonVoucherService assoPersonVoucherService;
- @Transactional(rollbackFor = Exception.class)
- public Order addOrder(WeiXinPayDTO weiXinPayDTO, String tradeNo, PersonnelVO personVO) {
- this.verifyPrice(weiXinPayDTO,personVO);
- Integer type = weiXinPayDTO.getGoodType();
- Double price = weiXinPayDTO.getPrice();
- Double finalPrice = weiXinPayDTO.getFinalPrice();
- Double discount = weiXinPayDTO.getDiscount();
- List<GoodVO> goodVOS = weiXinPayDTO.getGoods();
- List<Integer> assoVoucherIds = weiXinPayDTO.getPersonVoucherIds();
- Order order = new Order();
- order.setOrderState(0);
- order.setTradeNo(tradeNo);
- order.setPersonUuid(personVO.getUuid());
- order.setOrgPrice(price);
- order.setTruePrice(finalPrice);
- order.setPersonUuid(personVO.getUuid());
- order.setDiscount(discount);
- order.insert();
- assoPayOrderGoodService.addAssoPayOrderGood(order.getId(), type, goodVOS);
- assoPayOrderVoucherService.addAssoPayOrderVoucher(order.getId(), assoVoucherIds);
- assoPersonVoucherService.useVoucher(assoVoucherIds, 2);
- return order;
- }
- public Order getOrderByTradeNo(String tradeNo) {
- LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(Order::getTradeNo, tradeNo);
- Order order = this.getOne(queryWrapper);
- return order;
- }
- @Transactional(rollbackFor = Exception.class)
- public void payBack(String tradeNo) {
- Order order = this.getOrderByTradeNo(tradeNo);
- if (order.getOrderState() != 0) {
- return;
- }
- order.setOrderState(1);
- order.updateById();
- List<AssoPayOrderGood> assoPayOrderGoods = assoPayOrderGoodService.getPayOrderGoodByOrderId(order.getId());
- Integer orderType =assoPayOrderGoods.get(0).getGoodType();
- GoodImp goodImp = goodFactory.getClass(orderType);
- goodImp.activeGood(order, assoPayOrderGoods);
- List<Integer> assoPersonVoucherIds = assoPayOrderVoucherService.getAssoVoucherIdsByOrder(order.getId());
- assoPersonVoucherService.useVoucher(assoPersonVoucherIds, 1);
- }
- /**
- * 校验订单价格
- */
- private void verifyPrice(WeiXinPayDTO weiXinPayDTO, PersonnelVO personVO) {
- String personUuid = personVO.getUuid();
- Integer type = weiXinPayDTO.getGoodType();
- List<GoodVO> goodVOS = weiXinPayDTO.getGoods();
- List<Integer> assoVoucherIds = weiXinPayDTO.getPersonVoucherIds();
- Double price = weiXinPayDTO.getFinalPrice();
- Double discount = weiXinPayDTO.getDiscount();
- if(discount==null){
- discount=1d;
- }
- //校验优惠卷是否能使用
- Double voucherMon = assoPersonVoucherService.verifyVouchers(assoVoucherIds, personVO);
- //计算商品价格
- GoodImp goodImp = goodFactory.getClass(type);
- Double allPrice = goodImp.computePrice(goodVOS);
- Double withVocherPrice =allPrice-voucherMon;
- if(withVocherPrice<=0){
- withVocherPrice=0d;
- }
- //查看是否有折扣
- // 计算价格 (商品总价格-优惠券) *折扣
- Double truePrice =withVocherPrice*discount;
- if(!truePrice.equals(price)){
- throw new BusinessException(ExceptionEnum.BUSINESS_ERROR,"价格错误");
- }
- }
- }
|