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
88
89
90
91
92
93
94
95
96
97
global class NextMContractHistoricalBatch implements Database.Batchable<sObject> {
    public String query;
 
    global NextMContractHistoricalBatch() {
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select Id,Maintenance_Contract__c,Maintenance_Contract__r.id,Maintenance_Contract__r.RecordType_Name__c,Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,Maintenance_Contract__r.Maintenance_Contract_No_F__c,Maintenance_Contract__r.CreatedDate'
                        +' from Maintenance_Contract_Asset__c  where (Maintenance_Contract__r.Status__c = \'契約\' or Maintenance_Contract__r.Status__c = \'契約満了\') and Maintenance_Contract__r.RecordType_Name__c = \'多年保修合同\' order by Asset__c,Maintenance_Contract__r.CreatedDate desc';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Asset__c> mcaList) {
        Map<String,Maintenance_Contract_Asset__c> mcMap =  new Map<String,Maintenance_Contract_Asset__c>();
        for(Maintenance_Contract_Asset__c mca:mcaList){
            if (mca.Asset__r.First_Service_number__c!=null) {
                if (!mcMap.containsKey(mca.Asset__r.id)) {
                mcMap.put(mca.Asset__r.id,mca);
            }
                if(mcMap.containsKey(mca.Asset__r.id)&&mca.Maintenance_Contract__r.CreatedDate>mcMap.get(mca.Asset__r.id).Maintenance_Contract__r.CreatedDate){
                    mcMap.remove(mca.Asset__r.id);
                    mcMap.put(mca.Asset__r.id,mca);
                }
            }
        }
        List<String> mcids = new  List<String>();
        for ( Maintenance_Contract_Asset__c mca:mcMap.values()) {
            if (!mcids.contains(mca.Maintenance_Contract__r.id)) {
                mcids.add(mca.Maintenance_Contract__r.id);
            }
        }
        List<Maintenance_Contract_Asset__c> mcas =  [select Id,Maintenance_Contract__r.RecordType_Name__c,Next_Maintenance_Contract__c,Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,Maintenance_Contract__r.CreatedDate 
                                                        from Maintenance_Contract_Asset__c 
                                                        where Maintenance_Contract__c in :mcids];
                                                        
        List<Maintenance_Contract_Asset__c>   updateList = new List<Maintenance_Contract_Asset__c>();
        List<String> mcsql = new List<String>();
        for (Maintenance_Contract_Asset__c mca:mcas) {
            mcsql.add(mca.Asset__r.First_Service_number__c);
        }   
        List<Maintenance_Contract__c> mcs = [select Id,name,Maintenance_Contract_No__c from Maintenance_Contract__c where Maintenance_Contract_No__c in :mcsql ];
        Map<String,Maintenance_Contract__c> mcMap2 = new  Map<String,Maintenance_Contract__c>();
            for (Maintenance_Contract__c mc :mcs) {
                    if (!mcMap2.containsKey(mc.Maintenance_Contract_No__c)) {
                        mcMap2.put(mc.Maintenance_Contract_No__c,mc);
                    }
            }      
        for (Maintenance_Contract_Asset__c mca:mcas) {
            Maintenance_Contract_Asset__c mcaNew = new Maintenance_Contract_Asset__c();
            mcaNew.id = mca.id;
            mcaNew.Next_Maintenance_Contract__c = mcMap2.get(mca.Asset__r.First_Service_number__c).id;
            updateList.add(mcaNew);
        }
        if (updateList.size()>0) {
            update updateList;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        integer i=0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}