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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
global class MoreMaintenanceContractBatch implements Database.Batchable<sObject> {
    public String query;
    public String Id;
    global MoreMaintenanceContractBatch() {
        this.query = query;
    }
    global MoreMaintenanceContractBatch(String  Id) {
        Integer i=1;
        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++;
        this.Id = Id;
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select id,Estimate_Target__c,RecordType_DeveloperName__c,Contract_Conclusion_Date__c from Maintenance_Contract__c where Estimate_Target__c=\'经销商\' and (RecordType_DeveloperName__c = \'NewMaintenance_Contract\' or RecordType_DeveloperName__c =\'VM_Contract\') ';
        if(String.isNotBlank(this.Id)){
            query += '   AND Id = \''+this.Id+'\'';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<Maintenance_Contract__c> scope) {
        List<String> MCMC = new  List<String>();
        List<String> MCVM = new  List<String>();
        List<Repair__c> repairListMC = new List<Repair__c>();
        List<Repair__c> repairListVM = new List<Repair__c>();
        for(Maintenance_Contract__c mc:scope){
            if(mc.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')){
                MCMC.add(mc.Id);
            }
            if(mc.RecordType_DeveloperName__c.equals('VM_Contract')){
                MCVM.add(mc.Id);
            } 
        }
        if(MCMC.size()>0){
            repairListMC = [Select Id,Name,Maintenance_Contract__c,hospitalQuotationText__c from Repair__c where  Maintenance_Contract__c In:MCMC and Status1__c !='0.取消' and Status1__c != '0.删除' order by Maintenance_Contract__c];
            Map<String,Decimal> mapMC = new Map<String,Decimal>();
            Decimal hospitalQuotationText = 0.00;
            Decimal hospitalQuotationTextSum = 0.00;
            for(Repair__c obj : repairListMC){
                String MCId = (String)obj.Maintenance_Contract__c;
                if(mapMC.containsKey(MCId)){
                    hospitalQuotationText = mapMC.get(MCId);
                    if(obj.hospitalQuotationText__c !=null){
                        hospitalQuotationTextSum = hospitalQuotationText + obj.hospitalQuotationText__c;
                    }else{
                        hospitalQuotationTextSum = hospitalQuotationText;
                    }
                    mapMC.put(MCId, hospitalQuotationTextSum);
                }else{
                    mapMC.put(MCId, obj.hospitalQuotationText__c);
                }
            }
            List<Maintenance_Contract__c> upMC = new List<Maintenance_Contract__c>(); 
            for(String key : mapMC.keySet()){
                Maintenance_Contract__c updmc = new Maintenance_Contract__c();
                if(mapMC.get(key)>0.00){
                    updmc.Id = key;
                    updmc.hospitalQuotationTextSum__c = mapMC.get(key);
                    upMC.add(updmc); 
                }
                
            }
            update upMC;
        }
        if(MCVM.size()>0){
            repairListVM = [Select Id, Name,VM_Maintenance_Contract__c,hospitalQuotationText__c from Repair__c where  VM_Maintenance_Contract__c =:MCVM and Status1__c !='0.取消' and Status1__c != '0.删除' order by VM_Maintenance_Contract__c];
            Map<String, Decimal> mapVM = new Map<String, Decimal>();
            Decimal hospitalQuotationTextVM = 0.00;
            Decimal hospitalQuotationTextSumVM = 0.00;
            for(Repair__c obj : repairListVM){
                String VMId = (String)obj.VM_Maintenance_Contract__c;
                if(mapVM.containsKey(VMId)){
                    hospitalQuotationTextVM = mapVM.get(VMId);
                    if(obj.hospitalQuotationText__c !=null){
                        hospitalQuotationTextSumVM = hospitalQuotationTextVM + obj.hospitalQuotationText__c;
                    }else{
                        hospitalQuotationTextSumVM = hospitalQuotationTextVM;
                    }
                    mapVM.put(VMId, hospitalQuotationTextSumVM);
                }else{
                    mapVM.put(VMId, obj.hospitalQuotationText__c);
                }
            } 
            System.debug('test111:'+mapVM);
            List<Maintenance_Contract__c> upVM = new List<Maintenance_Contract__c>(); 
            for(String key : mapVM.keySet()){
                Maintenance_Contract__c updvm = new Maintenance_Contract__c();
                if(mapVM.get(key)>0.00){
                    updvm.Id = key;
                    updvm.hospitalQuotationTextSum__c = mapVM.get(key);
                    upVM.add(updvm);
                }
            }
            update upVM;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}