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
global class UpdateConrenewalpriceBatch implements Database.Batchable<sObject> {
// 设定运行的维修合同的list<ID>
public list<string> TestIDList;
 
global UpdateConrenewalpriceBatch() {
 
}
 
global UpdateConrenewalpriceBatch(list<string> TestIDList) {
    this.TestIDList = TestIDList;
}
 
global Database.QueryLocator start(Database.BatchableContext bc) {
    string query =
        ' select id , previousTotalRepairAmount__c , currentTotalRepairAmount__c ,'+
        'Contract_Consumption_rate__c,' +
        ' previousTotalContractAmount__c , currentTotalContractAmountVM__c , currentTotalGuaranteePrice__c '+
        ' , RecordType_DeveloperName__c  ' +
        '  from Maintenance_Contract__c '
        + ' where Status__c in (\'契約\',\'契約満了\') ';
    if (TestIDList != null && TestIDList.size() > 0) {
        query += ' and id in: TestIDList';
    }
    return Database.getQueryLocator(query);
}
 
global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> scope) {
    List<Data> datatemp = new List<Data>();
    datatemp = getChartData();
    for (Maintenance_Contract__c selectedLocal : scope) {
        // if (String.isBlank(String.valueOf(selectedLocal.Contract_Consumption_rate__c))) {
        //     continue;
        // }else {
        for (Data da : datatemp) {
            if (String.isBlank(String.valueOf(da.rate_Lower)) &&  selectedLocal.Contract_Consumption_rate__c < da.rate_Upper) {
                selectedLocal.Adjustment_ratio_Lower__c = da.price_Lower;
                selectedLocal.Adjustment_ratio_Upper__c = da.price_Upper;
                continue;
            } else if (selectedLocal.Contract_Consumption_rate__c >= da.rate_Lower && String.isBlank(String.valueOf(da.rate_Upper))) {
                selectedLocal.Adjustment_ratio_Lower__c = da.price_Lower;
                selectedLocal.Adjustment_ratio_Upper__c = da.price_Upper;
                continue;
            } else if (selectedLocal.Contract_Consumption_rate__c >= da.rate_Lower
                       && selectedLocal.Contract_Consumption_rate__c < da.rate_Upper) {
                selectedLocal.Adjustment_ratio_Lower__c = da.price_Lower;
                selectedLocal.Adjustment_ratio_Upper__c = da.price_Upper;
                continue;
            }
        }
        // }
    }
    update scope;
}
 
global void finish(Database.BatchableContext BC) {
 
}
 
public static List<Data> getChartData() {
    List<Data> data = new List<Data>();
    List<Achievement_linkage__c> achlist = [select id, Consumption_rate_Lower__c, Consumption_rate_Upper__c,
                                            PriceCount_Lower__c, PriceCount_Upper__c from Achievement_linkage__c];
    for (Achievement_linkage__c al : achlist ) {
        data.add(new Data(al.Consumption_rate_Lower__c, al.Consumption_rate_Upper__c, al.PriceCount_Lower__c, al.PriceCount_Upper__c));
    }
    return data;
}
 
// 价格体系 class
public class Data {
public Decimal rate_Upper { get; set; }
public Decimal rate_Lower { get; set; }
public Decimal price_Upper { get; set; }
public Decimal price_Lower { get; set; }
public Data(Decimal rate_Lower, Decimal rate_Upper, Decimal price_Lower, Decimal price_Upper) {
    this.rate_Upper = rate_Upper;
    this.rate_Lower = rate_Lower;
    this.price_Upper = price_Upper;
    this.price_Lower = price_Lower;
}
}
}