高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
global with sharing class InventoryReportDetailController extends CreateRelationListPagingCtrlBase {
    public override Integer getSearchNumMax() {
        //各ページに制御あれば、最大件数を指定する
        // searchNumMax = Integer.valueOf(Label.Product_Select_Limit);
        // searchNumMax = 100;
        pagesize = '200';
        return 20000;
    }
    public override Boolean getIsNeedRunSearch() {
        return true;
    }
    public override String getWidth0Field() {
        return '';
    }
    /* 選択されたデータ取得用Soql Fromから*/
    public override String getSelectedDataSql() {
        myComponentController.columnRightRW.put('Inventory_Chujie_Count_Jia__c', 'r');
        myComponentController.columnRightRW.put('WH_Location__c', 'r');
        myComponentController.columnRightRW.put('SerialNumber__c', 'r');
        myComponentController.columnRightRW.put('Equipment_Type__c', 'r');
        myComponentController.columnRightRW.put('Fixture_Model_No__c', 'r');
        myComponentController.columnRightRW.put('Internal_asset_location__c', 'r');
        myComponentController.columnRightRW.put('Salesdepartment__c', 'r');
        myComponentController.columnRightRW.put('Inventory_Diaobo_Count_Jia__c', 'r');//20210531 you 1650
        return '';
    }
    public override List<String> getWritableColumnFieldList() {
        return new List<String>{'Remarks_Person__c', 'Inventory_Remarks__c'};
    }
    public override String getOriginObjName() {
        // オブジェクトAPI名
        if(this.isConsum){
            originObjName = 'Consum_Inventory_Detail__c';
        }
        else{
            originObjName = 'Inventory_Detail__c';
        }
        return originObjName;
    }
    public override String getOriginObjColumns() {
        // 項目セット
        originObjColumns = 'Id';
        return originObjColumns;
    }
    public override String getObjName() {
        // オブジェクトAPI名
        if(this.isConsum){
            objName = 'Consum_Inventory_Detail__c';
        }
        else{
            objName = 'Inventory_Detail__c';
        }
        return objName;
    }
    // ページコントローラに検索処理は、WhereSoql作成のみ、パラメータとして、コンポーネントに渡される
    public override String getSqlWhereStr() {
        sqlWhereStr = '';
 
        if (getIsNeedRunSearch()) {
            sqlWhereStr = this.makeSoql(keywdSort);
            system.debug(sqlWhereStr);
        }
        return sqlWhereStr;
    }
     public override String getColumnLeftFieldSetName() {
        // 左の項目セット
        // columnLeftFieldSetName = 'InventoryReportRecord_FieldSet';
        // return columnLeftFieldSetName;
        return '';
    }
    public override String getColumnRightFieldSetName() {
        // 右の項目セット
        if(this.isConsum){
            columnRightFieldSetName = 'ConsumInventoryReportRecord_FieldSet';
        } else if(angecylookrole==false){//20210628 you 1650
            columnRightFieldSetName = 'InventoryReportRecord_FieldSet_Angecy';
        }else{
            columnRightFieldSetName = 'InventoryReportRecord_FieldSet';
        } 
        return columnRightFieldSetName;
    }
    public override List<String> getColumnFieldList() {
        // strColumus 里加 field
        return new List<String>{'Asset__c'};
    }
    public override String getFKColumnField() {
        // getObjName 连 getOriginObjName 的 FK
        return '';
    }
    public override String getRecordTypeId() {
        //ページレイアウトを収得するのレコードタイプ
        recordTypeId = '';
        return recordTypeId;
    }
    public override String getOrderbyStr() {
        return 'order by Inventory_Remarks__c ASC nulls last, Fixture_Model_No__c, SerialNumber__c';
    }
 
    private String keywdSort = null;
    public String saveType { get; set; }
    public String keyword { get; set; }
    public Inventory_Header__c parentObj { get; private set; }
    public String invDetailResultRemark {get; private set;}     // 盘点明细画面查询结果说明
    public static bp3_Setting__c conf = bp3_Setting__c.getOrgDefaults();
    public String assetStatus {get; set;}  // 备品状态 Asset_Status__c
    public String compareOperator {get; set;}
    public Consum_Inventory_Detail__c sampleCiDetail {get;set;}
    //public Asset sonObj { get; set; }
    public Boolean isConsum {get;set;}
    public Boolean angecylookrole {get; set;}  // 只读权限 1650 20210628
    public String bieCunFangDi { get; set; }     //别存放地, 肯定有值 不为null
 
    public List<SelectOption> assetStatusOptionList {
        get {
            List<SelectOption> selectOptions = new List<SelectOption> {new SelectOption('', '--全部--') };
            selectOptions.add(new SelectOption('未盘点', '未盘点'));
            selectOptions.add(new SelectOption('已盘点', '已盘点'));
            return selectOptions;
        }
    }
 
    private void searchOppSetParam() {
        this.keywdSort = this.keyword;
    }
    public PageReference searchOpp() {
        searchOppSetParam();
 
        if (!getIsNeedRunSearch()) {
            return null;
        }
 
        getSqlWhereStr();
        myComponentController.searchAndPaging();
        return null;
    }
 
    public InventoryReportDetailController() {
    }
    public InventoryReportDetailController(ApexPages.StandardController controller) {
        try {
            angecylookrole = false;
            isNeedCheckEvent = true;
            this.parentId = ApexPages.currentPage().getParameters().get('ivt_headId');
            this.invDetailResultRemark = conf.Inventory_Detail_Result_Remark__c;
            this.sampleCiDetail = new Consum_Inventory_Detail__c();
            this.isConsum = false;
 
            // 盘点表头の情報を取得--有ivt_headId的话根据id取盘点表头,否则取当前用户所在备品存放地最新一条
            if (!String.isBlank(this.parentId)) {
                List<Inventory_Header__c> parentObjs = [
                        SELECT Id, Internal_asset_location__c, Name, Inventory_Start_Date__c
                        , Inventory_Status__c, InventoryPIC__c, Fixture_Header__c
                        FROM Inventory_Header__c
                        WHERE Id = :parentId
                        ];
                if (!parentObjs.isEmpty()) {
                    parentObj = parentObjs.get(0);
                    //20210628 you 1650 办事处看不到 货架号
                    bieCunFangDi = parentObj.Internal_asset_location__c;
                    system.debug('zheli0'+bieCunFangDi);
                    if (bieCunFangDi=='广州 备品中心' || bieCunFangDi=='北京 备品中心' || bieCunFangDi=='上海 备品中心') {
                        angecylookrole =true;
                    }
                } else {
                    throw new ControllerUtil.myException('没有可以操作的盘点明细或者盘点表头数据不正确。');
                }
            } else {
                User currUser = [
                        SELECT Id, Name, Default_Referable_Apply_Equipment_Center__c
                        FROM User
                        WHERE Id = :UserInfo.getUserId()
                        ];
                if (String.isBlank(currUser.Default_Referable_Apply_Equipment_Center__c)) {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '当前用户所在地不能为空'));
                }
                //20210628 you 1650 办事处看不到 货架号
                bieCunFangDi = currUser.Default_Referable_Apply_Equipment_Center__c;
                if (bieCunFangDi=='广州 备品中心' || bieCunFangDi=='北京 备品中心' || bieCunFangDi=='上海 备品中心') {
                    angecylookrole =true;
                }
                List<Inventory_Header__c> ihObjList = [
                        SELECT Id, Internal_asset_location__c, Name, Inventory_Start_Date__c
                        , Inventory_Status__c, InventoryPIC__c, Fixture_Header__c
                        FROM Inventory_Header__c
                        WHERE Internal_asset_location__c = :currUser.Default_Referable_Apply_Equipment_Center__c
                          AND Inventory_Status__c <> '处理中'
                        ORDER BY Inventory_Start_Date__c desc
                        LIMIT 1
                        ];
                if (ihObjList.size() == 0) {
                    this.parentId = 'temp'; // 如果parentId为空,当前controller的myexception会被共通的'Id必须指定'覆盖
                    throw new ControllerUtil.myException('当前用户所在地区没有盘点报告, 或者盘点报告状态为处理中');
                } else {
                    parentObj = ihObjList[0];
                    this.parentId = parentObj.Id;
                }
            }
            this.isConsum = String.isNotBlank(parentObj.Fixture_Header__c);
 
        } catch (ControllerUtil.myException me) {
            System.debug(LoggingLevel.ERROR, 'myException caught when init: ' + me.getMessage() + me.getStackTraceString());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'when init ' + me.getMessage()));
            return;
        } catch (Exception e) {
            System.debug(LoggingLevel.ERROR, 'Exception caught when init: ' + e.getMessage() + e.getStackTraceString());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'when init ' + e.getMessage()));
            return;
        }
    }
    // 画面初始化
    public void init() {
        try {
            
            assetStatus = '';
            isNeedSearchFirst = true;
            isNeedCheckEvent = true;
            
            searchOppSetParam();
            getSqlWhereStr();
 
        } catch (ControllerUtil.myException me) {
            System.debug('myException caught when init: ' + me.getMessage());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, me.getMessage()));
            return;
        } catch (Exception e) {
            System.debug('Exception caught when init: ' + e.getStackTraceString());
            ApexPages.addMessages(e);
            return;
        }
    }
 
    public override void setViewList(List<sObject> queryList) {
        system.debug('●●●●● setViewList START ' );
        viewList = new List<WrapperInfo>();
 
        if (queryList.size() > 0) {
            final String soqlStr = 'SELECT {0} {1} ';
            String whereStr = ' FROM ' + this.getObjName() + ' WHERE ID IN: queryList';
            String soql = String.format(soqlStr, new String[] {myComponentController.strColumus , whereStr});
 
            List<sObject> InDList = null;
            InDList = Database.query(soql);
 
            Set<Id> assetSet = new Set<Id>();
            Map<Id, sObject> orderMap = null;
            if(this.isConsum){
                orderMap = new Map<Id, Consum_Inventory_Detail__c>();
                for (sObject sObj : InDList) {
                    Consum_Inventory_Detail__c InD = (Consum_Inventory_Detail__c) sObj;
                    orderMap.put(InD.Id, InD);
                    assetSet.add(InD.Asset__c);
                }
            }
            else{
                orderMap = new Map<Id, Inventory_Detail__c>();
                for (sObject sObj : InDList) {
                    Inventory_Detail__c InD = (Inventory_Detail__c) sObj;
                    orderMap.put(InD.Id, InD);
                    assetSet.add(InD.Asset__c);
                }
            }
            List<AggregateResult> statusArList = new List<AggregateResult>();
            List<AggregateResult> statusYList = new List<AggregateResult>();
            List<AggregateResult> statusKList = new List<AggregateResult>();
 
            if(this.isConsum){
                statusArList = [
                    SELECT Asset_Status__c status, sum(Amount__c) quantity
                        , sum(Inventory_Count__c) invShipanCount, sum(Inventory_Deviation__c) deviaCount, sum(Asset__r.Abandoned_Inventory__c) abCount
                        , Asset__c assetId
                     FROM Consum_Inventory_Detail__c
                    WHERE Asset__c in :assetSet
                      AND Inventory_Header__c = :parentId
                      AND Asset_Status__c!='已消耗明细'
                    GROUP BY Asset_Status__c, Asset__c
                    ];
                // 页面显示"盘盈/盘亏"只计算有盘点时间的
                statusYList = [
                    SELECT Asset_Status__c status, sum(Inventory_Deviation__c) yDeviaCount
                        , Asset__c assetId
                     FROM Consum_Inventory_Detail__c
                    WHERE Asset__c in :assetSet
                      AND Inventory_Header__c = :parentId
                      AND Inventory_Time__c <> null
                      AND Inventory_Deviation__c > 0        // 盘盈
                      AND Asset_Status__c!='已消耗明细'
                    GROUP BY Asset_Status__c, Asset__c
                    ];
                statusKList = [
                    SELECT Asset_Status__c status, sum(Inventory_Deviation__c) kDeviaCount
                        , Asset__c assetId
                     FROM Consum_Inventory_Detail__c
                    WHERE Asset__c in :assetSet
                      AND Inventory_Header__c = :parentId
                      AND Inventory_Time__c <> null
                      AND Inventory_Deviation__c < 0        // 盘亏
                      AND Asset_Status__c!='已消耗明细'
                    GROUP BY Asset_Status__c, Asset__c
                    ];
            }
            else{
                statusArList = [
                    SELECT Asset_Status__c status, sum(Amount__c) quantity
                        , sum(Inventory_Count__c) invShipanCount, sum(Inventory_Deviation__c) deviaCount
                        , Asset__c assetId
                     FROM Inventory_Detail__c
                    WHERE Asset__c in :assetSet
                      AND Inventory_Header__c = :parentId
                      AND Asset_Status__c <> '丢失借出明细'
                    GROUP BY Asset_Status__c, Asset__c
                    ];
                // 页面显示"盘盈/盘亏"只计算有盘点时间的
                statusYList = [
                    SELECT Asset_Status__c status, sum(Inventory_Deviation__c) yDeviaCount
                        , Asset__c assetId
                     FROM Inventory_Detail__c
                    WHERE Asset__c in :assetSet
                      AND Inventory_Header__c = :parentId
                      AND Asset_Status__c <> '丢失借出明细'
                      AND Inventory_Time__c <> null
                      AND Inventory_Deviation__c > 0        // 盘盈
                    GROUP BY Asset_Status__c, Asset__c
                    ];
                statusKList = [
                    SELECT Asset_Status__c status, sum(Inventory_Deviation__c) kDeviaCount
                        , Asset__c assetId
                     FROM Inventory_Detail__c
                    WHERE Asset__c in :assetSet
                      AND Inventory_Header__c = :parentId
                      AND Asset_Status__c <> '丢失借出明细'
                      AND Inventory_Time__c <> null
                      AND Inventory_Deviation__c < 0        // 盘亏
                    GROUP BY Asset_Status__c, Asset__c
                    ];
            }
 
            Map<Id, StatusCount> assetCountMap = new Map<Id, StatusCount>();
            for (AggregateResult ar : statusArList) {
                StatusCount statusCount = new StatusCount(0);
                Integer amount = (Integer.valueOf(ar.get('quantity')) == null) ? 0 : Integer.valueOf(ar.get('quantity'));               // 应盘数
                Integer acAmount = (Integer.valueOf(ar.get('invShipanCount')) == null) ? 0 : Integer.valueOf(ar.get('invShipanCount')); // 实盘数 (没有实盘的话显示空)
                Integer abmount = 0;
                if(isConsum){
                    abmount = (Integer.valueOf(ar.get('abCount')) == null) ? 0 : Integer.valueOf(ar.get('abCount'));               // 应盘数
                }
 
                Id assId = String.valueof(ar.get('assetId'));
                if (assetCountMap.get(assId) != null) {
                    statusCount = assetCountMap.get(assId);
                }
                if (Integer.valueOf(ar.get('invShipanCount')) != null) {
                    statusCount.ifSPNotNull = true;          // 有实盘记录
                }
 
                if (ar.get('status') == '在库') {
                    statusCount.zaikuNum += amount;
                    statusCount.quantity += abmount;
                } else if (ar.get('status') == '维修中') {
                    statusCount.weixiuNum += amount;         // 应盘维修中
                } else if (ar.get('status') == '出借中') {
                    statusCount.chujieNum += amount;         // 应盘出借中
                } else if (ar.get('status') == '丢失') {
                    statusCount.diushiNum += amount;         // 应盘丢失
                } else if (ar.get('status') == '冻结') {
                    statusCount.zaikuNum += amount;          // 盘点在库数='在库'应盘数+'冻结'应盘数
                } else if (ar.get('status') == '已消耗') {
                    statusCount.xiaohaoNum += amount;
                } else if (ar.get('status') == '调拨中') {//20210531 you 1650 应盘调拨中
                    statusCount.diaoboNum += amount;
                }
                if(ar.get('status') != '已消耗'){
                    statusCount.quantity += amount;              // 应盘数量
                    statusCount.shipanNum += acAmount;           // 实盘数量
                }
 
                assetCountMap.put(assId, statusCount);
 
                for (AggregateResult yAggr : statusYList) {
                    Integer yWuchaAmount = (Integer.valueOf(yAggr.get('yDeviaCount')) == null) ? 0 : Integer.valueOf(yAggr.get('yDeviaCount'));  // 盘点误差(盘盈/盘亏)
                    if (String.valueof(ar.get('assetId')) == String.valueof(yAggr.get('assetId'))
                        && String.valueOf(ar.get('status')) == String.valueOf(yAggr.get('status'))) {
                        if (assetCountMap.containsKey(assId)) {
                            statusCount = assetCountMap.get(assId);
                        }
                        statusCount.wuchaNum += yWuchaAmount;         // 盘点误差(盘盈/盘亏)
                        statusCount.panyinNum += yWuchaAmount;        // 实际盘盈
                        assetCountMap.put(assId, statusCount);
                        break;
                    }
                }
                for (AggregateResult kAggr : statusKList) {
                    Integer kWuchaAmount = (Integer.valueOf(kAggr.get('kDeviaCount')) == null) ? 0 : Integer.valueOf(kAggr.get('kDeviaCount'));  // 盘点误差(盘盈/盘亏)
                    if (String.valueof(ar.get('assetId')) == String.valueof(kAggr.get('assetId'))
                        && String.valueOf(ar.get('status')) == String.valueOf(kAggr.get('status'))) {
                        if (assetCountMap.containsKey(assId)) {
                            statusCount = assetCountMap.get(assId);
                        }
                        statusCount.wuchaNum += kWuchaAmount;         // 盘点误差(盘盈/盘亏)
                        statusCount.pankuiNum += -kWuchaAmount;       // 实际盘亏
                        assetCountMap.put(assId, statusCount);
                        break;
                    }
                }
            }
 
            for (Integer i = 0; i < queryList.size(); i++) {
                if(this.isConsum){
                    Consum_Inventory_Detail__c idcQueryObj = (Consum_Inventory_Detail__c) orderMap.get(queryList[i].Id);
                    Id eachId = idcQueryObj.Asset__c;
                    StatusCount eachSCount = assetCountMap.get(eachId);
                    if (eachSCount != null) {
                        idcQueryObj.Inventory_Zaiku_Count_Jia__c = eachSCount.zaikuNum;
                        // 实盘数 (没有实盘的话显示空)
                        if (eachSCount.shipanNum == 0 && eachSCount.ifSPNotNull == false) {
                            idcQueryObj.Inventory_Shipan_Count_Jia__c = null;
                        } else {
                            idcQueryObj.Inventory_Shipan_Count_Jia__c = eachSCount.shipanNum;
                        }
                        idcQueryObj.Inventory_Chujie_Count_Jia__c = eachSCount.chujieNum;
                        idcQueryObj.Inventory_Quantity_Jia__c = eachSCount.quantity;
                        idcQueryObj.Inventory_Deviation_Jia__c = eachSCount.wuchaNum;
                        idcQueryObj.Inventory_Profit_Quantity__c = eachSCount.panyinNum;
                        idcQueryObj.Inventory_Loss_Quantity__c = eachSCount.pankuiNum;
                        idcQueryObj.Consumed_Count_Jia__c = eachSCount.xiaohaoNum;
                    }
                    if (idcQueryObj != null) {
                        viewList.add(new WrapperInfo(idcQueryObj, myComponentController));
                        viewList[viewList.size() - 1].lineNo = viewList.size() - 1;     // setChangeFlg用到
                        viewList[viewList.size() - 1].check = true;
                        viewList[viewList.size() - 1].oldCheck = true;
                    }
                }
                else{
                    Inventory_Detail__c idcQueryObj = (Inventory_Detail__c) orderMap.get(queryList[i].Id);
                    Id eachId = idcQueryObj.Asset__c;
                    StatusCount eachSCount = assetCountMap.get(eachId);
                    if (eachSCount != null) {
                        idcQueryObj.Inventory_Zaiku_Count_Jia__c = eachSCount.zaikuNum;
                        // 实盘数 (没有实盘的话显示空)
                        if (eachSCount.shipanNum == 0 && eachSCount.ifSPNotNull == false) {
                            idcQueryObj.Inventory_Shipan_Count_Jia__c = null;
                        } else {
                            idcQueryObj.Inventory_Shipan_Count_Jia__c = eachSCount.shipanNum;
                        }
                        idcQueryObj.Inventory_Weixiu_Count_Jia__c = eachSCount.weixiuNum;
                        idcQueryObj.Inventory_Chujie_Count_Jia__c = eachSCount.chujieNum;
                        idcQueryObj.Inventory_Quantity_Jia__c = eachSCount.quantity;
                        idcQueryObj.Inventory_Deviation_Jia__c = eachSCount.wuchaNum;
                        idcQueryObj.Inventory_Profit_Quantity__c = eachSCount.panyinNum;
                        idcQueryObj.Inventory_Loss_Quantity__c = eachSCount.pankuiNum;
                        idcQueryObj.Inventory_Daibaofei_Diushi_Count_Jia__c = eachSCount.diushiNum;
                        idcQueryObj.Inventory_Diaobo_Count_Jia__c = eachSCount.diaoboNum;//20210531 you 1650
 
                    }
                    if (idcQueryObj != null) {
                        viewList.add(new WrapperInfo(idcQueryObj, myComponentController));
                        viewList[viewList.size() - 1].lineNo = viewList.size() - 1;     // setChangeFlg用到
                        viewList[viewList.size() - 1].check = true;
                        viewList[viewList.size() - 1].oldCheck = true;
                    }
                }
            }
        }
        system.debug('●●●●● setViewList END ' );
    }
 
    /**
     * 保存备注&备注人
     */
    public void save() {
        try{
            if(this.isConsum){
                List<Consum_Inventory_Detail__c> changedAsset = new List<Consum_Inventory_Detail__c>();
                for (WrapperInfo wrapper : this.viewList) {
                    if (wrapper.changeFlg != '0') {
                        changedAsset.add((Consum_Inventory_Detail__c)wrapper.sobj);
                    }
                }
                upsert changedAsset;
            }
            else{
                List<Inventory_Detail__c> changedAsset = new List<Inventory_Detail__c>();
                for (WrapperInfo wrapper : this.viewList) {
                    if (wrapper.changeFlg != '0') {
                        changedAsset.add((Inventory_Detail__c)wrapper.sobj);
                    }
                }
                upsert changedAsset;
            }
        
            if (saveType == '1') {
                searchOpp();
                saveType = '';
            }
        } catch (Exception e) {
            System.debug('Exception caught when save: ' + e.getStackTraceString());
            ApexPages.addMessages(e);
        }
    }
 
    /************************** 检索条件 **************************/
    public String modelNo { get; set; }           // 备品型号
    public String assetNumberKey { get; set; }    // 固定资产号(key)
    public String serialNo { get; set; }          // 机身号
    public String whLocation { get; set; }        // 货架号
    public String bpPankuiYing { get; set; }      // 盘亏/盘盈
    public String bpBodyFushupin { get; set; }    // 主体/附属品
    public String ifInternalAsset { get; set; }   // 是否固定资产
    public String bieChanPinFenLei { get; set; }  // 产品分类(GI/SP)
    public String bieBeiPinFenLei { get; set; }   // 备品分类
    public String bieManageType { get; set; }     // 管理种类
 
    // 盘亏/盘盈 SelectOption
    public List<SelectOption> getbpPankuiYingOps() {
        return new List<SelectOption>{new SelectOption('', '--全部--'), new SelectOption('Panying', '盘盈'), new SelectOption('Pankui', '盘亏')};
    }
    // 主体/附属品 SelectOption
    public List<SelectOption> getbpBodyFushupinOps() {
        return new List<SelectOption>{new SelectOption('', '--全部--'), new SelectOption('Zhuti', '主体'), new SelectOption('Fushupin', '附属品')};
    }
    // 是否固定资产 SelectOption
    public List<SelectOption> getifInternalAssetOps() {
        return new List<SelectOption>{new SelectOption('', '--全部--'), new SelectOption('true', '固定资产'), new SelectOption('false', '非固定资产')};
    }
    // 产品分类 SelectOption
    public List<SelectOption> getbieChanPinFenLeiOps() {
        return FixtureUtil.bieChanPinFenLeiOpsMap.get('GISP');
    }
    // 备品分类 SelectOption
    public List<SelectOption> getbieBeiPinFenLeiOps() {
        List<SelectOption> bpfl = FixtureUtil.bieBeiPinFenLeiOpsMap.get('备品分类');
        if(bpfl.get(3).getLabel() != '检测用备品'){
            bpfl.add(3, new SelectOption('检测用备品','检测用备品'));
        }
        if(bpfl.get(4).getLabel() != '空白'){
            bpfl.add(4, new SelectOption('空白','空白'));
        }
        return bpfl;
    }
    // 管理种类 SelectOption
    public List<SelectOption> getbieManageTypeOps() {
        return new List<SelectOption>{new SelectOption('', '--全部--'), new SelectOption('数量管理', '数量管理'), new SelectOption('个体管理', '个体管理')};
    }
    // 消耗品有效期至 比较符 SelectOption
    public List<SelectOption> getCompareOperatorOps() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('=', '等于'));
        options.add(new SelectOption('>', '大于'));
        options.add(new SelectOption('<', '小于'));
        options.add(new SelectOption('>=', '大于等于'));
        options.add(new SelectOption('<=', '小于等于'));
        return options;
    }
 
    // 检索条件
    private String makeSoql(String keyword) {
        String soql =' WHERE Sync_Asset_Record_Flag__c = true';
        soql += ' AND Inventory_Header__c = \'' + String.escapeSingleQuotes(parentId) + '\'';
        soql += ' AND Internal_asset_location__c = \'' + String.escapeSingleQuotes(parentObj.Internal_asset_location__c) + '\'';
        if (String.isNotBlank(keyword)) {
            String[] vals = keyword.split(' ');
             soql += ' AND (';
             String fmodelno = '';
            for (String v : vals) {
                // 关键字 -- 盘点备注
                // fmodelno += ' Asset_Status__c LIKE \'%' + String.escapeSingleQuotes(v.replaceAll('%', '\\%')) + '%\' ';
                fmodelno += ' Inventory_Remarks__c LIKE \'%' + String.escapeSingleQuotes(v.replaceAll('%', '\\%')) + '%\'';
                fmodelno += 'OR';
            }
            fmodelno = fmodelno.removeEnd('OR');
            soql += fmodelno + ' )';
        }
        // 备品配套明细型号
        if (String.isNotBlank(modelNo)) {
            soql += ' AND Fixture_Model_No__c = \'' + String.escapeSingleQuotes(modelNo) + '\'';
        }
        // 固定资产号(key)
        if (String.isNotBlank(assetNumberKey)) {
            soql += ' AND Internal_Asset_number__c = \'' + String.escapeSingleQuotes(assetNumberKey) + '\'';
        }
        // 机身号
        if (String.isNotBlank(serialNo)) {
            soql += ' AND SerialNumber__c = \'' + String.escapeSingleQuotes(serialNo) + '\'';
        }
        // 备品状态
        if (String.isNotBlank(assetStatus)) {
            if (assetStatus == '未盘点') soql += ' AND Asset_Inventory_Flg__c = false ';
            if (assetStatus == '已盘点') soql += ' AND Asset_Inventory_Flg__c = true ';
        }
        // 货架号
        if (String.isNotBlank(whLocation)) {
            soql += ' AND WH_location__c = \'' + String.escapeSingleQuotes(whLocation) + '\'';
        }
        // 盘亏/盘盈
        if (bpPankuiYing == 'Panying') {
            soql += ' AND Sync_Asset_Profit_Quantity__c > 0 ';
        } else if (bpPankuiYing == 'Pankui') {
            soql += ' AND Sync_Asset_Frozen_Quantity__c > 0 ';
        }
        // 主体/附属品
        if (bpBodyFushupin == 'Zhuti') {
            soql += ' AND Main_OneToOne__c = true ';
        } else if (bpBodyFushupin == 'Fushupin') {
            soql += ' AND Loaner_accsessary__c = true ';
        }
        // 是否固定资产
        if (ifInternalAsset == 'true') {
            soql += ' AND Internal_Asset_number__c <> null ';
        } else if (ifInternalAsset == 'false') {
            soql += ' AND Internal_Asset_number__c = null ';
        }
        // 产品分类(GI/SP)
        if (String.isNotBlank(bieChanPinFenLei)) {
            soql += ' AND Product_category__c = \'' + String.escapeSingleQuotes(bieChanPinFenLei) + '\'';
        }
        // 备品分类
        if (String.isNotBlank(bieBeiPinFenLei)) {
            if((bieBeiPinFenLei) == '空白'){
                soql += ' AND Equipment_Type__c = null';
            }
            else {
                soql += ' AND Equipment_Type__c = \'' + String.escapeSingleQuotes(bieBeiPinFenLei) + '\'';
            }
        }
        // 管理种类
        if (String.isNotBlank(bieManageType)) {
            soql += ' AND Manage_type__c = \'' + String.escapeSingleQuotes(bieManageType) + '\'';
        }
        // 消耗品有效期
        if (this.isConsum && String.isNotBlank(String.valueOf(sampleCiDetail.Consumable_Guaranteen_end__c))) {
            soql += ' AND Consumable_Guaranteen_end__c ' + compareOperator + String.escapeSingleQuotes(String.valueOf(sampleCiDetail.Consumable_Guaranteen_end__c));
        }
 
        return soql;
    }
 
    public class StatusCount {
        public Integer zaikuNum;   // 应盘在库+应盘冻结
        public Integer shipanNum;  // 实盘
        public Integer weixiuNum;  // 实盘维修
        public Integer chujieNum;  // 实盘出借
        public Integer diushiNum;  // 实盘丢失
        public Integer xiaohaoNum;  // 实盘消耗
        public Integer quantity;   // 应盘数量
        public Integer wuchaNum;   // 盘点误差(盘盈/盘亏)
        public Integer panyinNum;  // 盘点误差(盘盈)
        public Integer pankuiNum;  // 盘点误差(盘亏)
        public Boolean ifSPNotNull;// 是否实盘过
        public Integer diaoboNum;  // 实盘调拨 20210531 you 1650
 
        public StatusCount(Integer initData) {
            this.zaikuNum = initData;
            this.shipanNum = initData;
            this.weixiuNum = initData;
            this.chujieNum = initData;
            this.diushiNum = initData;
            this.xiaohaoNum = initData;
            this.quantity = initData;
            this.wuchaNum = initData;
            this.panyinNum = initData;
            this.pankuiNum = initData;
            this.ifSPNotNull = false;
            this.diaoboNum = initData;// 20210531 you 1650
        }
    }
 
}