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
global class HistoryTotalAmountBatch implements Database.Batchable<sObject> {
    public String query;
    public List<String> idl;
    global HistoryTotalAmountBatch() {
        this.query = query;
    }
 
    global HistoryTotalAmountBatch(List<String> idl) {
        this.idl = idl;
        this.query = query;
    } 
    global Database.QueryLocator start(Database.BatchableContext bc) {
          query = 'SELECT Id,Total_Contract_Price__c From Maintenance_Contract_Estimate__c Where  Maintenance_Contract_Status__c = \'契約\' and Dealer__r.Is_Active_Formula__c !=\'无效\' and Process_Status__c!=\'不批准\' and Process_Status__c!=\'中止\' ';
          if (idl!=null) {
              query+=' and id In :idl';
          }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Estimate__c> mceList) {
        List<String> mceIds = new List<String>();
        Map<String,Maintenance_Contract_Estimate__c> map1 = new Map<String,Maintenance_Contract_Estimate__c>();
        Map<String,Double> map2 = new Map<String,Double>();
        for (Maintenance_Contract_Estimate__c mce :mceList) {
            mceIds.add(mce.id);
        }
        List<Maintenance_Contract_Asset_Estimate__c> mcaeList =  [SELECT id,Product_Manual__c,Product_Manual__r.Maintenance_Price_Year__c,Asset__r.Product2.Maintenance_Price_Year__c,Asset__c,Maintenance_Contract_Estimate__r.id,Maintenance_Contract_Estimate__r.Contract_Range__c FROM Maintenance_Contract_Asset_Estimate__c WHERE Maintenance_Contract_Estimate__c IN :mceIds];
        for (String mceid:mceIds) {
            Maintenance_Contract_Estimate__c mce = new Maintenance_Contract_Estimate__c();
            for (Maintenance_Contract_Asset_Estimate__c mcae: mcaeList) {
                Decimal price = 0;
                if (mcae.Asset__c != null) {
                    system.debug('mcae.Asset__c=='+mcae.Asset__r.Product2.Maintenance_Price_Year__c);
                    price = mcae.Asset__r.Product2.Maintenance_Price_Year__c;
                }
                if (mcae.Product_Manual__c != null) {
                    system.debug('mcae.Product_Manual__c =='+mcae.Product_Manual__r.Maintenance_Price_Year__c);
                    price =  mcae.Product_Manual__r.Maintenance_Price_Year__c;
                }
                if (price != 0 && price!=null) {
                    if (mceid == mcae.Maintenance_Contract_Estimate__r.id ) {
                       
                            if (map1.containsKey(mceid)) {
                            Maintenance_Contract_Estimate__c mce1 = map1.get(mceid);
                            
                            if (mce1 !=null) {  
                                mce1.Total_Contract_Price__c += price;
                                map1.remove(mceid);
                                map1.put(mceid,mce1);
                            }
                            }else{
                                mce.id = mceid;
                                mce.Total_Contract_Price__c = price;
                                map1.put(mceid,mce);
                            }
                        if (mcae.Maintenance_Contract_Estimate__r.Contract_Range__c!=null) {
                            if (!map2.containsKey(mceid)) {
                                 map2.put(mceid,mcae.Maintenance_Contract_Estimate__r.Contract_Range__c);
                            }
                        }
                    }
                }
            }
        }
        for( String mceid :map1.keySet()){
           Maintenance_Contract_Estimate__c mce =  map1.get(mceid);
           if (map2.get(mceid)!=null) {
               Double month = map2.get(mceid);
               Decimal totalPrice = mce.Total_Contract_Price__c;
               totalPrice = totalPrice/12*month;
               mce.Total_Contract_Price__c = totalPrice;
               map1.remove(mceid);
               map1.put(mceid,mce);
           }
        }
        if (map1.keySet()!=null) {
            update map1.values();
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}