package com.malk.cloudpure.schedule;

import com.malk.cloudpure.service.CPClient;
import com.malk.utils.UtilDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.util.Date;

/**
 * @EnableScheduling 开启定时任务 [配置参考McScheduleTask]
 */
@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnProperty(name = {"spel.scheduling"})
public class CPScheduleTask {

    @Autowired
    private CPClient cpClient;

    /**
     * 同步销售部门人员 [每日13.30执行1次]
     */
    @Scheduled(cron = "0 30 13 * * ?")
    public void task_1() {
        try {
            cpClient.syncBusinessUserInfo();
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 同步钉钉日报数据 [18-23点, 每59执行1次]
     */
    @Scheduled(cron = "0 59 18-23 * * ?")
    public void task_2() {
        try {
            String sDate = UtilDateTime.formatDate(new Date());
            cpClient.syncDingTalkLogForRecord(sDate);
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 同步销帮帮跟进记录, 商机, 合同, 回款 - [全量] [09-23点, 每59执行1次]
     */
    @Scheduled(cron = "0 59 09-23 * * ?")
    public void task_3() {
        try {
            LocalDate date = LocalDate.now();
            cpClient.syncXBongBongForRecord_all(date);
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }

    /**
     * 推送群消息卡片 [9.30推前一天, 20.30推当日]
     */
    @Scheduled(cron = "0 30 9,20 * * 1,2,3,4,5")
    public void task_5() {

        LocalDate dateNow = LocalDate.now();
        if (new Date().getHours() < 12) {
            dateNow = dateNow.plusDays(-1);
        }
        try {
            String tDate = UtilDateTime.formatLocalDate(dateNow);
            cpClient.sendCardMessage(tDate, "组织数字化团队", "业务数字化团队", "协同数字化团队");
        } catch (Exception e) {
            // 记录错误信息
            e.printStackTrace();
        }
    }
}