package com.ai.da.common.task;

import com.ai.da.common.enums.PayTypeEnum;
import com.ai.da.mapper.primary.entity.OrderInfo;
import com.ai.da.service.*;
import com.paypal.http.exceptions.SerializeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@Component
public class PaymentTask {

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private StripeService stripeService;

    @Resource
    private AffiliateService affiliateService;

    // 考虑删除该定时任务（原因：之后的订单列允许用户查看发票，发票未过期时仍可以支付，所以不需要手动使订单过期）
//    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirmForStripe() throws SerializeException {

        // 查看超过30分钟以上仍未支付的订单 置为超时订单
        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(30, PayTypeEnum.STRIPE.getType());

        for (OrderInfo orderInfo : orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单 ===> {}", orderNo);

            //核实订单状态：调用支付宝查单接口
            stripeService.checkOrderStatus(orderNo);

        }
    }

    @Resource
    private PayPalCheckoutService payPalCheckoutService;

    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirmForPaypal() throws SerializeException {

//        log.info("PayPal orderConfirm 被执行......");

        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(30, PayTypeEnum.PAYPAL.getType());

        for (OrderInfo orderInfo : orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单 ===> {}", orderNo);

            //核实订单状态：调用支付宝查单接口
            payPalCheckoutService.checkOrderStatus(orderNo);

        }
    }

    @Resource
    private AliPayService aliPayService;

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
//    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirmForAlipay(){
/*
        log.info("Alipay orderConfirm 被执行......");

        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayTypeEnum.ALIPAY.getType());

        for (OrderInfo orderInfo : orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单 ===> {}", orderNo);

            //核实订单状态：调用支付宝查单接口
            aliPayService.checkOrderStatus(orderNo);
        }*/
    }

    // ！！关闭此定时器，改为提前三天站内信提醒！！
    // 提前7天向用户发送提醒邮件,每天早上8点执行
//    @Scheduled(cron = "0 0 8 * * ?")
//    public void subscriptionReminder(){
//        stripeService.subscriptionReminder();
//    }


    // 如果有订阅已创建，但是没有发邮件通知的，需要主动获取回调信息并向用户发送邮件
    public void checkSubscriptionPayment(){
        //
    }

    @Scheduled(cron = "0 */5 * * * *") // Run every 5 minutes
    public void updateAffiliateInfoWithPayment(){
//        log.info("佣金计算定时器");
        affiliateService.updateAffiliateInfoWithPayment();
    }

    // 定时同步（每分钟一次）
    @Scheduled(fixedRate = 60000)
    public void syncLinkViewCountToDB(){
        affiliateService.syncLinkViewCountToDB();
    }

//    @Scheduled(cron = "0 0 8 28-31 * ?")
    public void commissionSummaryReminder(){
        // 每个月末的最后一天的早上八点执行
        LocalDate today = LocalDate.now();
        // 判断是否为月底
        if (today.plusDays(1).getDayOfMonth() == 1) {
            log.info("今天是月底，执行佣金结算提醒任务！");
            affiliateService.commissionCalculation(null, null);
        }
    }

    @Scheduled(cron = "0 */5 * * * *") // Run every 5 minutes
    public void calcCouponsCommission(){
        log.info("优惠券佣金计算定时器");
        affiliateService.calcCouponsCommission();
    }

}
