/**
 * Created by T on 2020/5/21.
 */

global class MonthEndAutoJudgeSchedule implements Schedulable {

    // //  需在每个月最后一天运行
    // global void execute(SchedulableContext SC) {
    //     // 获取当前月份的第一天
    //     Date firstDayOfMonth = Date.today().toStartOfMonth();
    //     // 将月份推到下一个月
    //     Date nextMonth = Date.newInstance(firstDayOfMonth.year(), firstDayOfMonth.month() + 1, 1);
    //     // 获取当前月份最后一天
    //     Date lastDayOfMonth = nextMonth.addDays(-1);

    //     if (String.valueOf(Date.today()).equals(String.valueOf(lastDayOfMonth))) {
    //         Database.executeBatch(new MonthEndAutoJudgeBatch(),20);
    //     }
    // }

    //每个月最后一个工作日凌晨运行完毕 CHAN-BUZC7V update by rentx 2020-11-05 start
    global void execute(SchedulableContext SC) {
        // 获取当前月份的第一天
        Date firstDayOfMonth = Date.today().toStartOfMonth();
        // 将月份推到下一个月
        Date nextMonth = Date.newInstance(firstDayOfMonth.year(), firstDayOfMonth.month() + 1, 1);
        // 获取当前月份最后一天
        Date lastDayOfMonth = nextMonth.addDays(-1);
        //获取当前月份的最后一个工作日
        List<OlympusCalendar__c> ssDay 
        = [Select Date__c From OlympusCalendar__c Where Date__c >= :firstDayOfMonth And Date__c <= :lastDayOfMonth And IsWorkDay__c = 1 order by Date__c desc limit 1];

        if (ssDay != null && ssDay.size() > 0) {
            if (String.valueOf(Date.today()).equals(String.valueOf(ssDay.get(0).Date__c))) {
                Database.executeBatch(new MonthEndAutoJudgeBatch(),20);
            }
        }
        // chenjingwu Batch整理合并 20240306 start
        UpdateAssetToCurrentMCBatch u = new UpdateAssetToCurrentMCBatch();
        Id execBTId = Database.executeBatch(u, 50);
        Id execBTId1 = Database.executeBatch(new UserUpdateBatch(),100);
        Date toDate = Date.today();
        Date mon1stDate = Date.newInstance(toDate.year(), toDate.month(), 1);//每月第一天
        // 由每月第一个工作日触发接口，变为每月第一天触发接口
        if (toDate == mon1stDate) {
            Id execBTId2 = database.executebatch(new Opportunity_Shipments_ForecastBatch(), 100);
        }
        // chenjingwu Batch整理合并 20240306 end
        
    }
    //CHAN-BUZC7V update by rentx 2020-11-05 end
}