高章伟
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
public with sharing class ConsumTrialUpdateController {
 
    public static final String NONE = system.label.StartTrading_None;
    private List<String> assetFieldApiList;
    private List<String> receivingNoteFieldApiList;
 
    public List<LabelValue> outputFieldList {get; set;}
    public List<LabelValue> inputFieldList {get; set;}
    public Boolean canEdit{get;set;}
    public Boolean isCenter{get;set;}
 
    public String parId {get; set;}
    public PageBean pageB {get; set;}
    public Boolean showSelectRnsButton{get;set;}
    private String case_OR_animal_organLabel{get;set;}
 
    public ConsumTrialUpdateController() {
        parId = System.currentPageReference().getParameters().get('parId');
        canEdit = System.currentPageReference().getParameters().get('canedit') == 'true';
    }
 
    public void init() {
        try {
            if (String.isBlank(parId)) {
                throw new ControllerUtil.myException('请传入正确的参数');
            }
            List<Consum_Apply__c> rnList = [SELECT Id
                        // , DateOfDelivery__c
                        // , ReceivingNoteNo__c
                        , Status__c
                        // , Asset_loaner_category__c
                        , RA_Status__c
                        , HP_received_sign_rich__c
                        , demo_purpose2__c
                     FROM Consum_Apply__c
                    WHERE Id = :parId];
            if (rnList.size() == 0) {
                throw new ControllerUtil.myException('耗材申请Id不存在');
            }
            case_OR_animal_organLabel = '病例';
            if (rnList[0].demo_purpose2__c == '动物实验' ) {
                case_OR_animal_organLabel = '动物脏器';
            }
            Set<String> assetFieldApiSet = new Set<String>();
            Set<String> receivingNoteFieldApiSet = new Set<String>();
            outputFieldList = setFIeldSet('Consum_Apply_Equipment_Set_Detail__c', 'ConsumTrialUpdate_L', receivingNoteFieldApiSet);
            inputFieldList = setFIeldSet('Consum_Apply_Equipment_Set_Detail__c', 'ConsumTrialUpdate_R', receivingNoteFieldApiSet);
            addField(receivingNoteFieldApiSet, assetFieldApiSet);
            assetFieldApiList = new List<String>();
            receivingNoteFieldApiList = new List<String>();
            assetFieldApiList.addAll(assetFieldApiSet);
            receivingNoteFieldApiList.addAll(receivingNoteFieldApiSet);
            setEditable();
            pageB = new PageBean(rnList[0], assetFieldApiList, receivingNoteFieldApiList, isCenter);
        }
        catch (Exception e) {
            ApexPages.addMessages(e);
        }
    }
 
    //public List<SelectOption> getAsreceivedConditionOps() {
    //    return getPlickList('Consum_Apply__c', 'Status__c');
    //}
 
    //public List<SelectOption> getcategoryOps() {
    //    return getPlickList('Consum_Apply__c', 'Asset_loaner_category__c');
    //}
    /**
    @ description 备品中心确认只能让备品中心或管理员修改
    */
    public void setEditable(){
        Id profileId = userinfo.getProfileId();
        Profile pf = [Select Id, Name from Profile where Id=:profileId];
        String pfNo = pf.Name.split('_')[0];
        isCenter = false;
        // OLY_OCM-942
        if (pfNo.contains('2B1')
            || pfNo.contains('2B2')
            || pfNo.contains('2B3')
            || profileId == System.Label.ProfileId_SystemAdmin
        ){
            isCenter = true;
        }
    }
 
    //public void search() {
    //    System.debug(assetFieldApiList);
    //    String wher = '';
    //    if (String.isNotBlank(pageB.rn.Status__c)) {
    //        wher += ' AND RND_Status__c = \'' + String.escapeSingleQuotes(pageB.rn.Status__c) + '\'';
    //    }
 
    //    if (String.isNotBlank(pageB.fixture_Model_No)) {
    //        wher += ' AND Fixture_Model_No_F__c Like \'%' + String.escapeSingleQuotes(pageB.fixture_Model_No) + '%\'';
    //    }
    //    pageB.search(wher, assetFieldApiList, receivingNoteFieldApiList, isCenter);
    //}
 
    public void changeFixModel() {
        if (String.isNotBlank(pageB.rn.HP_received_sign_rich__c)){
            // 上传试用单后不可编辑
            ApexPages.Message message = new ApexPages.Message(ApexPages.severity.ERROR, '试用单已上传,不可以修改明细');
            ApexPages.addMessage(message);
        }
        else{
            pageB.fixMode = pageB.fixMode == false;
        }
    }
 
    private void addField(Set<String> receivingNoteFieldApiSet, Set<String> assetFieldApiSet) {
        receivingNoteFieldApiSet.add('Id');
        receivingNoteFieldApiSet.add('Name');
        receivingNoteFieldApiSet.add('Asset__c');
        receivingNoteFieldApiSet.add('Fixture_Model_No_F__c');
        receivingNoteFieldApiSet.add('Consum_Apply__r.AnimalExerimentFlag__c');
        receivingNoteFieldApiSet.add('Has_Picture__c');
        receivingNoteFieldApiSet.add('Asset_Center_Confirm__c');
        receivingNoteFieldApiSet.add('Consum_Apply__r.HP_received_sign_rich__c');
        receivingNoteFieldApiSet.add('StockDown__c');
        receivingNoteFieldApiSet.add('Picture1__c');
        receivingNoteFieldApiSet.add('Picture2__c');
    }
 
    //private List<SelectOption> getPlickList(String objApi, String fieldApi) {
    //    Schema.DescribeFieldResult fieldResult = Schema.getGlobalDescribe().get(objApi).getDescribe().fields.getMap().get(fieldApi).getDescribe();
    //    List<SelectOption> pickListValuesList= new List<SelectOption>();
    //    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
    //    pickListValuesList.add(new SelectOption('', NONE));
    //    for( Schema.PicklistEntry pickListVal : ple){
    //        pickListValuesList.add(new SelectOption(pickListVal.getValue(), pickListVal.getLabel()));
    //    }
    //    return pickListValuesList;
    //}
 
    private List<LabelValue> setFIeldSet(String objApi, String fieldSetApi, Set<String> fieldApiSet) {
        List<LabelValue> lvList = new List<LabelValue>();
        Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get(objApi).getDescribe().fieldSets.getMap();
        Schema.FieldSet fs = fsMap.get(fieldSetApi);
        List<FieldSetMember> fsmList = fs.getFields();
 
        for (FieldSetMember fsm : fsmList) {
            if (objApi == 'Asset') {
                fieldApiSet.add('Assert__r.' + fsm.getFieldPath());
            }
            else {
                fieldApiSet.add(fsm.getFieldPath());
            }
            if (fsm.getFieldPath() == 'Case_OR_animal_organ__c') {
                lvList.add(new LabelValue(case_OR_animal_organLabel, fsm.getFieldPath()));
            }
            else {
                lvList.add(new LabelValue(fsm.getLabel(), fsm.getFieldPath()));
            }
        }
        return lvList;
    }
 
    // 适用区域信息提示
    //public void showApplyMsg() {
    //    String applyMsg = ApexPages.currentPage().getParameters().get('applyMsg');
    //    String applyMsgType = ApexPages.currentPage().getParameters().get('applyMsgType');
    //    String applyErrorLabel = ApexPages.currentPage().getParameters().get('applyErrorLabel');
    //    if(applyMsgType == 'Fail') {
    //        ApexPages.Message vfApplyMsg = new ApexPages.Message(ApexPages.severity.ERROR, applyMsg);
    //        ApexPages.addMessage(vfApplyMsg);
    //        System.debug('applyMsg===' + applyMsg);
    //        System.debug('applyMsgType===' + applyMsgType);
    //        System.debug('applyErrorLabel===' + applyErrorLabel);
    //        if(applyErrorLabel != null) {
    //            System.debug('applyErrorLabel===' + applyErrorLabel);
    //        }
    //    } else {
    //        ApexPages.Message vfApplyMsg = new ApexPages.Message(ApexPages.severity.INFO, applyMsg);
    //        ApexPages.addMessage(vfApplyMsg);
    //    }
    //}
 
    //public List<SelectOption> getCase_OR_animal_organOps() {
    //    String tex = '病例';
    //    if (pageB.rn.AnimalExerimentFlag__c) {
    //        tex = '动物脏器';
    //    }
    //    List<SelectOption> opList = new List<SelectOption>();
    //    for (Integer i = 0; i < 20; i ++) {
    //        String value = tex + String.valueOf((i + 1)).leftPad(2,'0');
    //        opList.add(new SelectOption(value, value));
    //    }
    //    return opList;
    //}
 
    // 保存画面上的数据
    public PageReference saveRecord() {
 
        try {
            List<Consum_Apply_Equipment_Set_Detail__c> caesdListUpdate = new List<Consum_Apply_Equipment_Set_Detail__c>();
            Set<Id> assetIdSet = new Set<Id>();
 
            // 点保存发生在上传图片之后,所以需要重新查一下当前的图片情况,防止入力规则报错
            // 图片相关字段要比画面中对象要新,Received_Confirm__c和Received_NG_Content__c比画面中对象要旧
            Map<Id, Consum_Apply_Equipment_Set_Detail__c> oldCaesdMap = new Map<Id, Consum_Apply_Equipment_Set_Detail__c>([
                    SELECT Id
                         , Has_Picture__c
                         , Received_Confirm__c
                         , Received_NG_Content__c
                         , Picture1__c
                         , Picture2__c
                         , Asset_Center_Confirm__c
                    FROM Consum_Apply_Equipment_Set_Detail__c
                    WHERE Consum_Apply__c =: parId
                    AND (Cancel_Select__c = false OR NG_Select_Again__c = true)
                ]);
            String ngMessage = '';
            String ngModifyMessage = '';
            String ngRejectMessage = '';
            Boolean sendNGMail = false;
            Boolean sendNGModifyMail = false;
            Boolean sendNGRejectMail = false;
            Boolean ngContentEmpty = false;
            Boolean ngPictureEmpty = false;
 
            for (RowBean rowB : pageB.rowBList) {
                Consum_Apply_Equipment_Set_Detail__c newCaesd = rowB.rnd;
                Consum_Apply_Equipment_Set_Detail__c oldCaesd = oldCaesdMap.get(newCaesd.Id);
 
                if (rowB.checked) {
                    if(newCaesd.Received_Confirm__c == 'NG'){
                        // NG时检查
                        if(String.isBlank(newCaesd.Received_NG_Content__c)){
                            ngContentEmpty = true;
                        }
                        // 由于上传图片在另一个画面中更新,所以要取一下图片上传情况
                        if(!oldCaesdMap.get(newCaesd.Id).Has_Picture__c){
                            ngPictureEmpty  = true;
                        }
                        else {
                            newCaesd.Has_Picture__c = true;
                        }
                        ngMessage += newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n';
                        // 新增的NG明细
                        if(oldCaesd.Received_Confirm__c != 'NG'){
                            sendNGMail = true;
                        }
                        // 理由或图片变化的NG明细
                        else if (oldCaesd.Received_NG_Content__c != newCaesd.Received_NG_Content__c
                                || oldCaesd.Picture1__c != newCaesd.Picture1__c
                                || oldCaesd.Picture2__c != newCaesd.Picture2__c){
                            sendNGModifyMail = true;
                            ngModifyMessage+= newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n';
                            newCaesd.Asset_Center_Confirm__c = null;
                        }
                        // 备品中心确认过的NG明细
                        if(oldCaesd.Asset_Center_Confirm__c != 'OK' && newCaesd.Asset_Center_Confirm__c == 'OK'){
                            newCaesd.Check_lost_Item_Final__c = '消耗';
                            assetIdSet.add(newCaesd.Asset__c);
                        }
 
                        // 备品中心驳回的NG明细
                        if(oldCaesd.Asset_Center_Confirm__c != 'NG' && newCaesd.Asset_Center_Confirm__c == 'NG'){
                            sendNGRejectMail = true;
                            ngRejectMessage += newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n';
                        }
                    }
                    else if(newCaesd.Received_Confirm__c == 'OK'){
                        newCaesd.Received_NG_Content__c = null;
                        newCaesd.Picture1__c = null;
                        newCaesd.Picture2__c = null;
                    }
                    caesdListUpdate.add(newCaesd);
                }
                else{
                    // 没有被更新的NG明细
                    if(oldCaesd.Received_Confirm__c == 'NG') {
                        ngMessage += newCaesd.Fixture_Model_No_F__c + '\t' + newCaesd.Received_NG_Content__c + '\n';
                    }
                }
            }
 
            if(ngContentEmpty){
                throw new ControllerUtil.myException('没有填写【到货NG内容】!');
            }
            if(ngPictureEmpty){
                throw new ControllerUtil.myException('没有上传到货NG图片!');
            }
 
            if (caesdListUpdate.size() > 0) {
                List<Asset> assetList = [
                    SELECT Id
                      FROM Asset
                     WHERE Id IN:assetIdSet
                     FOR UPDATE
                ];
                FixtureUtil.withoutUpdate(caesdListUpdate, true);
                // 发送邮件用
                Consum_Apply__c ca0 = new Consum_Apply__c(Id=parId);
                Consum_Apply__c ca1 = new Consum_Apply__c(Id=parId);
                if(sendNGMail){
                    ca0.Received_NG_Detail_ForEmail__c = null;
                    ca1.Received_NG_Detail_ForEmail__c = ngMessage;
                }
                if(sendNGRejectMail){
                    ca0.Received_NG_Reject_Detail_ForEmail__c = null;
                    ca1.Received_NG_Reject_Detail_ForEmail__c = ngRejectMessage;
                }
                if(sendNGModifyMail){
                    ca0.Received_NG_Content_Modified_ForEmail__c = null;
                    ca1.Received_NG_Content_Modified_ForEmail__c = ngModifyMessage;
                    ca1.Received_NG_Content_Modified_Time__c = System.Now();
                }
                FixtureUtil.withoutUpdate(new List<Consum_Apply__c> {ca0});
                FixtureUtil.withoutUpdate(new List<Consum_Apply__c> {ca1});
                init();
 
                ApexPages.Message message = new ApexPages.Message(ApexPages.severity.INFO, '保存成功');
                ApexPages.addMessage(message);
                pageB.fixMode = false;
                return null;
            }
        }
        catch (Exception e) {
            ApexPages.addMessages(e);
            pageB.fixMode = true;
            return null;
        }
        return null;
    }
 
    public class PageBean {
        /* ------------ 复数编辑用 ------------ */
        public Consum_Apply_Equipment_Set_Detail__c caesdInput {get{
            if (caesdInput == null) {
                caesdInput = [SELECT Id
                         FROM Consum_Apply_Equipment_Set_Detail__c
                        LIMIT 1];
                caesdInput = new Consum_Apply_Equipment_Set_Detail__c(Id = caesdInput.Id);
            }
            return caesdInput;
        } set;}
 
        /* ------------ 检索用 ------------ */
        public Consum_Apply__c rn {get; set;}
        //public String fixture_Model_No {get; set;}
 
        /* ------------  显示用 ------------ */
        //public String spareLocationName {get; set;}
        //public String dateOfDelivery {get; set;}
        //public String orderCode {get; set;}
        //public String approvalNumber {get; set;}
        //public String receivingNoteNo {get; set;}
        public String asreceivedCondition {get; set;}
 
        /* ------------ 明细一覧 ------------ */
        public List<RowBean> rowBList {get; set;}
 
        /* ------------ 记录用 ------------ */
        public Boolean fixMode {get; set;}
        public Integer currentPageRecordCnt {get; set;}
 
        //public PageBean() {
        //    rn = new Consum_Apply__c();
        //    rowBList = new List<RowBean>();
        //    fixMode = false;
        //    currentPageRecordCnt = 0;
        //}
 
        public PageBean(Consum_Apply__c rnObj, List<String> assetFieldApiList, List<String> receivingNoteFieldApiList, Boolean isCenter) {
            rn = rnObj;
            asreceivedCondition = rnObj.RA_Status__c;
            // dateOfDelivery = rnObj.DateOfDelivery__c == null ? '' : rnObj.DateOfDelivery__c.format();
            rowBList = new List<RowBean>();
            fixMode = false;
            search(null, assetFieldApiList, receivingNoteFieldApiList, isCenter);
        }
 
        public void search(String wher, List<String> assetFieldApiList, List<String> receivingNoteFieldApiList, Boolean isCenter) {
            wher = String.isBlank(wher) ? '' : wher;
            System.debug(assetFieldApiList);
            String soql = 'SELECT '
                    + String.join(assetFieldApiList, ',')
                    + (assetFieldApiList.size() > 0 ? ',' : '')
                    + String.join(receivingNoteFieldApiList, ',')
                 + ' FROM Consum_Apply_Equipment_Set_Detail__c '
                + ' WHERE Consum_Apply_Equipment_Set__r.Consum_Apply__c = \'' + rn.Id + '\'' + wher
                  + ' AND (Cancel_Select__c = false OR NG_Select_Again__c = true)'
                + ' ORDER BY Consum_Apply_Equipment_Set__r.Consum_Apply__c, Consum_Apply_Equipment_Set__c, Fixture_Model_No_F__c, NG_Select_Again__c, Degree_Of_Importance__c Limit 501';
            rowBList = new List<RowBean>();
            System.debug(soql);
            List<Consum_Apply_Equipment_Set_Detail__c> rndList = Database.query(soql);
            if (rndList.size() > 500) {
                ApexPages.Message vfApplyMsg = new ApexPages.Message(ApexPages.severity.WARNING, '耗材明细超过500条,只显示前500条耗材明细');
                ApexPages.addMessage(vfApplyMsg);
                rndList.remove(500);
            }
            Map<String, Integer> maxDegree_Of_ImportanceMap = new Map<String, Integer>();
            for (Consum_Apply_Equipment_Set_Detail__c rnd : rndList) {
                if (maxDegree_Of_ImportanceMap.containsKey(rnd.Fixture_Model_No_F__c) == false) {
                    maxDegree_Of_ImportanceMap.put(rnd.Fixture_Model_No_F__c, 0);
                }
                maxDegree_Of_ImportanceMap.put(rnd.Fixture_Model_No_F__c, maxDegree_Of_ImportanceMap.get(rnd.Fixture_Model_No_F__c) + 1);
            }
            Integer i = 0;
            for (Consum_Apply_Equipment_Set_Detail__c rnd : rndList) {
                RowBean rb = new RowBean(rnd, i, isCenter);
                if(rnd.RAESD_Status__c == Consum_ApplyUtil.CaStatusMap.get(Consum_ApplyUtil.CaesdStatus.Yi_Xiao_Hao.ordinal())
                    || String.isNotBlank(rnd.Consum_Apply__r.HP_received_sign_rich__c) ) {
                    // 已消耗时,不可编辑
                    rb.canChange = false;
                }
                rb.maxDegree_Of_Importance = maxDegree_Of_ImportanceMap.get(rb.rnd.Fixture_Model_No_F__c);
                rowBList.add(rb);
                i ++;
            }
            currentPageRecordCnt = rndList.size();
        }
    }
 
    public class RowBean {
        public Boolean checked {get; set;}
        public Boolean canChange {get; set;}
        public Boolean dataConfirmation {get; set;}
        public String canChangeField {get; set;}
        public Integer lineNo {get; set;}
        public Boolean haveAsset {get; set;}
        public Integer maxDegree_Of_Importance {get; set;}
        public Consum_Apply_Equipment_Set_Detail__c rnd {get; set;}
        public Boolean canCenterEdit {get;set;}
 
        // public Asset asse {get; set;}
 
        //public RowBean() {
        //    checked = false;
        //    rnd = new Consum_Apply_Equipment_Set_Detail__c();
        //    // asse = new Asset();
        //    canChange = true;
        //    dataConfirmation = false;
        //    canChangeField = '';
        //}
 
        public RowBean(Consum_Apply_Equipment_Set_Detail__c rnd1, Integer lineNo1, Boolean isCenter) {
            rnd = rnd1;
            // asse = rnd1.RNDAssert__r;
            checked = false;
            canChange = false;
            canCenterEdit = false;
            lineNo = lineNo1 + 1;
            canChangeField = 'Received_Confirm__c, Received_NG_Content__c';
 
            // 未出库的不可编辑
            if(!rnd.StockDown__c){
                canChange = false;
                canCenterEdit = false;
            }
            // 备品中心
            else if(isCenter){
                if(rnd.Received_Confirm__c == 'OK' || String.isBlank(rnd.Received_Confirm__c)
                    || (rnd.Received_Confirm__c == 'NG' && rnd.Asset_Center_Confirm__c == 'OK')) {
                    canChange = false;
                    canCenterEdit = false;
                }
                else{
                    canChange = true;
                    canCenterEdit = true;
                }
            }
            // 申请人
            else{
                canCenterEdit = false;
                if(rnd.Received_Confirm__c == 'OK'){
                    // 为了使OK变NG时可以立即编辑,OK时的只读用js控制
                    canChange = true;
                }
                else if(rnd.Received_Confirm__c == 'NG'){
                    if(rnd.Asset_Center_Confirm__c == 'OK'){
                        canChange = false;
                    }
                    else{
                        canChange = true;
                    }
                }
                else if(rnd.Received_Confirm__c == '默认签收-OK') {
                    if(rnd.Asset_Center_Confirm__c == 'NG'){
                        canChange = true;
                    }
                    else{
                        canChange = false;
                    }
                }
                else {
                    canChange = true;
                }
            }
            haveAsset = String.isNotBlank(rnd1.Asset__c);
        }
    }
 
    public class LabelValue {
        public String label {get; set;}
        public String value {get; set;}
 
        public LabelValue() {
            label = '';
            value = '';
        }
 
        public LabelValue(String l, String v) {
            label = l;
            value = v;
        }
    }
 
}