高章伟
2022-05-16 2e38b5c0b5e2ce302aed60be311f1c56986b1c06
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
 * 注残出納表をupsert
 */
trigger NFM010UpsertStatuAchievements on Statu_Achievements__c (after insert, after update) {
    // SWAG-CE55BX 预测优化 start
    if (StaticParameter.EscapeOppandStaTrigger) {
        return;
    }
    // SWAG-CE55BX 预测优化 end
    // CHAN-AZM8SE 2018/8/27 start
    if (Label.update_Statu_Achievements_Journal.equals('true')) {
        return;
    }
    // CHAN-AZM8SE 2018/8/27 end
 
    Map<String, Statu_Achievements_Journal__c> stajUpsertMap = new Map<String, Statu_Achievements_Journal__c>();
    // CHAN-BCPCA3 需要更新或创建的虚拟DN start
    Map<String, Statu_Achievements_DN__c> staDNUpsertMap
        = new Map<String, Statu_Achievements_DN__c>();
 
    // CHAN-BCPCA3 需要更新或创建的虚拟DN end
 
    List<String> stajOldKeys = new List<String>();        // 发货日変更なし、金額変更がある場合
    Map<String, Statu_Achievements_Journal__c> stajOldKeyNewMap = new Map<String, Statu_Achievements_Journal__c>();
    List<String> stajOld2Keys = new List<String>();       // 金額変更なし、发货日変更がある場合
    Map<String, Statu_Achievements_Journal__c> stajOld2KeyNewMap = new Map<String, Statu_Achievements_Journal__c>();
    List<String> stajOldKeyDelKeyList = new List<String>();       // 发货日がありからなしに変更した場合、oldが削除対象
 
    Datetime pNow = Datetime.now();
    //--------------------CHAN-AZM8SE--START--------------------------------------------------------------------
    // 累计询价中所有注残的已发货金额
    //Set<String> oppNo = new Set<String>();
    //Map<String,Decimal> oppShippedAmount = new Map<String,Decimal>();
    //Map<String,Decimal> staUnshippedAmount = new Map<String,Decimal>();
    //Map<String,Decimal> changeAmount = new Map<String,Decimal>();
    //for(Statu_Achievements__c oppSta : Trigger.new) {
    //    oppNo.add(oppSta.Opp_Number__c);
    //    oppShippedAmount.put(oppSta.Opp_Number__c, 0);
    //    Decimal am = changeAmount.get(oppSta.Opp_Number__c) == null ? 0 : changeAmount.get(oppSta.Opp_Number__c);
    //    Decimal newAm = 0;
    //    if (oppSta.ReturnFlag__c) {
    //        newAm = -(oppSta.ContractAmount__c - (oppSta.UnshippedAmount__c == null ? 0 : oppSta.UnshippedAmount__c));
    //    } else {
    //        newAm = oppSta.ContractAmount__c - (oppSta.UnshippedAmount__c == null ? 0 : oppSta.UnshippedAmount__c);
    //    }
    //    changeAmount.put(oppSta.Opp_Number__c, am + newAm);
    //}
    //List<Statu_Achievements__c> oppStaList = [Select Id, Name, Opp_Number__c, ShippedAmount_F__c, ReturnFlag__c
    //                                        from Statu_Achievements__c where Opp_Number__c In:oppNo];
    //if (oppStaList != null && oppStaList.size() > 0) {
    //    for (Statu_Achievements__c amountSum : oppStaList) {
    //        oppShippedAmount.put(amountSum.Opp_Number__c, oppShippedAmount.get(amountSum.Opp_Number__c) + amountSum.ShippedAmount_F__c);
    //        if (amountSum.ReturnFlag__c) {
    //            staUnshippedAmount.put(amountSum.Name, (staUnshippedAmount.get(amountSum.Name) ==null ? 0 : staUnshippedAmount.get(amountSum.Name)) + amountSum.ShippedAmount_F__c);
    //        }
    //    }
    //}
 
    if (Trigger.isInsert) {
        // insertの場合、そのまま1レコードStatu_Achievements_Journal__cにinsert
        for (Statu_Achievements__c sta : Trigger.new) {
            Statu_Achievements_Journal__c staj = new Statu_Achievements_Journal__c();
            staj.IF_LastModifiedDateTime__c = pNow;
            staj.Statu_Achievements__c = sta.id;
            staj.CurrencyIsoCode = sta.CurrencyIsoCode;
            if (sta.UnshippedAmount__c == null) {
                staj.ShippedAmount__c = -sta.ContractAmount__c;
            } else {
                staj.ShippedAmount__c = sta.ContractAmount__c - sta.UnshippedAmount__c;
            }
            if (sta.ReturnFlag__c) {
                staj.DeliveryDate__c = sta.LastApproveDate__c;
                staj.ShippedAmount__c = -staj.ShippedAmount__c;
            } else {
                staj.DeliveryDate__c = sta.DeliveryDate__c;
            }
            
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN start
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 start
            System.debug('sta.orderType__c1:' + sta.orderType__c);
            if ('ZM17'.equals(sta.orderType__c) || 'ZM18'.equals(sta.orderType__c)) {
                System.debug('sta.orderType__c2:' + sta.orderType__c);
                Statu_Achievements_DN__c staDN  = new Statu_Achievements_DN__c();
                staDN.orderType__c              = sta.orderType__c;
                staDN.DeliveryDate__c           =  sta.LastApproveDate__c;
                if (sta.orderType__c.equals('ZM17')) {
                    staDN.ShippedAmount_ZM__c       =  sta.UnshippedAmount__c - sta.ContractAmount__c  ;
                } else {
                    staDN.ShippedAmount_ZM__c       =  sta.ContractAmount__c  - sta.UnshippedAmount__c ;
                }
                staDN.Statu_Achievements__c     =   sta.id;
                if (staDN.DeliveryDate__c == null) continue;
                staDN.DNKey__c                  =  sta.Name + ':' +
                                                   datetime.newInstance(
                                                       staDN.DeliveryDate__c.year(),
                                                       staDN.DeliveryDate__c.month(),
                                                       staDN.DeliveryDate__c.day()
                                                   ).format('yyyyMMdd');
                
                staDNUpsertMap.put(staDN.DNKey__c, staDN);
                continue;
            }
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 end
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN end
 
            if (staj.DeliveryDate__c == null) continue;
            staj.SoNo_DeliveryDate_Text__c = sta.Name + ':' +
                                             datetime.newInstance(
                                                 staj.DeliveryDate__c.year(), staj.DeliveryDate__c.month(), staj.DeliveryDate__c.day()
                                             ).format('yyyyMMdd');
 
            stajUpsertMap.put(staj.SoNo_DeliveryDate_Text__c, staj);
        }
    } else {
        // updateの場合、发货日、发货金额 有变化才是对象
        Map<Id, Decimal> staAmountMap = new Map<Id, Decimal>();
        List<Id> staIds = new List<String>();
 
        // CHAN-BCPCA3 同一条ZM17,ZM18 注残所有调整金额 start
        Map<Id, Decimal> staDNALAmountMap = new Map<Id, Decimal>();
        // CHAN-BCPCA3 同一条ZM17, ZM18 注残所有调整金额 end
 
        //HWAG-B4PCZ5 start 2018-09-19 更新对应询价的共享规则
        List<OpportunityShare> osList = new List<OpportunityShare>();
        list<Statu_Achievements__c> tmpSAlist =
            [select id, DeliveryDate__c, FSE_GI_LeaderID__c, FSE_SP_Leader_id__c
             , Opportunity__c
             , Opportunity__r.Hospital__r.FSE_GI_Main_Leader__r.IsActive
             , Opportunity__r.Hospital__r.FSE_SP_Main_Leader__r.IsActive
             , ForecastAccuracyObject__c , Opportunity__r.ownerId
             , Opportunity__r.OCM_man_province__c // CHAN-B6H4NT 2018/11/14
             // SWAG-B7LADC 2018/12/21 start
             , Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id
             , Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.IsActive
             , Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c
             , Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c
             , Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c
             // SWAG-B7LADC 2018/12/21 end
             from Statu_Achievements__c where id in: Trigger.new];
 
        // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID start
        list<ID> FSE_SPVicelist = new list<ID>();
        list<ID> FSE_GIVicelist = new list<ID>();
        list<ID> FSE_ENGVicelist = new list<ID>();
        map<id, string> FSE_SPViceIDtoNameMap = new  map<id, string>();
        map<id, string> FSE_GIViceIDtoNameMap = new  map<id, string>();
        map<id, string> FSE_ENGViceIDtoNameMap = new  map<id, string>();
        list<string> FSE_SP_ViceNameList = new list<string>();
        list<string> FSE_GI_ViceNameList = new list<string>();
        list<string> FSE_ENG_ViceNameList = new list<string>();
        Map<id, Map<string, list<String>>> sAIDtoVICENameMap = new Map<id, Map<string, list<String>>>();
        for (Statu_Achievements__c temSta : tmpSAlist) {
            Statu_Achievements__c OldSta = Trigger.oldMap.get(temSta.id);
            if ((temSta.DeliveryDate__c != null && temSta.ForecastAccuracyObject__c)
                    && (StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger  ||
                        OldSta.DeliveryDate__c == null || label.Statu_Achievements_share.equals('true'))) {
                Map<String, List<String>>  FSETypetoNameMap = new Map<String, List<String>> ();
                if (temSta.Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c != null) {
 
                    list<string> currentFSE_ENGUserNameList = new list<string>();
                    for (String UsrName : temSta.Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c.split('[,\\,]')) {
                        FSE_ENG_ViceNameList.add(UsrName);
                        currentFSE_ENGUserNameList.add(UsrName);
                    }
                    FSETypetoNameMap.put('FSE_ENG', currentFSE_ENGUserNameList);
 
                }
 
                if (temSta.Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c != null) {
                    list<string> currentFSE_GIUserNameList = new list<string>();
                    for (String UsrName : temSta.Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c.split('[,\\,]')) {
                        FSE_GI_ViceNameList.add(UsrName);
                        currentFSE_GIUserNameList.add(UsrName);
                    }
                    FSETypetoNameMap.put('FSE_GI', currentFSE_GIUserNameList);
                }
                if (temSta.Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c != null) {
                    list<string> currentFSE_SPUserNameList = new list<string>();
                    for (String UsrName : temSta.Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c.split('[,\\,]')) {
                        FSE_SP_ViceNameList.add(UsrName);
                        currentFSE_SPUserNameList.add(UsrName);
                    }
                    FSETypetoNameMap.put('FSE_SP', currentFSE_SPUserNameList);
 
                }
                sAIDtoVICENameMap.put(temSta.id, FSETypetoNameMap);
            }
        }
        //throw new ControllerUtil.myException( 'a' + FSE_ENG_ViceNameList + FSE_GI_ViceNameList +FSE_SP_ViceNameList);
 
        list<user> allViceUserList =
            [select id, Alias__c
             from user
             where IsActive = true
                              and (Alias__c in: FSE_ENG_ViceNameList
                                   or Alias__c in: FSE_GI_ViceNameList
                                   or Alias__c in: FSE_SP_ViceNameList )];
        system.debug('allViceUserList------:' + allViceUserList);
        for (User tempUser : allViceUserList) {
            if (FSE_ENG_ViceNameList.contains(tempUser.Alias__c)) {
                FSE_ENGVicelist.add(tempUser.id);
                FSE_ENGViceIDtoNameMap.put(tempUser.id, tempUser.Alias__c);
            }
            if (FSE_SP_ViceNameList.contains(tempUser.Alias__c)) {
                FSE_SPVicelist.add(tempUser.id);
                FSE_SPViceIDtoNameMap.put(tempUser.id, tempUser.Alias__c);
            }
            if (FSE_GI_ViceNameList.contains(tempUser.Alias__c)) {
                FSE_GIVicelist.add(tempUser.id);
                FSE_GIViceIDtoNameMap.put(tempUser.id, tempUser.Alias__c);
            }
        }
        system.debug('FSE_GIVicelist------:' + FSE_GIVicelist);
        system.debug('FSE_SPVicelist------:' + FSE_SPVicelist);
        system.debug('FSE_ENGVicelist------:' + FSE_ENGVicelist);
 
 
        // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID end
 
        for (Statu_Achievements__c sta : tmpSAlist) {
            Statu_Achievements__c OldSta = Trigger.oldMap.get(sta.id);
            //system.debug('OldSta:'+OldSta.DeliveryDate__c);
            //throw new ControllerUtil.myException('sta.Opportunity__r.OCM_man_province__c:' + sta.Opportunity__r.OCM_man_province__c);
            if (  sta.Opportunity__r.OCM_man_province__c != null &&
                    !sta.Opportunity__r.OCM_man_province__c.equals('能量') && // CHAN-B6H4NT 2018/11/14
                    (sta.DeliveryDate__c != null && sta.ForecastAccuracyObject__c)
                    && (StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger  ||
                        OldSta.DeliveryDate__c == null || label.Statu_Achievements_share.equals('true'))) {
                if (sta.FSE_GI_LeaderID__c != null
                        && sta.FSE_GI_LeaderID__c != sta.Opportunity__r.ownerId &&
                        sta.Opportunity__r.Hospital__r.FSE_GI_Main_Leader__r.IsActive) {
                    osList.add(new OpportunityShare(
                                   UserOrGroupId = sta.FSE_GI_LeaderID__c,
                                   OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit'
                               ));
                }
                if (sta.FSE_SP_Leader_id__c != null
                        && sta.FSE_SP_Leader_id__c != sta.Opportunity__r.ownerId &&
                        sta.Opportunity__r.Hospital__r.FSE_SP_Main_Leader__r.IsActive) {
                    osList.add(new OpportunityShare(
                                   UserOrGroupId = sta.FSE_SP_Leader_id__c,
                                   OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit'
                               ));
                }
 
 
                for (ID FSEGIID : FSE_GIVicelist) {
 
                    if (FSEGIID != sta.Opportunity__r.ownerId
                            && FSE_GIViceIDtoNameMap.get(FSEGIID) != null
                            && sta.Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c != null
                            && sAIDtoVICENameMap.get(sta.id) != null
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_GI') != null
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_GI').contains(FSE_GIViceIDtoNameMap.get(FSEGIID))
                       ) {
                        osList.add(new OpportunityShare(
                                       UserOrGroupId = FSEGIID,
                                       OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit'
                                   ));
 
                    }
                }
                for (ID FSESPID : FSE_SPVicelist) {
                    if (FSESPID != sta.Opportunity__r.ownerId
                            && FSE_SPViceIDtoNameMap.get(FSESPID) != null
                            && sta.Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c != null
                            && sAIDtoVICENameMap.get(sta.id) != null
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_SP') != null
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_SP').contains(FSE_SPViceIDtoNameMap.get(FSESPID))
                       ) {
                        osList.add(new OpportunityShare(
                                       UserOrGroupId = FSESPID,
                                       OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit'
                                   ));
 
                    }
                }
                osList.add(new OpportunityShare(
                               UserOrGroupId = Label.Group_ServiceManagementID ,
                               OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Read'
                           ));
            }
            if (sta.Opportunity__r.OCM_man_province__c != null &&
                    sta.Opportunity__r.OCM_man_province__c.equals('能量') && // CHAN-B6H4NT 2018/11/14
                    (sta.DeliveryDate__c != null && sta.ForecastAccuracyObject__c)
                    && (StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger  ||
                        OldSta.DeliveryDate__c == null || label.Statu_Achievements_share.equals('true'))) {
                if (sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id != null
                        && sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id != sta.Opportunity__r.ownerId &&
                        sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.IsActive) {
                    osList.add(new OpportunityShare(
                                   UserOrGroupId = sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id,
                                   OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit'
                               ));
                }
                for (ID FSEENGID : FSE_ENGVicelist) {
                    if (FSEENGID != sta.Opportunity__r.ownerId
                            && FSE_ENGViceIDtoNameMap.get(FSEENGID) != null
                            && sta.Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c != null
                            && sAIDtoVICENameMap.get(sta.id) != null
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_ENG') != null
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_ENG').contains(FSE_ENGViceIDtoNameMap.get(FSEENGID))
 
                       ) {
                        osList.add(new OpportunityShare(
                                       UserOrGroupId = FSEENGID,
                                       OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit'
                                   ));
 
                    }
                }
            }
        }
        if (osList.size() > 0) {
            upsert osList;
        }
        //HWAG-B4PCZ5 end  2018-09-19 更新对应询价的共享规则
 
        for (Statu_Achievements__c sta : Trigger.new) {
            //Statu_Achievements__c oldTemp = Trigger.oldMap.get(sta.Id);
            //if (oldTemp.UnshippedAmount__c == sta.UnshippedAmount__c) {
            //    continue;
            //}
            Statu_Achievements_Journal__c staj = new Statu_Achievements_Journal__c();
            staj.IF_LastModifiedDateTime__c = pNow;
            staj.Statu_Achievements__c = sta.id;
            staj.CurrencyIsoCode = sta.CurrencyIsoCode;
            if (sta.UnshippedAmount__c == null) {
                staj.ShippedAmount__c = sta.ContractAmount__c;
            } else {
                staj.ShippedAmount__c = sta.ContractAmount__c - sta.UnshippedAmount__c;
            }
            //CHAN-BAN9DG 2019/03/27 start 注释为原有逻辑 退货注残出纳表的发货日从注残最终审判日改为发货日
            //if (sta.ReturnFlag__c) {
            //    staj.ShippedAmount__c = -staj.ShippedAmount__c;
            //    staj.DeliveryDate__c = sta.LastApproveDate__c;
            //} else {
            //    staj.DeliveryDate__c = sta.DeliveryDate__c;
            //}
            if (sta.ReturnFlag__c) {
                staj.ShippedAmount__c = -staj.ShippedAmount__c;
 
            }
            staj.DeliveryDate__c = sta.DeliveryDate__c;
 
            // CHAN-BAN9DG 2019/03/27 end  注释为原有逻辑
 
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN start
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 start
            if ('ZM17'.equals(sta.orderType__c) || 'ZM18'.equals(sta.orderType__c)) {
                Statu_Achievements_DN__c staDN  = new Statu_Achievements_DN__c();
                staDN.orderType__c              = sta.orderType__c;
                staDN.DeliveryDate__c           =  sta.LastApproveDate__c;
                if (sta.orderType__c.equals('ZM17')) {
                    staDN.ShippedAmount_ZM__c       =  sta.UnshippedAmount__c - sta.ContractAmount__c  ;
                } else {
                    staDN.ShippedAmount_ZM__c       =  sta.ContractAmount__c  - sta.UnshippedAmount__c ;
                }
                staDN.Statu_Achievements__c     =   sta.id;
                if (staDN.DeliveryDate__c == null) continue;
                staDN.DNKey__c                  =  sta.Name + ':' +
                                                   datetime.newInstance(
                                                       staDN.DeliveryDate__c.year(),
                                                       staDN.DeliveryDate__c.month(),
                                                       staDN.DeliveryDate__c.day()
                                                   ).format('yyyyMMdd');
                
                staIds.add(sta.Id);
                staDNUpsertMap.put(staDN.DNKey__c, staDN);
                staDNALAmountMap.put(sta.Id, 0);
                continue;
            }
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 end
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN end
            if (staj.DeliveryDate__c == null) continue;
            if (staj.DeliveryDate__c != null) {
                staj.SoNo_DeliveryDate_Text__c = sta.Name + ':' +
                                                 datetime.newInstance(
                                                     staj.DeliveryDate__c.year(),
                                                     staj.DeliveryDate__c.month(),
                                                     staj.DeliveryDate__c.day()
                                                 ).format('yyyyMMdd');
            }
            staIds.add(sta.Id);
            stajUpsertMap.put(staj.SoNo_DeliveryDate_Text__c, staj);
            staAmountMap.put(sta.Id, 0);
        }
 
        //  CHAN-BCPCA3 检索该注残下现有的虚拟DN 并计算 虚拟DN值 start
        /*
         *
         */
        List<Statu_Achievements_DN__c> staDNList =
            [Select Id, Statu_Achievements__c, DeliveryDate__c, ShippedAmount_ZM__c, DNKey__c,orderType__c 
             from Statu_Achievements_DN__c
             where Statu_Achievements__r.Id in : staIds];
 
        if (staDNList != null && staDNList.size() > 0) {
            for (Statu_Achievements_DN__c staDN : staDNList) {
                if (staDN.orderType__c == null)
                    continue;
                staDNALAmountMap.put(staDN.Statu_Achievements__c, staDN.ShippedAmount_ZM__c
                                     + staDNALAmountMap.get(staDN.Statu_Achievements__c));
                if (staDNUpsertMap.containsKey(staDN.DNKey__c)) {
                    Statu_Achievements_DN__c tempStaDN = staDNUpsertMap.get(staDN.DNKey__c);
                    tempStaDN.ShippedAmount_ZM__c += staDN.ShippedAmount_ZM__c;
                }
            }
        }
        for (String staDNKey : staDNUpsertMap.keySet()) {
            Statu_Achievements_DN__c tempStaDN = staDNUpsertMap.get(staDNKey);
            tempStaDN.ShippedAmount_ZM__c = tempStaDN.ShippedAmount_ZM__c
                                            - staDNALAmountMap.get(tempStaDN.Statu_Achievements__c);
        }
        //  CHAN-BCPCA3 检索该注残下现有的虚拟DN 并计算 虚拟DN值 end
        List<Statu_Achievements_Journal__c> stajList = [Select Id, Statu_Achievements__c, DeliveryDate__c, ShippedAmount__c, SoNo_DeliveryDate_Text__c
                from Statu_Achievements_Journal__c where Statu_Achievements__r.Id in : staIds];
                if (stajList != null && stajList.size() > 0) {
                    for (Statu_Achievements_Journal__c sajc : stajList) {
                        staAmountMap.put(sajc.Statu_Achievements__c, sajc.ShippedAmount__c
                                 + staAmountMap.get(sajc.Statu_Achievements__c));
                if (stajUpsertMap.containsKey(sajc.SoNo_DeliveryDate_Text__c)) {
                    Statu_Achievements_Journal__c tempStaj = stajUpsertMap.get(sajc.SoNo_DeliveryDate_Text__c);
                    tempStaj.ShippedAmount__c += sajc.ShippedAmount__c;
                }
            }
        }
        for (String stajKey : stajUpsertMap.keySet()) {
            Statu_Achievements_Journal__c tempStaj = stajUpsertMap.get(stajKey);
            tempStaj.ShippedAmount__c = tempStaj.ShippedAmount__c - staAmountMap.get(tempStaj.Statu_Achievements__c);
        }
    }
 
    // SoNo_DeliveryDate_Text__c にて upsert
    if (stajUpsertMap.size() > 0) {
        ControllerUtil.upsertStatuAchievementsJournalByKey(stajUpsertMap.values());
    }
 
    // CHAN-BCPCA3 插入并更新虚拟DN数据 start
    if (staDNUpsertMap.size() > 0) {
        ControllerUtil.upsertStatuAchievementsDNByKey(staDNUpsertMap.values());
    }
 
    // CHAN-BCPCA3 插入并更新虚拟DN数据 end
    // 削除
    //ControllerUtil.deleteStatuAchievementsJournalByKey(stajOldKeyDelKeyList);
    //--------------------CHAN-AZM8SE--END--------------------------------------------------------------------
}