高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
global class MCUpdateEstimateCostBatch implements Database.Batchable<sObject> {
    // 设定运行的维修合同的ID
    public string TestID;
 
    // 设定运行的维修合同的list<ID>
    public list<string> TestIDList;
 
    // 报错的维修合同ID
    public String logstr = '';
 
    // 报错信息
    public String errorstr = '';
 
    global Integer totalCount = 0; // 总件数
    global Integer failedCount = 0; // 失败件数
 
    global List<String> emailMessages = new List<String>();
 
    global MCUpdateEstimateCostBatch() {
 
    }
 
    global MCUpdateEstimateCostBatch(string TestID ) {
        this.TestID = TestID;
    }
 
    global MCUpdateEstimateCostBatch(list<string> TestIDList ) {
        this.TestIDList = TestIDList;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        string query =
            ' select id ,  Contract_Amount__c , Estimation_Id__c, RecordType_DeveloperName__c, ' +
            ' Estimation_Id__r.Asset_Repair_Sum_Price__c, '+
            'Estimation_Id__r.NewEstimation_Amount__c ,Estimate_Trial_Money__c, '+ 
            'Estimation_Id__r.Asset_Sum_Price__c '+
            '  from Maintenance_Contract__c '
            // + ' where RecordType_DeveloperName__c != \'VM_Contract\' ';
            + ' where RecordType_DeveloperName__c = \'NewMaintenance_Contract\' ';
            // + ' where RecordType_DeveloperName__c = \'NewMaintenance_Contract\' ';
        if (string.isNotBlank(TestID) ) {
            query += ' and id =: TestID';
        } else if (TestIDList != null && TestIDList.size() > 0) {
            query += ' and id in: TestIDList';
        }
        system.debug('query:'+query);
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> MCList) {
        set<id> MCIDSet = new set<id>();
        Map<id,Maintenance_Contract__c> MCMap = new Map<id,Maintenance_Contract__c>();
        for (Maintenance_Contract__c tempMC : MCList) {
            mcIDSet.add(tempMC.id);
            MCMap.put(tempMC.id, tempMC);
        }
        list<Maintenance_Contract_Asset__c> UpdateMCAList = new list<Maintenance_Contract_Asset__c>();
        map<id,Maintenance_Contract_Asset_Estimate__c> UpdateMCAEMap = new map<id,Maintenance_Contract_Asset_Estimate__c>();
        list<Maintenance_Contract_Asset__c> MCAList = 
                    [select id, Estimate_List_Price_All__c, Estimate_Cost__c,
                        Maintenance_Contract_Asset_Estimate__c, 
                        Maintenance_Contract_Asset_Estimate__r.Estimate_Cost__c,
                        Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Maintenance_Price__c,
                        Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Asset_Repair_Sum_Price__c,
                        Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c,
                        Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.GuidePrice_Up__c,
                        Maintenance_Contract__c
                        from Maintenance_Contract_Asset__c 
                        where Maintenance_Contract__c in: mcIDSet];
        for (Maintenance_Contract_Asset__c tempMCA : MCAList) {
            decimal tempEC = 0;
            decimal realprice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Maintenance_Price__c - tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Asset_Repair_Sum_Price__c;
            decimal assetListPrice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c == null? 0 : tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c;
            decimal guidePriceUp = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.GuidePrice_Up__c;
            tempEC = ((realprice)*( assetListPrice / guidePriceUp)).setScale(2);
            if(tempMCA.Maintenance_Contract_Asset_Estimate__c != null
            &&!UpdateMCAEMap.containskey(tempMCA.Maintenance_Contract_Asset_Estimate__c)){
                // decimal realprice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Maintenance_Price__c - tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.Asset_Repair_Sum_Price__c;
                // decimal assetListPrice = tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c == null? 0 : tempMCA.Maintenance_Contract_Asset_Estimate__r.Adjustment_Upper_price__c;
                // decimal guidePriceUp = tempMCA.Maintenance_Contract_Asset_Estimate__r.Maintenance_Contract_Estimate__r.GuidePrice_Up__c;
                // tempEC = ((realprice)*( assetListPrice / guidePriceUp)).setScale(2);
                Maintenance_Contract_Asset_Estimate__c tempMCAE = new Maintenance_Contract_Asset_Estimate__c();
                tempMCAE.id = tempMCA.Maintenance_Contract_Asset_Estimate__c;
                tempMCAE.Estimate_Cost__c = tempEC;
                UpdateMCAEMap.put(tempMCAE.id,tempMCAE);
            }
            System.debug(' ++++ ' + realprice + ' ++++ ' + assetListPrice + ' ++++ ' + guidePriceUp);
            if(tempMCA.Estimate_Cost__c != tempEC){
                Maintenance_Contract_Asset__c newMCA = new Maintenance_Contract_Asset__c();
                newMCA.id = tempMCA.id;
                newMCA.Estimate_Cost__c = tempEC;
                UpdateMCAList.add(newMCA);
            }
        }
        if( UpdateMCAList.size() > 0 ){
            Database.SaveResult[] saveResults = Database.update(UpdateMCAList, false);
            totalCount = + saveResults.size();
            for (Integer i = 0; i < saveResults.size(); i++) {
                if (!saveResults.get(i).isSuccess() || Test.isRunningTest()) {
                    logstr += '维修合同保有设备:' + UpdateMCAList.get(i).Id + ' ,';
                    errorstr += saveResults.get(i).getErrors() + '\n';
                    failedCount ++;
                }
            }
        }
        if( UpdateMCAEMap.size() > 0 ){
            Database.SaveResult[] saveResults = Database.update(UpdateMCAEMap.values(), false);
            totalCount = + saveResults.size();
            for (Integer i = 0; i < saveResults.size(); i++) {
                if (!saveResults.get(i).isSuccess()) {
                    logstr += '维修合同报价保有设备:' + UpdateMCAEMap.values().get(i).Id + ' ,';
                    errorstr += saveResults.get(i).getErrors() + '\n';
                    failedCount ++;
                }
            }
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}