package com.sifang.modules.task;

import com.sifang.modules.customer.Service.CrmCustomerEntryRentManagementService;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Component
public class RentTask {

    Logger logger = LoggerFactory.getLogger(RentTask.class);
    @Autowired
    private CrmCustomerEntryRentManagementService rentManagementService;

    //每周全量查询应付日期是三个月前的数据  分三次查询 每次查询一个月
    @Scheduled(cron = "0 40 0 * * 1?")//每周一凌晨0点40分执行
    public void rentTaskFull(){
        logger.info(">>>>>>>>RentTask  定时任务开始");
        long begin = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        //获取三个月前的日期
        calendar.add(Calendar.MONTH, -3);
        String time1 = dateFormat.format(calendar.getTime());
        //获取两个月最前的日期
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.MONTH, -2);
        String time2 = dateFormat.format(calendar2.getTime());
        //获取一个月前的日期
        Calendar calendar3 = Calendar.getInstance();
        calendar3.add(Calendar.MONTH, -1);
        String time3 = dateFormat.format(calendar3.getTime());
        //获取今天日期
        String time4 = dateFormat.format(Calendar.getInstance().getTime());
        //全量
        rentManagementService.dealRentList("FULL",time1,time2, null);
        rentManagementService.dealRentList("FULL",time2,time3, null);
        rentManagementService.dealRentList("FULL",time3,time4, null);
        long end = System.currentTimeMillis();
        logger.info("RentTask 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    //每日增量查询数据
    @Scheduled(cron = "0 20 0 * * ?")//每天凌晨0点20分执行
    public void rentTaskInc(){
        logger.info(">>>>>>>>RentTask  定时任务开始");
        long begin = System.currentTimeMillis();
        //增量
        rentManagementService.dealRentList("INC",null,null, null);
        long end = System.currentTimeMillis();
        logger.info("RentTask 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    //每日增量查询数据2
    @Scheduled(cron = "0 20 0 * * ?")//每天凌晨0点20分执行
    public void sysPaidDateTask(){
        logger.info(">>>>>>>>sysPaidDateTask  定时任务开始");
        long begin = System.currentTimeMillis();
        // 查询已付日期并更新
        rentManagementService.sysPaidDate();
        long end = System.currentTimeMillis();
        logger.info("sysPaidDateTask 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    /**
     * 初始化 先同步电站数据，再同步租金数据
     */
    //获取友夸电站信息
    @Scheduled(cron = "0 8 0 * * ?")//每天凌晨0点08分执行
    public void getYouKuaStationData(){
        logger.info("getYouKuaStationData 定时任务开始");
        long begin = System.currentTimeMillis();
        rentManagementService.getYouKuaStationData();
        long end = System.currentTimeMillis();
        logger.info("getYouKuaStationData 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }
}
