高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
global class threeYearsRepair implements Database.Batchable<sObject>,Database.Stateful {
    public String query;
    public List < String > accountIdList;
    private BatchIF_Log__c iflog;
    public Date td = Date.today();
    public String OCSM_Period_half;
    public String OCSM_Period;
 
    global threeYearsRepair() {
        this.query = query;
        OCSM_Period = 'FY'+(td.year()+1);
    }
 
    global threeYearsRepair(List <String> accountIdList) {
        this.query = query;
        this.accountIdList = accountIdList;
        OCSM_Period = 'FY'+(td.year()+1);
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('执行start');
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Log__c  = 'threeYearsRepair start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        if (td.month() >= 4 && td.month() <= 9) {
            OCSM_Period_half = '1H';
        }else{
            OCSM_Period_half = '2H';
        }
        query='select Id,Hospital__c,Product2.ServiceCategory__c from Asset where Id not in (Select Asset__c from Maintenance_Contract_Asset__c) and Id in (select Delivered_Product__c from Repair__c)';//没有维修合同的数据
        if (accountIdList != null && accountIdList.size() > 0) {
            query += ' AND Hospital__c IN :accountIdList ';
        }  
        System.debug(LoggingLevel.INFO, '*** query: ' + query);
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Asset> resultList) {
        system.debug('*** 执行execute');
        System.debug(LoggingLevel.INFO, '*** 查询没有维修合同的保有设备Id resultList: ' + resultList);
 
        List<Id> mids = new List<Id>();
        List<Id> assetHos = new List<Id>();
        for (Asset ass: resultList) {
            mids.add(ass.Id);
            assetHos.add(ass.Hospital__c);
        }
        Map<Id,List<Asset>> aMapLists =  new Map<Id,List<Asset>>();
 
        Map<String,Account_Service_Of_Target__c> targetMap = new Map<String,Account_Service_Of_Target__c>();
        for(Account_Service_Of_Target__c asotList:[SELECT Id ,Account_HP__c,OCSM_Period_half__c,Three_Years_Repair_Cost_Soft_Mirror__c,Three_Years_Repair_Cost_Text__c,Three_Years_Repair_Cost_Surrounding_Area__c,Three_Years_Repair_Cost_Hard_Mirror__c 
                                                FROM Account_Service_Of_Target__c 
                                                WHERE OCSM_Period_half__c = :OCSM_Period_half 
                                                AND OCSM_Period__c = :OCSM_Period
                                                AND Account_HP__c IN:assetHos]){
            targetMap.put(asotList.Account_HP__c,asotList);
        }
 
 
        //存客户服务目标对象 医院Id 后续判断 有则给Id赋值。
        // Map<Id,Account_Service_Of_Target__c> hospMap = new Map<Id,Account_Service_Of_Target__c>();
        // for (Account_Service_Of_Target__c ast : asotList) {
        //     hospMap.put(ast.Account_HP__c,ast);
        // }
 
        //最后upsert客户服务目标对象 list目标
        List<Account_Service_Of_Target__c> asslist = new List<Account_Service_Of_Target__c>();
        //amaplist
        
        
        Date LastThirdYearDate = td.addYears(-3);
        System.debug(LoggingLevel.INFO, '*** LastThirdYearDate: ' + LastThirdYearDate);
        List<AggregateResult> ThreeyearList = [
            select
            sum(Discount_Price_formula__c) SumPrice,
            sum(Repair_Quotation_Id__r.sales_discount__c) sales_discount,
            sum(Repair_Quotation_Id__r.Contract_target__c) contract_target,
            sum(Repair_Quotation_Id__r.Loaner_repair__c) loaner_repair,
            sum(Repair_Quotation_Id__r.long_term_insurance__c) long_term_insurance,
            sum(Repair_Quotation_Id__r.Set_discount__c) set_discount,
            sum(Repair_Quotation_Id__r.Servince_contract_discount_amount__c) sercince,
            sum(Repair_Quotation_Id__r.long_term_insurance_MD__c) long_term_insuranceMD,
            sum(Repair_Quotation_Id__r.Delivery_compensation__c) delivery,
            sum(Repair_Quotation_Id__r.Other_discount__c) other,
            AVG(Delivered_Product__r.Last_Years_Repair_Month__c) threeYearM,
            Delivered_Product__c
            from
            Repair__c
            where
            Delivered_Product__c in:mids
            and Agreed_Date__c != null
            and Agreed_Date__c <= :td
            and Agreed_Date__c > :LastThirdYearDate
            group by Delivered_Product__c
        ];       
        Map<Id, Decimal> ThreeYearPriceSumMap = new Map<id, Decimal>();
        Map<Id, Decimal> ThiYearMonthMap = new Map<id, Decimal>();
 
        for (AggregateResult Rpc : ThreeyearList) {
            id idf        = String.valueOf(Rpc.get('Delivered_Product__c'));
            //Decimal Defir = decimal.valueOf(Rpc.get('SumPrice')+'');
            Decimal threeYearM = decimal.valueOf(Rpc.get('threeYearM') + '');
            Decimal Defir = sumPrice1(Rpc);
            ThreeYearPriceSumMap.put(idf, Defir);
            ThiYearMonthMap.put(idf, threeYearM);
        }        
        for (Asset assertItem : resultList) {
            if(ThreeYearPriceSumMap.containsKey(assertItem.Id)){
                assertItem.Three_Years_Repair_Cost_Text__c = ThreeYearPriceSumMap.get(assertItem.Id);           
                // bMapAsset.put(assertItem.Hospital__c,assertItem);
                List<Asset> assetLists = new List<Asset>();
                assetLists.add(assertItem);
                if(aMapLists.containsKey(assertItem.Hospital__c)){
                    List <Asset> alist = aMapLists.get(assertItem.Hospital__c);
                    alist.add(assertItem);
                    aMapLists.put(assertItem.Hospital__c,alist);
                }else{
                    aMapLists.put(assertItem.Hospital__c,assetLists);
                }         
            }
        }
        update resultList;
        for (Id mapId : aMapLists.keySet()) {
            Decimal sumThreePrice = 0;
            Decimal softE = 0;
            Decimal hardE = 0;
            Decimal periE = 0;
            for (Asset asset : aMapLists.get(mapId) ) {
                sumThreePrice += asset.Three_Years_Repair_Cost_Text__c;
                    //判断条件
                if(asset.Product2.ServiceCategory__c == '软性镜'){
                    softE += asset.Three_Years_Repair_Cost_Text__c;
                }else if(asset.Product2.ServiceCategory__c == '硬性镜'){
                    hardE += asset.Three_Years_Repair_Cost_Text__c;
                }else if(asset.Product2.ServiceCategory__c == '周边'){
                    periE += asset.Three_Years_Repair_Cost_Text__c;
                }
            }
            Account_Service_Of_Target__c astItem = new Account_Service_Of_Target__c();
            if(targetMap.containsKey(mapId)){
                astItem = targetMap.get(mapId);  
            }else{
                astItem.OCSM_Period__c = OCSM_Period; 
                astItem.OCSM_Period_half__c = OCSM_Period_half;
                astItem.Account_HP__c = mapId;
            }
            astItem.Three_Years_Repair_Cost_Soft_Mirror__c = softE;
            astItem.Three_Years_Repair_Cost_Hard_Mirror__c = hardE;
            astItem.Three_Years_Repair_Cost_Surrounding_Area__c = periE;
            astItem.Three_Years_Repair_Cost_Text__c = sumThreePrice;
            asslist.add(astItem);
 
        }
        upsert asslist;       
    }
 
    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += '\nthreeYearsRepair end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        update iflog;
 
    }
 
    private static Decimal sumPrice1(AggregateResult rpc) {
        Decimal SumPrice = Decimal.valueOf(rpc.get('SumPrice') + '');
        Decimal sales_discount = Decimal.valueOf((rpc.get('sales_discount') == null ? 0 : rpc.get('sales_discount')) + '') * -1;
        Decimal contract_target = Decimal.valueOf((rpc.get('contract_target') == null ? 0 : rpc.get('contract_target')) + '') * -1;
        Decimal loaner_repair = Decimal.valueOf((rpc.get('loaner_repair') == null ? 0 : rpc.get('loaner_repair')) + '') * -1;
        Decimal long_term_insurance = Decimal.valueOf((rpc.get('long_term_insurance') == null ? 0 : rpc.get('long_term_insurance')) + '') * -1;
        Decimal set_discount = Decimal.valueOf((rpc.get('set_discount') == null ? 0 : rpc.get('set_discount')) + '') * -1;
        Decimal sercince = Decimal.valueOf((rpc.get('sercince') == null ? 0 : rpc.get('sercince')) + '') * -1;
        Decimal long_term_insuranceMD = Decimal.valueOf((rpc.get('long_term_insuranceMD') == null ? 0 : rpc.get('long_term_insuranceMD')) + '') * -1;
        Decimal delivery = Decimal.valueOf((rpc.get('delivery') == null ? 0 : rpc.get('delivery')) + '') * -1;
        Decimal other = Decimal.valueOf((rpc.get('other') == null ? 0 : rpc.get('other')) + '') * -1;
        system.debug(rpc.get('SumPrice') + '--' + rpc.get('sales_discount') + '--' + rpc.get('contract_target') + '--' +
                     rpc.get('loaner_repair') + '--' + rpc.get('long_term_insurance') + '--' + rpc.get('set_discount') + '--' + rpc.get('long_term_insuranceMD')
                     + '--' + rpc.get('delivery') + '--' + rpc.get('other') + '--');
        return SumPrice + sales_discount + contract_target + loaner_repair + long_term_insurance + sercince + set_discount + long_term_insuranceMD + delivery + other;
    }
 
 
}