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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.base.MqListener;
import com.bohuikeji.frame.module.admin.entity.sys.SysError;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * 支付超时处理
 * @author 孙劲辉
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PayTimoutListener extends MqListener<String> {
    final ITPayService payService;
    final ITCourierPayService courierPayService;
    final ITCustomerPayService customerPayService;
    final ITRewardPayService rewardPayService;
    final ITStoreWalletPayService storeWalletPayService;
    final ITDiscountPayService discountPayService;
    final ITRentalPayService rentalPayService;
    @Override
    public boolean onReceive(String payNum) {
        //订单支付记录
        TPay pay=payService.getByNum(payNum);
        //入驻支付记录
        TCourierPay tCourierPay = courierPayService.getOne(new QueryWrapper<TCourierPay>().eq("pay_num",payNum));
        //钱包充值记录
        TCustomerPay tCustomerPay = customerPayService.getOne(new QueryWrapper<TCustomerPay>().eq("pay_num",payNum));
        //订单打赏记录
        TRewardPay rewardPay = rewardPayService.getOne(new QueryWrapper<TRewardPay>().eq("pay_num",payNum));
        //门店钱包充值
        TStoreWalletPay walletPay =storeWalletPayService.getOne(new QueryWrapper<TStoreWalletPay>().eq("pay_num",payNum));
        //购买优惠支付记录
        TDiscountPay discountPay = discountPayService.getOne(new QueryWrapper<TDiscountPay>().eq("pay_num",payNum));
        //租车服务支付记录
        TRentalPay rentalPay =rentalPayService.getOne(new QueryWrapper<TRentalPay>().eq("rental_pay_num",payNum));
        if(pay==null && tCourierPay == null && tCustomerPay == null && rewardPay == null && walletPay==null && discountPay==null && rentalPay == null){
            log.error("支付超时更新失败，支付单：{}号已丢失",payNum);
            SysError.saveError("支付超时更新失败，支付单：{0}号已丢失",payNum);
            return true;
        }
        //订单支付超时
        if(pay!=null && "待付款".equals(pay.getPayStatus()) && tCourierPay == null && tCustomerPay == null && rewardPay==null && walletPay==null && discountPay==null && rentalPay == null){
            payService.update(new UpdateWrapper<TPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("pay_id",pay.getPayId()));
            return true;
        }
        //钱包充值超时
        if(tCustomerPay != null && "待付款".equals(tCustomerPay.getPayStatus()) && tCourierPay == null &&  pay == null && rewardPay==null && walletPay==null && discountPay==null && rentalPay == null){
            customerPayService.update(new UpdateWrapper<TCustomerPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("customer_pay_id",tCustomerPay.getCustomerPayId()));
            return true;
        }
        //入驻缴费超时
        if(tCourierPay != null && "待付款".equals(tCourierPay.getPayStatus()) && tCustomerPay == null &&  pay == null && rewardPay==null && walletPay==null && discountPay==null && rentalPay == null){
            courierPayService.update(new UpdateWrapper<TCourierPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("courier_pay_id",tCourierPay.getCourierPayId()));
            return true;
        }
        //入驻缴费超时
        if(rewardPay != null && "待付款".equals(rewardPay.getPayStatus()) && tCustomerPay == null &&  pay == null && tCourierPay==null && walletPay==null && discountPay==null && rentalPay == null){
            rewardPayService.update(new UpdateWrapper<TRewardPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("reward_id",rewardPay.getRewardId()));
            return true;
        }
        //门店钱包充值超时
        if(walletPay != null && "待付款".equals(walletPay.getPayStatus()) && tCustomerPay == null &&  pay == null && tCourierPay==null && rewardPay==null && discountPay==null && rentalPay == null){
            storeWalletPayService.update(new UpdateWrapper<TStoreWalletPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("store_pay_id",walletPay.getStorePayId()));
            return true;
        }
        //购买优惠超时记录
        if(discountPay != null && "待付款".equals(discountPay.getPayStatus()) && tCustomerPay == null &&  pay == null && tCourierPay==null && rewardPay==null && walletPay==null && rentalPay == null){
            discountPayService.update(new UpdateWrapper<TDiscountPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("dispay_id",discountPay.getDispayId()));
            return true;
        }
        //租车服务超时记录
        if(rentalPay != null && "待付款".equals(rentalPay.getPayStatus()) && tCustomerPay == null &&  pay == null && tCourierPay==null && rewardPay==null && walletPay==null && discountPay == null){
            rentalPayService.update(new UpdateWrapper<TRentalPay>().set("pay_status","已超时").set("pay_time", LocalDateTime.now()).eq("rental_pay_id",rentalPay.getRentalPayId()));
            return true;
        }
        return true;
    }

    @Override
    public void onError(String msg, Exception e) {

    }
}
