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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
global class SetPersonalTargetBatch implements Database.Batchable<sObject> {
    String query;
    // 目标的数据类型 
    private RecordType rt;
    //  现在年度
    private Integer currentYear;
    // 比重
    private Map<String, List<Double>> proportion;
    // 既存目标数据
    private Map<String, Opportunity> oppMap;
    private Map<Id, OpportunityLineItem> oliMap;
    private static String[] amountCategory = new String[] {'GI','ET','BF','GS','URO','GYN','ENT','ENG1','ENG2'};
 
    //  当前年度
    private Integer iYear;
    //  当前月份
    private Integer iMonth;
    //  当前日期
    private Integer iDay;
    //  4月可编辑期限
    private Integer iBuffer;
 
    // 当前期
    public String currentPeriod { get; private set; }
    global SetPersonalTargetBatch() {
    }
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'select key__c,Amount__c,user_Alias__c,SAP_Province__c,TargetType__c,remarks__c,Is_Processing__c,iYear__c,Use_Start_Date__c from Amount_Major_Product__c where Is_Processing__c = true';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Amount_Major_Product__c> Amount_Major_Products) {
        init(Amount_Major_Products);
        List<Opportunity> saveList = new List<Opportunity>();
        List<Opportunity> deleteList = new List<Opportunity>();
 
        for (Amount_Major_Product__c Amount_Major_Product : Amount_Major_Products){
            string key = Amount_Major_Product.key__c;
            list<string> user_Id_target_category = key.split('_');
            if(user_Id_target_category.size() < 2){
                return;
            }
            Amount_Major_Product.Is_Processing__c = false;
            string user_Id = user_Id_target_category[0];
            string target_category = user_Id_target_category[1];
            //使用开始后目标金额补正系数计算
            //Decimal proportionSum = 0.0;
            Map<String, Decimal> proportionSumMap = new Map<String, Decimal>();
            for (Integer j = 0; j < amountCategory.size(); j++) {
                String amountC = amountCategory[j];
                proportionSumMap.put(amountC, 0.0);
            }
            for (Integer i = 0; i < 12; i++) {
                Integer y = Integer.valueOf(Amount_Major_Product.iYear__c);
                Integer m = 4 + i;
                if (m > 12) {
                    y += 1;
                    m -= 12;
                }
                String syear = String.valueOf(y);
                String smonth = String.valueOf(m);
                if (m < 10) {
                    smonth = '0' + smonth;
                }
                String sTagetDay = syear + '-' + smonth + '-01';
                Date tagetDay = Date.valueOf(sTagetDay);
                if (Amount_Major_Product.Use_Start_Date__c < tagetDay) {
                    //proportionSum += proportion.get(amountC)[i];
                    for (Integer j = 0; j < amountCategory.size(); j++) {
                        String amountC = amountCategory[j];
                        proportionSumMap.put(amountC, proportionSumMap.get(amountC) + proportion.get(amountC)[i]);
                    }
                }
            }
            //proportionSum = proportionSum / 100;
            for (Integer j = 0; j < amountCategory.size(); j++) {
                String amountC = amountCategory[j];
                proportionSumMap.put(amountC, proportionSumMap.get(amountC) / 100);
            }
            //--------------
            for (Integer i = 0; i < 12; i++) {
                Integer y = Integer.valueOf(Amount_Major_Product.iYear__c);
                Integer m = 4 + i;
                if (m > 12) {
                    y += 1;
                    m -= 12;
                }
                String syear = String.valueOf(y);
                String smonth = String.valueOf(m);
                if (m < 10) {
                    smonth = '0' + smonth;
                }
                String sTagetDay = syear + '-' + smonth + '-01';
                Date tagetDay = Date.valueOf(sTagetDay);
                if (Amount_Major_Product.Use_Start_Date__c >= tagetDay) {
                    // 使用開始当月 及び 開始前 は目標を登録しない,无视。
                    continue;
                }
                String temp_key = user_Id + '_' + target_category+'_' +sTagetDay;
                // 每月数据赋值
                Opportunity opp = new Opportunity();
                System.debug('temp_key===' + temp_key+';==='+oppMap.keySet());
                System.debug('===真/假===' + oppMap.containskey(temp_key));
                if (oppMap.containskey(temp_key)) {
                    opp = oppMap.get(temp_key);
                    if (Amount_Major_Product.Amount__c == null || Amount_Major_Product.Amount__c == 0 ) {
                        deleteList.add(opp);
                        continue;
                    }
                    
                    opp.Name = Amount_Major_Product.user_Alias__c + ' 担当目标';
                    opp.StageName = '目標';
                    opp.OwnerId = user_Id;
                    // トリガをスルーのため、ここでやります
                    opp.Owner_System__c = user_Id;
                    opp.Opportunity_Category__c = target_category;
                    opp.Proportion__c = proportion.get(target_category)[i];
                    opp.CloseDate = tagetDay;
                    opp.Amount = Amount_Major_Product.Amount__c / proportionSumMap.get(target_category);
                    opp.Target_category__c = '担当目标';
                    opp.SAP_Province__c = Amount_Major_Product.SAP_Province__c;
                    opp.TargetType__c = Amount_Major_Product.TargetType__c;
                    opp.remarks__c = Amount_Major_Product.remarks__c; // 20230510 ljh 
                    opp.RecordTypeId = rt.Id;
                    opp.OCM_Target_period__c = String.valueOf(integer.valueOf(Amount_Major_Product.iYear__c) - 1867 + 'P');
                    opp.CurrencyIsoCode = 'CNY';
                    saveList.add(opp);
                } else {
                    if (Amount_Major_Product.Amount__c == null || Amount_Major_Product.Amount__c == 0) {
                        continue;
                    }
                    opp.Name = Amount_Major_Product.user_Alias__c + ' 担当目标';
                    opp.StageName = '目標';
                    opp.OwnerId = user_Id;
                    // トリガをスルーのため、ここでやります
                    opp.Owner_System__c = user_Id;
                    opp.Opportunity_Category__c = target_category;
                    opp.Proportion__c = proportion.get(target_category)[i];
                    opp.CloseDate = tagetDay;
                    opp.Amount = Amount_Major_Product.Amount__c / proportionSumMap.get(target_category);
                    opp.Target_category__c = '担当目标';
                    opp.SAP_Province__c = Amount_Major_Product.SAP_Province__c;
                    opp.TargetType__c = Amount_Major_Product.TargetType__c;
                    opp.remarks__c = Amount_Major_Product.remarks__c;// 20230510 ljh 
                    opp.RecordTypeId = rt.Id;
                    opp.OCM_Target_period__c = String.valueOf(integer.valueOf(Amount_Major_Product.iYear__c) - 1867 + 'P');
                    opp.CurrencyIsoCode = 'CNY';
                    // 加入保存列表
                    saveList.add(opp);
                }
                
            }
        }
        System.debug('deleteList+++++++++++' + deleteList.size());
        // トリガをスルー
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
 
        if (deleteList.size() > 0) {
            delete deleteList;
        }
 
        if (saveList.size() > 0) {
            System.debug('saveList+++++++++++' + saveList[0]);
            upsert saveList;
        }
        
        
        upsert Amount_Major_Products;
 
    }
 
    global void finish(Database.BatchableContext BC) {
        system.debug('SetPersonalTargetBatch结束.');
    }
 
    private void init(List<Amount_Major_Product__c> Amount_Major_Products){
        // 每月比重
        if (proportion == null) {
            proportion = new Map<String, List<Double>>();
            String strObjectiveProportionGI = System.Label.ObjectiveProportionGI;
            List<String> objectiveProportionGI = strObjectiveProportionGI.split(',');
            List<Double> doubleGI = new List<Double>();
            for (String strGI : objectiveProportionGI) {
                doubleGI.add(Double.valueOf(strGI));
            }
            proportion.put('GI', doubleGI);
 
            String strObjectiveProportionET = System.Label.ObjectiveProportionET;
            List<String> objectiveProportionET = strObjectiveProportionET.split(',');
            List<Double> doubleET = new List<Double>();
            for (String strET : objectiveProportionET) {
                doubleET.add(Double.valueOf(strET));
            }
            proportion.put('ET', doubleET);
 
            String strObjectiveProportionBF = System.Label.ObjectiveProportionBF;
            List<String> objectiveProportionBF = strObjectiveProportionBF.split(',');
            List<Double> doubleBF = new List<Double>();
            for (String strBF : objectiveProportionBF) {
                doubleBF.add(Double.valueOf(strBF));
            }
            proportion.put('BF', doubleBF);
 
            String strObjectiveProportionGS = System.Label.ObjectiveProportionGS;
            List<String> objectiveProportionGS = strObjectiveProportionGS.split(',');
            List<Double> doubleGS = new List<Double>();
            for (String strGS : objectiveProportionGS) {
                doubleGS.add(Double.valueOf(strGS));
            }
            proportion.put('GS', doubleGS);
 
            String strObjectiveProportionURO = System.Label.ObjectiveProportionURO;
            List<String> objectiveProportionURO = strObjectiveProportionURO.split(',');
            List<Double> doubleURO = new List<Double>();
            for (String strURO : objectiveProportionURO) {
                doubleURO.add(Double.valueOf(strURO));
            }
            proportion.put('URO', doubleURO);
 
            String strObjectiveProportionGYN = System.Label.ObjectiveProportionGYN;
            List<String> objectiveProportionGYN = strObjectiveProportionGYN.split(',');
            List<Double> doubleGYN = new List<Double>();
            for (String strGYN : objectiveProportionGYN) {
                doubleGYN.add(Double.valueOf(strGYN));
            }
            proportion.put('GYN', doubleGYN);
 
            String strObjectiveProportionENT = System.Label.ObjectiveProportionENT;
            List<String> objectiveProportionENT = strObjectiveProportionENT.split(',');
            List<Double> doubleENT = new List<Double>();
            for (String strENT : objectiveProportionENT) {
                doubleENT.add(Double.valueOf(strENT));
            }
            proportion.put('ENT', doubleENT);
 
            String strObjectiveProportionOTH = System.Label.ObjectiveProportionOTH;
            List<String> objectiveProportionOTH = strObjectiveProportionOTH.split(',');
            List<Double> doubleOTH = new List<Double>();
            for (String strOTH : objectiveProportionOTH) {
                doubleOTH.add(Double.valueOf(strOTH));
            }
            proportion.put('OTH', doubleOTH);
            // DB202303443108 you start ENG拆分为ENG1和ENG2
            String strObjectiveProportionENG1 = System.Label.ObjectiveProportionENG;
            List<String> objectiveProportionENG1 = strObjectiveProportionENG1.split(',');
            List<Double> doubleENG1 = new List<Double>();
            for (String strENG1 : objectiveProportionENG1) {
                doubleENG1.add(Double.valueOf(strENG1));
            }
            proportion.put('ENG1', doubleENG1);
 
            String strObjectiveProportionENG2 = System.Label.ObjectiveProportionENG;
            List<String> objectiveProportionENG2 = strObjectiveProportionENG2.split(',');
            List<Double> doubleENG2 = new List<Double>();
            for (String strENG2 : objectiveProportionENG2) {
                doubleENG2.add(Double.valueOf(strENG2));
            }
            proportion.put('ENG2', doubleENG2);
            // DB202303443108 you end ENG拆分为ENG1和ENG2
        }
        //初始化既存目标数据
        oppMap = new Map<String, Opportunity>();
        // 目标的数据类型
        if (rt == null) {
            rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target'];
        }
        // 现在时间
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        if (month < 4) {
            year -= 1;
        }
        // 初始化
        currentYear = year;
        iYear = year;
        iMonth = month;
        iDay = dateNow.day();
        iBuffer = Integer.valueOf(System.Label.SetPersonalTarget_buffer_day);
        
        //WLIG-BZVDB4 20210414  ljh update start
        //currentPeriod = String.valueOf(iYear - 1867 + 'P');
        list<string> currentPeriods = new list<string>();
        currentPeriod = String.valueOf(iYear - 1867 + 'P');
        currentPeriods.add(currentPeriod);
        currentPeriod = String.valueOf(iYear + 1 - 1867 + 'P');
        currentPeriods.add(currentPeriod);
        currentPeriod = String.valueOf(iYear - 1 - 1867 + 'P');
        currentPeriods.add(currentPeriod);
        //WLIG-BZVDB4 20210414  ljh update end
        //--------------
        // 获取 存储用户 用于搜索所对应询价
        list<ID> OwnerId = new list<ID>();
        for (Amount_Major_Product__c Amount_Major_Product : Amount_Major_Products) {
            string key = Amount_Major_Product.key__c;
            list<string> user_Id_target_category = key.split('_');
            if (user_Id_target_category.size() < 2) {
                //Database.rollback(sp);
                return;
            }
            Amount_Major_Product.Is_Processing__c = false;
            string user_Id = user_Id_target_category[0];
            OwnerId.add(user_Id);
        }      
        //获取既存Opportunity
        Opportunity[] opportunitys = [select
                        Id, OwnerId, Opportunity_Category__c, Proportion__c, CloseDate,
                        Amount, Objective__c, Target_category__c,
                        SAP_Province__c,TargetType__c, RecordTypeId, OCM_Target_period__c
                 from Opportunity
                where Target_category__c = '担当目标'
                  and RecordTypeId = :rt.Id
                  and OwnerId in :OwnerId
                  //WLIG-BZVDB4 20210414  ljh update start
                  //and OCM_Target_period__c = :currentPeriod];
                  and OCM_Target_period__c in :currentPeriods];
                  //WLIG-BZVDB4 20210414  ljh update end
        system.debug('currentPeriod:'+currentPeriod);
        if(opportunitys.size() <= 0)
            return;
 
        for (Opportunity opp : opportunitys) {
            if (opp.OwnerId != null && opp.CloseDate !=null){
                //数据检索key
                String key = opp.OwnerId + '_' + opp.Opportunity_Category__c+'_'+String.valueOf(opp.CloseDate);
                //system.debug('===key'+key);
                oppMap.put(key, opp);
            }
        }
        //
    }
}