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
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
public without sharing class MCESTrigger {
    public static void getLastyearQuotationDayRepairPrice(List<Maintenance_Contract_Estimate__c> newList, Map<Id, Maintenance_Contract_Estimate__c> newMap, List<Maintenance_Contract_Estimate__c> oldList, Map<Id, Maintenance_Contract_Estimate__c> oldMap) {
        if (Trigger.isAfter) {
            List<id> idList = new List<id>();
            Map<id, Date> mceMap = new Map<id, Date>();
            //add    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
            Map<id,Maintenance_Contract_Estimate__c> mapMCE = new Map<id,Maintenance_Contract_Estimate__c>();
            //add    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
            for (Maintenance_Contract_Estimate__c mce : newList) {
                if (mce.Submit_quotation_day__c != null) {
                    idList.add(mce.Id);
                    mceMap.put(mce.Id, mce.Submit_quotation_day__c);
                }else{
                    //add    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
                    //1:如果 合同月数 的值是否有变动,并且报价状态为 批准,那么就更新一下 维修合同报价/保有设备 的 合同价格(公式拷文本)字段的值
                    //2:如果 报价状态 变为批准状态,那么也更新一下维修合同报价/保有设备 的 合同价格(公式拷文本)字段的值
                    if(Trigger.isUpdate){
                        if(oldMap.get(mce.id).Contract_Range__c != mce.Contract_Range__c){
                            idList.add(mce.Id);
                        }
                    }
                    //add    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
                }
            }
 
            List<Maintenance_Contract_Asset_Estimate__c> mcaeList = new List<Maintenance_Contract_Asset_Estimate__c>();
            mcaeList = [select id, asset__c, Maintenance_Contract_Estimate__c, Bef_QuotationDay_1Year_Price__c,Maintenance_Contract_Estimate_Status__c from Maintenance_Contract_Asset_Estimate__c where Maintenance_Contract_Estimate__c in : idList];
 
            List<id> mcaeIdList = new List<id>();
            Map<id, Date> mcaeMap = new Map<id, Date>();
            for (Maintenance_Contract_Asset_Estimate__c mcae : mcaeList) {
                //update    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
                if (newMap.get(mcae.Maintenance_Contract_Estimate__c).Submit_quotation_day__c != null) {
                    mcaeIdList.add(mcae.asset__c);
                    mcaeMap.put(mcae.asset__c, mceMap.get(mcae.Maintenance_Contract_Estimate__c));
                }
                //update    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
            }
            if (mcaeIdList.size() > 0) {
                List<Repair__c> rpList = [
                                        select 
                                            Repair_List_Price_formula__c,
                                            Delivered_Product__c,
                                            Repair_Final_Inspection_Date__c
                                        from 
                                            Repair__c
                                        where 
                                            Delivered_Product__c in:mcaeIdList
                                        and (
                                                    (
                                                        Repair_Quotation_Id__c!=null 
                                                            and     (Repair_Quotation_Id__r.New_QIS_free__c = 0 
                                                                    or 
                                                                        Repair_Quotation_Id__r.New_QIS_free__c = null)
                                                            and     (Repair_Quotation_Id__r.Contract_free__c= 0
                                                                    or
                                                                        Repair_Quotation_Id__r.Contract_free__c= null)
                                                    )
                                                or  Repair_Quotation_Id__c =null
                                            )
                                        and Status2__c != '00.关闭' 
                                        and Status2__c != '00.取消'
                                        and Status2__c != '00.删除'
                                        and (
                                                (QIS_ID__c != null
                                                    and QIS_ID__r.next_action__c != '无偿维修'
                                                    and QIS_ID__r.next_action__c != '无偿更换'
                                                )
                                            or QIS_ID__c = null
                                        )
                                        and Return_Without_Repair_IF__c = false];
                // 以保有设备为单位做报价申请日范围内金额合计
                Map<id, Decimal> asPriceMap = new Map<id,Decimal>(); 
                for (Repair__c r : rpList) {
                    Date nowDt = mcaeMap.get(r.Delivered_Product__c);
                    Date lastYearDate = nowDt.addYears(-1);
                    lastYearDate = lastYearDate.addDays(1);
                    if (r.Repair_Final_Inspection_Date__c > lastYearDate && r.Repair_Final_Inspection_Date__c <= nowDt) {
                        Decimal Defir = r.Repair_List_Price_formula__c == null ? 0 : r.Repair_List_Price_formula__c;
                        if (asPriceMap.get(r.Delivered_Product__c) == null) {
                            asPriceMap.put(r.Delivered_Product__c, Defir);
                        } else {
                            asPriceMap.put(r.Delivered_Product__c, Defir + asPriceMap.get(r.Delivered_Product__c));
                        }
                    }
                }
                // 更新List作成
                List<Maintenance_Contract_Asset_Estimate__c> updList = new List<Maintenance_Contract_Asset_Estimate__c>();
                for (Maintenance_Contract_Asset_Estimate__c updMcae : mcaeList) {
                    //update    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
                    boolean flagTrue = false;//判断是否需要空更新一下
                    if (asPriceMap.get(updMcae.asset__c) != null) {
                        if (updMcae.Bef_QuotationDay_1Year_Price__c != asPriceMap.get(updMcae.asset__c)) {
                            updMcae.Bef_QuotationDay_1Year_Price__c = asPriceMap.get(updMcae.asset__c);
                            updList.add(updMcae);
                        }else{
                            flagTrue = true;
                        }
                    }else{
                        flagTrue = true;
                    }
                    if(flagTrue){
                        if(Trigger.isUpdate){
                            //如果 合同月数 的值有变化,并且 状态等于 批准,那么对象维修合同报价/保有设备空更新一下
                            if(newMap.get(updMcae.Maintenance_Contract_Estimate__c).Contract_Range__c 
                                != oldMap.get(updMcae.Maintenance_Contract_Estimate__c).Contract_Range__c
                            ){
                                Maintenance_Contract_Asset_Estimate__c mcaeNull = new Maintenance_Contract_Asset_Estimate__c();
                                mcaeNull.id = updMcae.id;
                                updList.add(mcaeNull);
                            }
                        }
                    }
                    //update    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
                }
                if (updList.size() > 0) {
                    update updList;
                }
            }
        }
 
        Integer i =0;
        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++;
        i++;
        i++;
        i++;
 
    
    }
 
    // HWAG-B3G3FE 2018/08/13 报价提交对象修改批准后更新
    public static void setEstimateTarget(List<Maintenance_Contract_Estimate__c> newList, Map<Id, Maintenance_Contract_Estimate__c> newMap, List<Maintenance_Contract_Estimate__c> oldList, Map<Id, Maintenance_Contract_Estimate__c> oldMap) {
        for (Maintenance_Contract_Estimate__c mce : newList) {
            Maintenance_Contract_Estimate__c oldMce = oldMap.get(mce.Id);
            if (mce.Changed_Target_Approval__c != oldMce.Changed_Target_Approval__c && mce.Changed_Target_Approval__c) {
                mce.Estimate_Target__c = mce.Changed_Estimate_Target__c;
                mce.Dealer__c = mce.Changed_Dealer__c;
            }
            system.debug('setEstimateTarget>>>' + Json.serialize(mce));//add by tiger
        }
    }
}