liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
75
76
77
78
79
80
81
82
83
84
85
86
87
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
        // CurrentContract__r.RecordType_Name__c ---最近一期维修合同类型,Is_Add_888_Alarm__c --是否生成过888预警对象
        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,CurrentContract__r.RecordType_Name__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) {
                    //多年保  第一次提醒日-1天
                    Date startDate = asset.InstallDate.addMonths(6).addDays(-1);
                    //monthsBetween
                    Integer count = (asset.Extend_Gurantee_DateTo__c.year()-asset.InstallDate__c.year()) * 2;
                    while(alarmNumber<= count && startDate <= asset.Extend_Gurantee_DateTo__c){
                        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;
                            // 2023/08/24 888添加提醒类型字段 根据保有设备.多年保修类型 判断是哪种多年保修
                            if (asset.SerMarGuranteeType__c == '服务多年保修') {
                                alarm888.Alarm_Type__c = '服务多年保修';
                            }else if (asset.SerMarGuranteeType__c == '市场多年保修'){
                                alarm888.Alarm_Type__c = '市场多年保修';
                            }
 
                            alarm888List.add(alarm888);
                            alarmNumber++;
                        }
                        startDate = startDate.addMonths(6);
                    }
                }else if (asset.CurrentContract_Start_Date__c != null && asset.CurrentContract_End_Date__c != null){
                    //维修合同  第一次提醒日-1天
                    Date startDate = asset.InstallDate.addMonths(6).addDays(-1);
                    //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){
                        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;
                            // 2023/08/24 888添加提醒类型字段
                            alarm888.Alarm_Type__c = '服务合同';
 
                            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);
    }
}