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 { @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 goodVOS = weiXinPayDTO.getGoods(); List 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 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 assoPayOrderGoods = assoPayOrderGoodService.getPayOrderGoodByOrderId(order.getId()); Integer orderType =assoPayOrderGoods.get(0).getGoodType(); GoodImp goodImp = goodFactory.getClass(orderType); goodImp.activeGood(order, assoPayOrderGoods); List 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 goodVOS = weiXinPayDTO.getGoods(); List 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,"价格错误"); } } }