buli
2023-05-23 07390e2fcb4adf27c928335bf27ae7939c5a80ad
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
public with sharing class ConsumEquipmentSetReturnController extends CreateRelationListPagingCtrlBase{
    private String cesIdsStr;
    public String caIdStr {get;set;}
    private List<String> cesIdList;
    public String c_apply_no { get; private set; }
    public Boolean done_flg { get;  set; }
    private String keywdSort = null;
    public FixtureDeliverySlip__c slip { get; set; }
    public String keepWher { get; set; }
    public String keyword { get; set; }
    public transient String keywordDate { get; set; }
    public Boolean saveBtnDisabled { get; private set; }
    public String userid {get;set;}
    public  Map<Id, Consum_Apply_Equipment_Set_Detail__c> caesdVFMap { get; private set; } //transient
    private List<Consum_Apply_Equipment_Set_Detail__c> useCaesdList;
 
    public ConsumEquipmentSetReturnController() {
        caIdStr = ApexPages.currentPage().getParameters().get('id');
        String saved = ApexPages.currentPage().getParameters().get('save') ;
        if (saved != null && saved == '1') {
            done_flg = true;
        }
    }
 
    public override Integer getSearchNumMax() {
        //各ページに制御あれば、最大件数を指定する
        // searchNumMax = Integer.valueOf(Label.Product_Select_Limit);
        searchNumMax = 370;
        pagesize = '20';
        return searchNumMax;
    }
 
    /* 選択されたデータ取得用Soql Fromから*/
    public override String getSelectedDataSql() {
        // オブジェクトAPI名
        selectedDataSql = ' From Consum_Apply_Equipment_Set_Detail__c';
        selectedDataSql += ' where Id != null ';
        selectedDataSql += '   and (';
        selectedDataSql += 'Consum_Apply__r.Name = \'' + String.escapeSingleQuotes(caIdStr) + '\' OR Consum_Apply__c = \'' + String.escapeSingleQuotes(caIdStr) + '\'';
        selectedDataSql += ')';
        selectedDataSql += '   and Shippment_loaner_time2__c != null';
        selectedDataSql += ' AND (Cancel_Select__c = false OR NG_Select_Again__c = true)';
        selectedDataSql += ' order by Fixture_Model_No_F__c nulls last, Consumable_Guaranteen_end_F__c nulls last, name';
        keepWher = selectedDataSql;
        myComponentController.columnRightRW.put('Yi_Fa_Huo_Jia__c', 'r');
        myComponentController.columnRightRW.put('Dao_Huo_OK_Jia__c', 'r');
        myComponentController.columnRightRW.put('Dao_Huo_NG_Jia__c', 'r');
        myComponentController.columnRightRW.put('Wei_Dao_Huo_Jia__c', 'r');
        myComponentController.columnRightRW.put('Dao_Huo_NG_Confirm_Jia__c', 'r');
        myComponentController.columnRightRW.put('Dao_Huo_NG_UnConfirm_Jia__c', 'r');
        myComponentController.columnRightRW.put('Yi_Xiao_Hao_Jia__c', 'r');
        myComponentController.columnRightRW.put('Hui_Ji_Jia__c', 'w');
        myComponentController.columnRightRW.put('Dai_Xiao_Hao_Jia__c', 'r');
 
        return selectedDataSql;
    }
 
    // 検索元対象オブジェクトAPI名
    public override String getOriginObjName() {
        // オブジェクトAPI名
        originObjName = 'Consum_Apply_Equipment_Set_Detail__c';
        return originObjName;
    }
    public override String getOriginObjColumns() {
        // 項目セット
        originObjColumns =  'Id';
        return originObjColumns;
    }
 
    public override String getObjName() {
        // オブジェクトAPI名
        objName = 'Consum_Apply_Equipment_Set_Detail__c';
        return objName;
    }
    public override String getColumnLeftFieldSetName() {
        // 左の項目セット
        columnLeftFieldSetName = '';
        return columnLeftFieldSetName;
    }
    public override String getColumnRightFieldSetName() {
        // 右の項目セット
        columnRightFieldSetName = 'ConsumEquipmentSetReturn_R';
        return columnRightFieldSetName;
    }
 
    public override List<String> getColumnFieldList() {
        // strColumus 里加 field
        // FixtureUtil#raesdGroupBy()の項目も必要
        return new List<String>{'Id'
                              , 'Asset__c'
                              , 'Shippment_loaner_time2__c'
                              , 'Received_Confirm__c'
                              , 'Asset_Center_Confirm__c'
                              , 'Check_lost_Item_Final__c'
                              , 'Check_lost_Item__c'
                              , 'Shipment_Saved_Flg__c'
                              , 'Return_DeliverySlip__c'
                              , 'Fixture_Model_No__c'
                              , 'SerialNumber_F__c'
                              , 'ManagementCode__c'
                              , 'EquipmentManagementCode__c' //add by yc 20220217 
                              , 'EquipmentManagementCode_OldData__c' //add by yc 20220217 
                              , 'Shipment_request_time2__c', 'Consum_Apply_Equipment_Set__r.Yi_Shipment_request__c'};
    }
    // getObjName 连 getOriginObjName 的 FK
    public override String getFKColumnField() {
        return null;
    }
 
    public override String getRecordTypeId() {
        //ページレイアウトを収得するのレコードタイプ
        recordTypeId = '';
        return recordTypeId;
    }
 
    // ページコントローラに検索処理は、WhereSoql作成のみ、パラメータとして、コンポーネントに渡される
    public override String getSqlWhereStr() {
        sqlWhereStr = '';
        sqlWhereStr = this.makeSoql(keywdSort);
        System.debug('sqlWhereStr is:' + sqlWhereStr);
        return sqlWhereStr;
    }
 
    public override String getOrderbyStr() {
        return 'order by Fixture_Model_No_F__c nulls last, Consumable_Guaranteen_end_F__c nulls last, name';
    }
 
    public override Boolean getIsNeedRunSearch() {
 
      //   if (String.isBlank(keyword)) {
            // isNeedRunSearch = false;
      //   }else{
            // isNeedRunSearch = true;
      //   }
        return false;
    }
 
    public void init() {
 
        Set<Id> rnsIdSet = new Set<Id>();
        userid = Userinfo.getUserId();
        if (caIdStr != null) {
            // 借出耗材申请一览
            List<Consum_Apply__c> caList = [select Id
                                            from Consum_Apply__c
                                           where (Name =:caIdStr or Id =:caIdStr)
                                             ];
            if (caList.isEmpty()) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '耗材备品申请不存在'));
                saveBtnDisabled = true;
                return;
            }
            parentId = caList[0].Id;
        }
 
        slip = new FixtureDeliverySlip__c();
        slip.Wh_Staff__c = userid;
        slip.ConsumSlip__c = true;
 
        searchOppSetParam();
        getSqlWhereStr();
 
        // return null;
    }
 
    private void searchOppSetParam() {
        keywdSort = keyword;
    }
 
    public void searchSlip() {
        String qryString = 'select Id, Shippment_loaner_time__c, Name, DeliveryCompany__c, Wh_Staff__c, Combine_Pack__c, Distributor_method__c, DeliveryType__c, DeliveryCompany_SlipNo__c '
                + 'from FixtureDeliverySlip__c '
                + 'where Name =\''+ slip.Name +'\' and DeliveryType__c = \'回寄\'';
        if (String.isNotBlank(slip.Distributor_method__c)) {
            qryString += ' and Distributor_method__c = \''+ slip.Distributor_method__c +'\'';
        }
        if (String.isNotBlank(slip.DeliveryCompany__c)) {
            qryString += ' and DeliveryCompany__c = \''+ slip.DeliveryCompany__c +'\'';
        }
        List<FixtureDeliverySlip__c> slipList = Database.query(qryString);
 
        if (slipList.size() > 0) {
            if (slipList.size() > 1) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '当前条件检索到复数条运输单,请追加检索条件!'));
            }
            slip = slipList[0];
        }
    }
 
    public PageReference save() {
        if(String.isEmpty(slip.Name)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'没有填写运输单号!'));
            return null;
        }
        if(String.isEmpty(slip.Distributor_method__c)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'没有选择运输方式!'));
            return null;
        }
        if(String.isEmpty(slip.DeliveryCompany__c)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'没有选择物流公司!'));
            return null;
        }
        if(String.isEmpty(slip.Wh_Staff__c)){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'没有选择担当者!'));
            return null;
        }
        Map<String, Integer> sendAssetKey = new Map<String, Integer>();
        for (WrapperInfo wi : viewList) {
            Consum_Apply_Equipment_Set_Detail__c caesd = (Consum_Apply_Equipment_Set_Detail__c)wi.sobj;
            if (!wi.check && caesd.Return_DeliverySlip__c == null) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'请全部勾选后再保存!'));
                return null;
            }
            Integer cnt = caesd.Hui_Ji_Jia__c == null ? 0 : Integer.valueOf(caesd.Hui_Ji_Jia__c);
            String asscodeX = caesd.Asset__c + caesd.EquipmentManagementCode__c + caesd.ManagementCode__c;
            //add by yc 20220217 处理老数据开的口 start
            if (String.isEmpty(caesd.EquipmentManagementCode__c)) {
                   asscodeX = caesd.Asset__c + caesd.EquipmentManagementCode_OldData__c + caesd.ManagementCode__c;
            }  
            //add by yc 20220217 处理老数据开的口 end
            //要发货件数
            if(caesd.Return_DeliverySlip__c == null) {
                //20211115 耗材追溯 yc 方法中所有得 caesd.Asset__c-->caesd.Id  因为此时的数据是clone没有id的所以用asset加上备品管理码做key
                sendAssetKey.put(asscodeX, cnt);
            }
            System.debug('zheli0'+caesd+'==='+caesd.Asset__c);
 
        }
 
 
        List<Consum_Apply_Equipment_Set_Detail__c> updList = new List<Consum_Apply_Equipment_Set_Detail__c>();
        List<Consum_Apply_Equipment_Set_Detail__c> sendbackList = new List<Consum_Apply_Equipment_Set_Detail__c>();
 
        for (Consum_Apply_Equipment_Set_Detail__c caesd : useCaesdList) {
            if(caesd.Return_DeliverySlip__c != null) {
                continue;
            }
             //20211115 耗材追溯 yc 定义key
            String asscodeX1 = caesd.Asset__c + caesd.EquipmentManagementCode__c + caesd.ManagementCode__c; 
            //add by yc 20220217 处理老数据开的口 start
            if (String.isEmpty(caesd.EquipmentManagementCode__c)) {
                asscodeX1 = caesd.Asset__c + caesd.EquipmentManagementCode_OldData__c + caesd.ManagementCode__c;
            }  
            //add by yc 20220217 处理老数据开的口 end
            System.debug('zheli1'+sendAssetKey);
            if (sendAssetKey.containsKey(asscodeX1)) {
                Integer cnt = sendAssetKey.get(asscodeX1);
                System.debug('zheli2'+cnt+'');
                if (caesd.Received_Confirm__c == 'OK' || caesd.Received_Confirm__c == '默认签收-OK') {
                    // 需要寄回的明细
                    if(cnt > 0) {
                        cnt -= 1;
                        sendAssetKey.put(asscodeX1, cnt);
                        sendbackList.add(caesd);
                        //20211115 耗材追溯 yc 方法中所有得 caesd.Asset__c-->caesd.Id  
                        caesdVFMap.get(caesd.Id).Dai_Xiao_Hao_Jia__c = caesdVFMap.get(caesd.Id).Dao_Huo_OK_Jia__c
                                                                           + caesdVFMap.get(caesd.Id).Dao_Huo_NG_Jia__c
                                                                           - caesdVFMap.get(caesd.Id).Yi_Xiao_Hao_Jia__c
                                                                           - caesdVFMap.get(caesd.Id).Hui_Ji_Jia__c;
                    }
                    // 不需要寄回的明细,变成待消耗
                    else {
                        System.debug('zheli3'+cnt+'');
                        caesd.Check_lost_Item__c = '消耗';
                        // 待消耗操作人和时间在trigger里改
                        updList.add(caesd);
                    }
                }
            }
            // 待消耗数为0 <=> 画面上不显示 => 直接待消耗
            else {
                System.debug('zheli4');
                caesd.Check_lost_Item__c = '消耗';
                // 待消耗操作人和时间在trigger里改
                updList.add(caesd);
            }
        }
 
        Savepoint sp = Database.setSavepoint();
        try {
            if (sendbackList.size() > 0) {
                slip.Shippment_loaner_time__c = Datetime.now();
                String message = '';
                FixtureUtil.withoutUpsertObjects(new List<FixtureDeliverySlip__c>{slip});
                for (Consum_Apply_Equipment_Set_Detail__c caesd : sendbackList) {
                    caesd.Return_DeliverySlip__c = slip.Id;
                    caesd.Return_Operator__c = slip.Wh_Staff__c;
                    caesd.Asset_return_time__c = slip.Shippment_loaner_time__c;
                    updList.add(caesd);
                }
            }
            if (updList.size() > 0) {
                FixtureUtil.withoutUpdate(updList);
            }
            done_flg = true;
 
            PageReference pg = new PageReference('/apex/ConsumEquipmentSetReturn');
            pg.getParameters().put('id',caIdStr);
            pg.getParameters().put('save','1');
            pg.setRedirect(true);
            return pg;
        } catch (Exception ex) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()));
            Database.rollback(sp);
            done_flg = false;
            return null;
        }
    }
 
    public void cancel() {
        for (WrapperInfo wi : viewList) {
            Consum_Apply_Equipment_Set_Detail__c caesd = (Consum_Apply_Equipment_Set_Detail__c)wi.sobj;
            if(caesd.Return_DeliverySlip__c == null){
                caesd.Hui_Ji_Jia__c = 0;
                caesd.Dai_Xiao_Hao_Jia__c = caesd.Dao_Huo_OK_Jia__c
                                          + caesd.Dao_Huo_NG_Jia__c
                                          - caesd.Yi_Xiao_Hao_Jia__c
                                          - caesd.Hui_Ji_Jia__c;
                wi.check = false;
            }
        }
        slip = new FixtureDeliverySlip__c();
        slip.deliveryType__c = '回寄';
        slip.Wh_Staff__c = userid;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '取消成功'));
    }
 
    private String makeSoql(String keyword) {
        // 検索条件
 
        return keepWher;
    }
 
    public override void setViewList(List<Sobject> queryList) {
        viewList = new List<WrapperInfo>();
        if (queryList.size() > 0) {
            useCaesdList = (List<Consum_Apply_Equipment_Set_Detail__c>)queryList;
        }
        else {
            useCaesdList = (List<Consum_Apply_Equipment_Set_Detail__c>)selectedData;
        }
        system.debug(queryList+'zheli123'+useCaesdList);
        // asset.Id-->连到asset上的明细列表
        Map<Id, List<Consum_Apply_Equipment_Set_Detail__c>> caesdMap = new Map<Id, List<Consum_Apply_Equipment_Set_Detail__c>>();
        // asset.Id-->用于画面显示的假明细
        caesdVFMap = new Map<Id, Consum_Apply_Equipment_Set_Detail__c>();
        Integer i = 0;
        //20211018 耗材追溯 yc 方法中所有得 caesd.Asset__c-->caesd.Id  因为“已发货件数”不用累计了,需要展开
        for (Consum_Apply_Equipment_Set_Detail__c caesd : useCaesdList) {
            if (!caesdMap.containsKey(caesd.Id)) {
                caesdMap.put(caesd.Id, new List<Consum_Apply_Equipment_Set_Detail__c>());
            }
            caesdMap.get(caesd.Id).add(caesd);
            system.debug(caesdMap+'zheli5'+caesd.Id);
 
            if (i == getSearchNumMax()) { break; }
            i++;
            if(!caesdVFMap.containsKey(caesd.Id)){
                Consum_Apply_Equipment_Set_Detail__c caesdVF = caesd.clone();
                caesdVF.Received_Confirm__c = null;
                caesdVF.Asset_Center_Confirm__c = null;
                caesdVF.Check_lost_Item_Final__c = null;
                caesdVF.Check_lost_Item__c = null;
                caesdVF.Return_DeliverySlip__c = null;
                // 已发货件数
                caesdVF.Yi_Fa_Huo_Jia__c = 0;
                // 到货确认OK件数
                caesdVF.Dao_Huo_OK_Jia__c = 0;
                // 到货NG件数
                caesdVF.Dao_Huo_NG_Jia__c = 0;
                // 未到货确认件数
                caesdVF.Wei_Dao_Huo_Jia__c = 0;
                // 到货确认NG件数
                caesdVF.Dao_Huo_NG_Confirm_Jia__c = 0;
                // 到货确认未确认数
                caesdVF.Dao_Huo_NG_UnConfirm_Jia__c = 0;
                // 已消耗件数
                caesdVF.Yi_Xiao_Hao_Jia__c = 0;
                // 回寄件数
                caesdVF.Hui_Ji_Jia__c = 0;
                // 待消耗件数
                caesdVF.Dai_Xiao_Hao_Jia__c = 0;
                caesdVFMap.put(caesd.Id, caesdVF);
            }
            Consum_Apply_Equipment_Set_Detail__c caesdVF = caesdVFMap.get(caesd.Id);
            if(caesdVF.Return_DeliverySlip__c == null && caesd.Return_DeliverySlip__c!=null) {
                caesdVF.Return_DeliverySlip__c = caesd.Return_DeliverySlip__c;
            }
            system.debug('zheli6'+caesd.Return_DeliverySlip__c);
            if(caesdVF.Check_lost_Item__c == null
               && (caesd.Check_lost_Item__c == '消耗' && String.isBlank(caesd.Check_lost_Item_Final__c))
               ) {
               caesdVF.Check_lost_Item__c = caesd.Check_lost_Item__c;
            }
 
            caesdVF.Yi_Fa_Huo_Jia__c += caesd.Shippment_loaner_time2__c != null ? 1 : 0;
            caesdVF.Dao_Huo_OK_Jia__c += caesd.Received_Confirm__c == 'OK' || caesd.Received_Confirm__c == '默认签收-OK' ? 1 : 0;
            caesdVF.Dao_Huo_NG_Jia__c += caesd.Received_Confirm__c == 'NG' ? 1 : 0;
            caesdVF.Wei_Dao_Huo_Jia__c += String.isBlank(caesd.Received_Confirm__c) ? 1 : 0;
            caesdVF.Dao_Huo_NG_Confirm_Jia__c += caesd.Received_Confirm__c =='NG' && caesd.Asset_Center_Confirm__c=='OK' ? 1 : 0;
            caesdVF.Dao_Huo_NG_UnConfirm_Jia__c += caesd.Received_Confirm__c =='NG' && String.isBlank(caesd.Asset_Center_Confirm__c) ? 1 : 0;
            caesdVF.Yi_Xiao_Hao_Jia__c += caesd.Check_lost_Item_Final__c == '消耗' ? 1 : 0;
            caesdVF.Hui_Ji_Jia__c += caesd.Return_DeliverySlip__c != null ? 1 : 0;
            caesdVF.Dai_Xiao_Hao_Jia__c = caesdVF.Dao_Huo_OK_Jia__c + caesdVF.Dao_Huo_NG_Jia__c - caesdVF.Yi_Xiao_Hao_Jia__c - caesdVF.Hui_Ji_Jia__c;
        }
 
        for(Consum_Apply_Equipment_Set_Detail__c caesdVF : caesdVFMap.values()){
            system.debug('zheli7'+caesdVF);
            system.debug('zheli8'+caesdVF.Dai_Xiao_Hao_Jia__c+'====='+caesdVF.Return_DeliverySlip__c);
            if(caesdVF.Dai_Xiao_Hao_Jia__c == 0 && caesdVF.Return_DeliverySlip__c == null) {
                continue;
            }
            WrapperInfo winfo = new WrapperInfo(caesdVF, myComponentController);
            system.debug('zheli9'+winfo);
            if (caesdVF.Return_DeliverySlip__c != null 
                || caesdVF.Check_lost_Item__c == '消耗'
            ) {
                winfo.canEdit = false;
            }
            winfo.check = false;
            viewList.add(winfo);
        }
    }
 
    @TestVisible private static void test() {
        if (false == Test.isRunningTest()) return;
        Integer i = 0;
       
    }
}