package com.sifang.modules.task;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.OperatingLeaseService;
import com.sifang.modules.customer.Service.YueXiuBankCardService;
import com.sifang.modules.customer.Service.YueXiuProjectOperatingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class YueXiuGetStatusTask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private YueXiuProjectOperatingService yueXiuProjectOperatingService;

    @Autowired
    private YueXiuBankCardService yueXiuBankCardService;

    @Autowired
    OperatingLeaseService operatingLeaseService;

    //获取第二次进件后，越秀那边的进件状态
    @Scheduled(cron = "0 0/10 * * * ?")
    void getYueXiuStatus(){
       logger.info(">>>>>>>>getYueXiuStatus  定时任务开始");
        long begin = System.currentTimeMillis();
        yueXiuProjectOperatingService.getNeedToAskJJStatus();
        // 获取并网验收后 需要查询进件状态的电站,查询进件状态 定时任务调用方法
        yueXiuProjectOperatingService.getNeedToAskBWStatus();
        long end = System.currentTimeMillis();
        logger.info("getYueXiuStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }
    //更新请款状态
    @Scheduled(cron = "0 0/10 * * * ?")
    void updatePaymentStatus(){
        logger.info(">>>>>>>>updatePaymentStatus  定时任务开始");
        long begin = System.currentTimeMillis();
        yueXiuProjectOperatingService.updatePaymentStatus();
        long end = System.currentTimeMillis();
        logger.info("updatePaymentStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    //获取开卡状态
    @Scheduled(cron = "0 0/8 * * * ?")
    void getYueXiuOpenCardStatus(){
        logger.info("getYueXiuOpenCardStatus 获取开卡状态");
        long begin = System.currentTimeMillis();
        yueXiuBankCardService.activityGetOpenCardStatus();
        long end = System.currentTimeMillis();
        logger.info("getYueXiuOpenCardStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    //获取越秀合同(第一次进件通过后，越秀不推送合同生成状态)
    @Scheduled(cron = "0 0/6 * * * ?")
    void getYueXiuContract(){
        logger.info("getYueXiuContract 获取农户合同");
        long begin = System.currentTimeMillis();
        yueXiuProjectOperatingService.getYueXiuContract();
        long end = System.currentTimeMillis();
        logger.info("getYueXiuContract 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    //获取越秀续保保单状态
    @Scheduled(cron = "0 0/10 * * * ?")
    void getYueXiuRenewalInsuranceStatus(){
        logger.info("getYueXiuRenewalInsuranceStatus 获取越秀续保保单状态");
        long begin = System.currentTimeMillis();
        List<Record> records = Db.find(" SELECT *FROM crm_customer_entry_insurance WHERE `status` = '1' AND policy_type = '1' AND renewal_status !='审批通过' ");
        for (Record record : records) {
            try {
                operatingLeaseService.getYueXiuRenewalInsuranceStatus(record.getStr("id"));
            }catch (Exception e){
                logger.error("getYueXiuRenewalInsuranceStatus 执行异常，error :{}  id :{} " ,e.getMessage(), record.getStr("id"));
            }
        }
        long end = System.currentTimeMillis();
        logger.info("getYueXiuRenewalInsuranceStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    //获取非本系统电站越秀续保保单状态
    @Scheduled(cron = "0 10 04 * * ?")
    void getYueXiuNoSysRenewalInsuranceStatus(){
        logger.info("getYueXiuNoSysRenewalInsuranceStatus 获取非本系统电站越秀续保保单状态");
        long begin = System.currentTimeMillis();
        List<Record> records = Db.find(" SELECT *FROM crm_customer_no_system_entry_insurance WHERE `status` = '1' AND policy_type = '1' AND renewal_status !='审批通过' ");
        for (Record record : records) {
            try {
                operatingLeaseService.getYueXiuNoSysRenewalInsuranceStatus(record.getStr("id"));
            }catch (Exception e){
                logger.error("getYueXiuNoSysRenewalInsuranceStatus 执行异常，error :{}  id :{} " ,e.getMessage(), record.getStr("id"));
            }
        }
        long end = System.currentTimeMillis();
        logger.info("getYueXiuNoSysRenewalInsuranceStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }
}
