global class Create888AlarmBatch implements Database.Batchable<sObject> , Database.AllowsCallouts, Database.Stateful {
    public Date installDate = Date.newInstance(1900, 1, 1);
    public Date nowDate = Date.today();
    global Create888AlarmBatch() {

    }
    //Database.QueryLocator
    global List<Asset> start(Database.BatchableContext bc) {
        //Is_Add_888_Alarm__c = 'False' 首次执行去掉这个条件或者更新所有Asset的这个字段为False
        List<Asset> assetList = [Select Id,InstallDate,SerMarGuranteeType__c,InstallDate__c,Extend_Gurantee_DateTo__c,CurrentContract__c,CurrentContract_Start_Date__c,CurrentContract_End_Date__c,Is_Add_888_Alarm__c FROM Asset 
                          Where Is_Add_888_Alarm__c = 'False' AND (InstallDate!= null OR InstallDate!= :installDate)
                          AND ((CurrentContract_End_Date__c >=: nowDate  AND CurrentContract_Start_Date__c <= :nowDate) OR (Extend_Gurantee_DateTo__c >= :nowDate AND InstallDate__c <= :nowDate ))];
        /*return Database.getQueryLocator([Select Id,InstallDate,SerMarGuranteeType__c,InstallDate__c,Extend_Gurantee_DateTo__c,CurrentContract__c,CurrentContract_Start_Date__c,CurrentContract_End_Date__c,Is_Add_888_Alarm__c FROM Asset 
                          Where Is_Add_888_Alarm__c = 'False' AND (InstallDate!= null OR InstallDate!= :installDate)
                          AND ((CurrentContract_End_Date__c >=: nowDate  AND CurrentContract_Start_Date__c <= :nowDate) OR (Extend_Gurantee_DateTo__c >= :nowDate AND InstallDate__c <= :nowDate ))
                          AND Id = '02i100000036gbhAAA' LIMIT 1]);*/
        return assetList;

    }

    global void execute(Database.BatchableContext BC,List<Asset> assetList) {
        //assetList = [Select Id,InstallDate,SerMarGuranteeType__c,InstallDate__c,Extend_Gurantee_DateTo__c,CurrentContract__c,CurrentContract_Start_Date__c,CurrentContract_End_Date__c,Is_Add_888_Alarm__c FROM Asset Where Is_Add_888_Alarm__c = 'False' AND (InstallDate!= null OR InstallDate!= :installDate)];
        System.debug(LoggingLevel.INFO, '*** assetList: ' + assetList);
        if (assetList.size() > 0) {
            List<Alarm_888_Month__c>  alarm888List= new List<Alarm_888_Month__c>();
            for (Asset asset : assetList) {
                Integer alarmNumber = 1;
                if (asset.SerMarGuranteeType__c != null) {
                    //多年保
                    // Date startDate = nowDate<asset.InstallDate__c ? nowDate : asset.InstallDate__c;
                    Date startDate = nowDate;
                    //monthsBetween
                    Integer count = (asset.Extend_Gurantee_DateTo__c.year()-asset.InstallDate__c.year()) * 2;
                    while(startDate < asset.Extend_Gurantee_DateTo__c && alarmNumber<= count){
                            Alarm_888_Month__c alarm888 = new Alarm_888_Month__c();
                            String numberStr = '第'+alarmNumber+'次做业务';
                            alarm888.Number_Of_Operations__c = numberStr;
                            alarm888.Nth_Operating_Day__c = startDate.addMonths(6);
                            alarm888.Alarm_Date__c = alarm888.Nth_Operating_Day__c.addMonths(-2);
                            alarm888.Asset_ID__c = asset.Id;
                            alarm888List.add(alarm888);
                            alarmNumber++;
                            startDate = startDate.addMonths(6);
                            //最后一次预警  提前服务结束时间3个月为操作日
                            if (startDate.addMonths(-6) < asset.Extend_Gurantee_DateTo__c && startDate >= asset.Extend_Gurantee_DateTo__c) {
                                Alarm_888_Month__c alarm888Last = new Alarm_888_Month__c();
                                numberStr = '第'+alarmNumber+'次做业务';
                                alarm888Last.Number_Of_Operations__c = numberStr;
                                alarm888Last.Nth_Operating_Day__c = asset.Extend_Gurantee_DateTo__c.addMonths(-3);
                                alarm888Last.Alarm_Date__c = alarm888Last.Nth_Operating_Day__c.addMonths(-2);
                                alarm888Last.Asset_ID__c = asset.Id;
                                alarm888List.add(alarm888Last);
                            }
                    }
                   /* if (asset.SerMarGuranteeType__c == '市场多年保修') {
                        //23.8,24.2,24.8,25.2   24.9  end-start 时间
                        
                    }else if (asset.SerMarGuranteeType__c == '服务多年保修') {
                        while(startDate <= asset.Extend_Gurantee_DateTo__c.addDays(5) && alarmNumber<=8){
                            Alarm_888_Month__c alarm888 = new Alarm_888_Month__c();
                            String numberStr = '第'+alarmNumber+'次做业务';
                            alarm888.Number_Of_Operations__c = numberStr;
                            alarm888.Nth_Operating_Day__c = startDate.addMonths(6);
                            alarm888.Alarm_Date__c = alarm888.Nth_Operating_Day__c.addMonths(-2);
                            alarm888.Asset_ID__c = asset.Id;
                            alarm888List.add(alarm888);
                            alarmNumber++;
                            startDate = startDate.addMonths(6);
                        }
                    }*/

                }else{
                    //维修合同
                    //Date startDate = nowDate<asset.CurrentContract_Start_Date__c ? nowDate : asset.CurrentContract_Start_Date__c;
                    /*Date startDate = nowDate;
                    while(startDate <= asset.CurrentContract_End_Date__c.addDays(5) ){
                            Alarm_888_Month__c alarm888 = new Alarm_888_Month__c();
                            String numberStr = '第'+alarmNumber+'次做业务';
                            alarm888.Number_Of_Operations__c = numberStr;
                            alarm888.Nth_Operating_Day__c = startDate.addMonths(6);
                            alarm888.Alarm_Date__c = alarm888.Nth_Operating_Day__c.addMonths(-2);
                            alarm888.Asset_ID__c = asset.Id;
                            alarm888List.add(alarm888);
                            alarmNumber++;
                            startDate = nowDate.addMonths(6);
                        }*/

                }
                asset.Is_Add_888_Alarm__c = 'True';
            }
            StaticParameter.EscapeVMCTrigger = true;
            update assetList;
            StaticParameter.EscapeVMCTrigger = false;
            insert alarm888List;
            System.debug(LoggingLevel.INFO, '*** assetList: ' + assetList);
            System.debug(LoggingLevel.INFO, '*** alarm888List: ' + alarm888List);
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}