binxie
2024-01-16 1b08402678deb31bba4a347bfd388eba8360cbc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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) {
        // test  02i10000002dHUPAA2
        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 (MDM_Model_No__c = 'OER-AW' OR MDM_Model_No__c = 'OER-Smart') AND Is_Add_888_Alarm__c = false AND (InstallDate!= null AND 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 assetList;
 
    }
 
    global void execute(Database.BatchableContext BC,List<Asset> assetList) {
        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.InstallDate__c != null && asset.Extend_Gurantee_DateTo__c != null && asset.Extend_Gurantee_DateTo__c >= nowDate && asset.InstallDate__c <= nowDate) {
                    //多年保
                    Date startDate = asset.InstallDate.addMonths(6) ;
                    //monthsBetween
                    Integer count = (asset.Extend_Gurantee_DateTo__c.year()-asset.InstallDate__c.year()) * 2;
                    while(alarmNumber<= count && startDate< asset.Extend_Gurantee_DateTo__c.addMonths(9) ){
                        if (startDate >= nowDate) {
                            Alarm_888_Month__c alarm888 = new Alarm_888_Month__c();
                            alarm888.Number_Of_Operations__c = '第'+alarmNumber+'次作业';
                            alarm888.Nth_Operating_Day__c = startDate;
                            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 if (asset.CurrentContract_Start_Date__c != null && asset.CurrentContract_End_Date__c != null){
                    //维修合同
                    Date startDate = asset.InstallDate.addMonths(6) ;
                    //monthsBetween
                    Integer count = (asset.CurrentContract_End_Date__c.year()-asset.CurrentContract_Start_Date__c.year()) * 2;
                    //结束时间大于上一次作业时间3个月则生成
                    while(alarmNumber<= count && startDate < asset.CurrentContract_End_Date__c.addMonths(9)){
                        if (startDate >= nowDate) {
                            Alarm_888_Month__c alarm888 = new Alarm_888_Month__c();
                            alarm888.Number_Of_Operations__c = '第'+alarmNumber+'次作业';
                            alarm888.Nth_Operating_Day__c = startDate;
                            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);
                    }
                }
                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) {
        //发送邮件
        Id execEmailId = Database.executeBatch(new Email888AlarmBatch(),100);
    }
}