package com.bohuikeji.delivery.backstage.listener.pay;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.constants.payConst;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.entity.discount.TDiscountPay;
import com.bohuikeji.delivery.backstage.entity.rental.TRentalPay;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.entity.sys.SysError;
import com.bohuikeji.frame.module.admin.entity.vo.AliReturnPayVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 支付宝回调业务公共类
 * @author sjh
 * @date 2022/2/22 17:04
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class PayCallBackService {

    final ITRewardPayService rewardPayService;
    final ITCourierPayService courierPayService;
    final ITCustomerPayService customerPayService;
    final ITPayService payService;
    final ITStoreWalletPayService storeWalletPayService;
    final ITCustomerWalletService customerWalletService;
    final ITwalletDetailService twalletDetailService;
    final ITGiveDetailService giveDetailService;
    final ITDiscountPayService discountPayService;
    final ITRentalPayService rentalPayService;

    public void getAllPay(JSONObject jsonObject, AliReturnPayVo req) throws Exception {
        String payNum = payConst.getPayNum(jsonObject,req);
        if(payNum==null){
            SysError.saveError("支付回调处理失败，订单：{0}丢失，系统未找到记录",jsonObject==null? req.getOut_trade_no():jsonObject);return;
        }
        //骑手入住缴费
        TCourierPay tCourierPay = courierPayService.getOne(new QueryWrapper<TCourierPay>().eq("pay_num",payNum).eq("pay_status","待付款"));
        //客户钱包重置
        TCustomerPay tCustomerPay = customerPayService.getOne(new QueryWrapper<TCustomerPay>().eq("pay_num",payNum).eq("pay_status","待付款"));
        //客户打赏
        TRewardPay rewardPay = rewardPayService.getOne(new QueryWrapper<TRewardPay>().eq("pay_num",payNum).eq("pay_status","待付款"));
        //门店钱包充值
        TStoreWalletPay walletPay = storeWalletPayService.getOne(new QueryWrapper<TStoreWalletPay>().eq("pay_num",payNum).eq("pay_status","待付款"));
        //订单支付
        TPay pay = payService.getOne(new QueryWrapper<TPay>().eq("pay_num",payNum).eq("pay_status","待付款"));
        //优惠卷支付
        TDiscountPay discountPay =discountPayService.getOne(new QueryWrapper<TDiscountPay>().eq("pay_num",payNum).eq("pay_status","待付款"));
        //租车支付
        TRentalPay rentalPay =rentalPayService.getOne(new QueryWrapper<TRentalPay>().eq("rental_pay_num",payNum).eq("pay_status","待付款"));
        this.handleService(pay,tCourierPay,tCustomerPay,rewardPay,walletPay,discountPay,rentalPay,jsonObject,req);
    }
    public void handleService(TPay pay, TCourierPay tCourierPay, TCustomerPay tCustomerPay, TRewardPay rewardPay, TStoreWalletPay walletPay, TDiscountPay discountPay,TRentalPay rentalPay,JSONObject jsonObject, AliReturnPayVo req) throws Exception {
        //微信、支付宝 入驻缴费回调：根据回调回来的订单号查询支付记录，并判断支付记录属于业务支付或是入驻缴费，如果业务支付为空，入驻缴费不为空并且是“待付款”状态则进行业务支付
        log.info("支付回调信息1：{}",jsonObject);
        log.info("支付回调信息2：{}",req);
        if(tCourierPay != null){
            log.info("进入入住缴费");
            if("已超时".equals(tCourierPay.getPayStatus())){
                courierPayService.updateCourierPay(req,tCourierPay,jsonObject);return;
            }
            if("待付款".equals(tCourierPay.getPayStatus())){
                courierPayService.updateCourierPay(req,tCourierPay,jsonObject);return;
            }
        }
        //微信钱包充值回调：根据回调回来的订单号查询支付记录，并判断支付记录属于业务支付或是入驻缴费，如果业务支付为空，入驻缴费不为空并且是“待付款”状态则进行业务支付
        if(tCustomerPay != null && "待付款".equals(tCustomerPay.getPayStatus()) ){
            log.info("进入钱包充值");
            if(customerPayService.updateWalletPay(req,tCustomerPay,jsonObject)){
                TCustomerWallet customerWallet = customerWalletService.getById(tCustomerPay.getWalletId());
                twalletDetailService.saveDetail(tCustomerPay,null,null,customerWallet,"余额充值");
                giveDetailService.saveGiveDetail(tCustomerPay,customerWallet,"充值赠金");
                return;
            }
        }
        //微信、支付宝、百度、抖音、快手 业务支付调用方法
        if(pay != null && "待付款".equals(pay.getPayStatus())){
            log.info("进入订单付款支付");
           /* if(req != null){
                payService.aliCallbackPay(req,pay);return;
            }
            if(jsonObject != null){
                payService.wxCallbackPay(jsonObject,pay);return;
            }*/
            payService.callbackPayServis(jsonObject,req,pay);return;
        }
        //微信 订单打赏支付回调
        if(rewardPay != null && "待付款".equals(rewardPay.getPayStatus())){
            log.info("订单打赏缴费");
            rewardPayService.wxRewardPay(jsonObject,rewardPay);
            return;
        }
        //微信 门店钱包充值
        if(walletPay != null && "待付款".equals(walletPay.getPayStatus())){
            log.info("门店钱包充值");
            storeWalletPayService.wxWalletPay(jsonObject,walletPay);
            return;
        }
        //优惠购买支付
        if(discountPay != null && "待付款".equals(discountPay.getPayStatus())){
            log.info("优惠购买支付");
            discountPayService.handleDiscountService(jsonObject,req,discountPay,null,null);
            return;
        }
        //租车服务支付
        if(rentalPay != null && "待付款".equals(rentalPay.getPayStatus())){
            log.info("进入租车服务支付");
            if(req != null){
                rentalPayService.aliCallbackPay(req,rentalPay);return;
            }
            /*if(jsonObject != null){
                rentalPayService.wxCallbackPay(jsonObject,rentalPay);return;
            }*/
            return;
        }
    }

}
