高章伟
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
public without sharing class GuaranteePeriodAlterationController {
 
    /*****************検索用******************/
    public String text1 { get; set; } // 对象
    public String val1 { get; set; }  // 值
    public String Statu_AchievementsId  { get; set; }
    public GuaranteePeriodAlterationApplication__c ambc { get; set; }
    public static List<SelectOption> textOpts { get; private set; }
    public static List<SelectOption> changeReasonOpts { get; private set; }
 
    public Map<String,String> assetIdMap;
    static {
        textOpts = new List<SelectOption>();
        textOpts.add(new SelectOption('','-无-'));
        //textOpts.add(new SelectOption('Order_No__c'  , Schema.SObjectType.Asset.fields.Order_No__c.label));
        textOpts.add(new SelectOption('Name'                , Schema.SObjectType.Asset.fields.Name.label));
        textOpts.add(new SelectOption('SerialNumber'        , Schema.SObjectType.Asset.fields.SerialNumber.label));
        //textOpts.add(new SelectOption('Installation_Site__c', Schema.SObjectType.Asset.fields.Installation_Site__c.label));
        //textOpts.add(new SelectOption('Department_Name__c'  , Schema.SObjectType.Asset.fields.Department_Name__c.label));
    
        changeReasonOpts = new List<SelectOption>();
        changeReasonOpts.add(new SelectOption(''  , '-无-'));
        changeReasonOpts.add(new SelectOption('集中采购,未分配到医院'  , '集中采购,未分配到医院'));
        changeReasonOpts.add(new SelectOption('其他'  , '其他'));
        
    }
   
    /*****************画面初始化用********************************/
    
    public Boolean editAble {get;set;}
    public Boolean spoLinkFlag {get;set;}
    public String statusEdit {get;set;}
    /*****************画面表示Bean******************/
    
    private List<GuaranteePeriodAlterationDetailInfo> guaranteePeriodAlterationDetailRecords = new List<GuaranteePeriodAlterationDetailInfo>();
    public List<GuaranteePeriodAlterationDetailInfo> guaranteePeriodAlterationDetaiRecordsview { get; set; }
    public Integer ConsumableorderdetailsCount { 
        get {
            return guaranteePeriodAlterationDetailRecords == null ? 0 : guaranteePeriodAlterationDetailRecords.size();
        }
    }
    public Integer ConsumableorderdetailsviewCount { 
        get {
            return guaranteePeriodAlterationDetaiRecordsview == null ? 0 : guaranteePeriodAlterationDetaiRecordsview.size();
        }
    }
    //变更单Id
    public String Id  { get; set; }
    public Boolean isShow { get; set; }
    public List<Attachment> attachments { get; set; }
 
    //分页功能
    public Integer pageLimit{get;set;}
    public String soql {get;set;}
    
    public GuaranteePeriodAlterationController(ApexPages.StandardController stdController) {
        isShow = false;
        Statu_AchievementsId = ApexPages.currentPage().getParameters().get('Statu_AchievementsId');
        Id = ApexPages.currentPage().getParameters().get('Id');
 
        statusEdit = ApexPages.currentPage().getParameters().get('KeyWords');
        guaranteePeriodAlterationDetailRecords = new List<GuaranteePeriodAlterationDetailInfo>();
        guaranteePeriodAlterationDetaiRecordsview = new List<GuaranteePeriodAlterationDetailInfo>();
        editAble = false;
        spoLinkFlag = false;
        pageLimit = Integer.valueOf(System.Label.orderdetPageLimitsize);
 
        attachments = new List<Attachment>();
    }
 
    // 画面初始化
    public void init() {
        
        if(Id!=null&&Id!=''&&statusEdit==''&&statusEdit==null){
            editAble = false;
        }else if((Id == null||Id=='')&&(statusEdit==''||statusEdit==null)){
            editAble = true;
        }else if(Id != null&&Id!=''&&statusEdit!=''&&statusEdit!=null){
            editAble = true;
        }
        ambc = new GuaranteePeriodAlterationApplication__c();
        guaranteePeriodAlterationDetailRecords = new List<GuaranteePeriodAlterationDetailInfo>();
        List<Asset> assetListed = new List<Asset>();
        Map<String,GuaranteePeriodAlterationDetailInfo> MidMap = new Map<String,GuaranteePeriodAlterationDetailInfo>();
        
        if(Id== NULL || Id==''){
            List<Statu_Achievements__c> statuAchievements = [select Id,InstallDate__c from Statu_Achievements__c where Id = :Statu_AchievementsId]; 
            ambc.WarrantyStartDate__c = statuAchievements[0].InstallDate__c;
            soql = this.makeSoql1('','');
            assetListed = Database.query(soql);
 
            for (Integer i = 0; i < assetListed.size(); i++) {
                guaranteePeriodAlterationDetailRecords.add(new GuaranteePeriodAlterationDetailInfo(assetListed[i]));
                
                MidMap.put(assetListed[i].Id, new GuaranteePeriodAlterationDetailInfo(assetListed[i]));
            }
 
            for(Integer i = 0; i < guaranteePeriodAlterationDetailRecords.size(); i++){
                guaranteePeriodAlterationDetaiRecordsview.add(guaranteePeriodAlterationDetailRecords[i]);
            }
        }else{
            List<GuaranteePeriodAlterationDetail__c> changeDetailList = new List<GuaranteePeriodAlterationDetail__c>();
            List<GuaranteePeriodAlterationApplication__c>  assetChangeList  = new List<GuaranteePeriodAlterationApplication__c>();
            
            assetChangeList = [select Id, Name,Change_status__c,
                                    ChangeReason__c,WarrantyStartDate__c,
                                    ChangeReason_text__c,GpaaStatu_Achievements__c
                                from GuaranteePeriodAlterationApplication__c
                                where Id = :Id];
            if(assetChangeList.size()>0){
                ambc = assetChangeList[0];
            }
 
            changeDetailList = [select Id,Name,Asset__c,Asset__r.Name,
                                    Asset__r.Asset_situation__c,
                                    Asset__r.Order_No__c,
                                    Asset__r.SerialNumber,
                                    Asset__r.Account.Name,
                                    Asset__r.Installation_Site__c,
                                    Asset__r.CurrentContract__c,
                                    Asset__r.Status,
                                    Asset__r.guarantee_period__c,
                                    Asset__r.RepairQuantity__c
                                from GuaranteePeriodAlterationDetail__c
                                where GuaranteePeriodAlteration__c = :Id]   ;
            for(Integer i = 0; i < changeDetailList.size(); i++){
                MidMap.put(changeDetailList[i].Id, new GuaranteePeriodAlterationDetailInfo(changeDetailList[i]));
            }
 
            for(GuaranteePeriodAlterationDetailInfo ass:MidMap.values()){
                guaranteePeriodAlterationDetailRecords.add(ass);
                
            }
 
            for(GuaranteePeriodAlterationDetailInfo ass : guaranteePeriodAlterationDetailRecords){
                for(GuaranteePeriodAlterationDetail__c ambcDetail :changeDetailList){
                    if(ass.ast.Id == ambcDetail.Asset__c){
                        ass.check = true;
                        guaranteePeriodAlterationDetaiRecordsview.add(ass);
                    }
                }
                
 
            }
 
            isShow = true;
            attachments = seekAttachment();
        } 
    }
    // 检索
    public void searchConsumableorderdetails() {
        
        Map<String,GuaranteePeriodAlterationDetailInfo> MidMap = new Map<String,GuaranteePeriodAlterationDetailInfo>();
        List<Asset> asListed = new List<Asset>();
        soql = this.makeSoql1(text1,val1);
        asListed = Database.query(soql);
        for (GuaranteePeriodAlterationDetailInfo bss : guaranteePeriodAlterationDetaiRecordsview) {
            if(bss.check == true){
                MidMap.put(bss.ast.Id, bss);
            }
        }
 
        for (Integer i = 0; i < asListed.size(); i++) {
            if(!MidMap.containsKey(asListed[i].Id)){
                MidMap.put(asListed[i].Id, new GuaranteePeriodAlterationDetailInfo(asListed[i]));
            }
            
        }
 
 
        guaranteePeriodAlterationDetaiRecordsview.clear();
        for(GuaranteePeriodAlterationDetailInfo bss : MidMap.values()){
            guaranteePeriodAlterationDetaiRecordsview.add(bss);
        }
    }
 
    private String makeSoql1(String text1,String val1){
        String statu_AchievementId;
        if(String.isNotBlank(val1)){
            val1 = val1.trim();
        }
        if (String.isNotBlank(Statu_AchievementsId)) {
            statu_AchievementId = Statu_AchievementsId;
        }  else {
            statu_AchievementId = ambc.GpaaStatu_Achievements__c;
        }
        String soql = 'select Id,name,SerialNumber,Asset_situation__c,Order_No__c,Account.Name,Status,Installation_Site__c,CurrentContract__c,guarantee_period__c,RepairQuantity__c from Asset where Backorder__c = \'' + statu_AchievementId +'\'';
        soql += ' AND AssetMark__c != \'耗材\'';
        if(!String.isBlank(text1)){
 
                soql += ' AND ('+ text1 +' like \'%' + String.escapeSingleQuotes(val1.replaceAll('%', '\\%')) + '%\' )';
        }
        soql += ' limit 200';
        return soql;
    }
 
    // 编辑按钮
    public PageReference setEditAble(){
        statusEdit = 'Redirect';
        PageReference ref = new Pagereference('/apex/AssetGuaranteePeriodAlteration?Id='+Id+'&KeyWords='+statusEdit);
        ref.setRedirect(true);
        return ref;
    }
 
    // 再申请
    public PageReference backOrder(){
        statusEdit = 'Redirect';
        PageReference ref = new Pagereference('/apex/AssetGuaranteePeriodAlteration?Id='+Id+'&KeyWords='+statusEdit);
        ref.setRedirect(true);
        return ref;
    }
 
    //删除按钮
    public PageReference DelConsumable(){
        GuaranteePeriodAlterationApplication__c ambc = new GuaranteePeriodAlterationApplication__c(Id = Id);
        List<GuaranteePeriodAlterationDetail__c> changeDetailList = [Select Id,GuaranteePeriodAlteration__r.GpaaStatu_Achievements__c 
                                                                        from GuaranteePeriodAlterationDetail__c 
                                                                    where GuaranteePeriodAlteration__c = :Id]; 
        String rawAccountIded = ''; 
        if(changeDetailList.size()> 0){
            rawAccountIded = changeDetailList[0].GuaranteePeriodAlteration__r.GpaaStatu_Achievements__c;
        }
        
        try {
            delete changeDetailList;
            delete ambc;
            //return null;
            return returnRawAccountPage(rawAccountIded);
        } catch (Exception e) {
            ApexPages.addMessages(e);
            return null;
        }   
        //return null;
    }
 
    // 删除“变更申请单”时,跳转到“注残”页面
    public PageReference returnRawAccountPage(String rawAccountIded){
        PageReference ref = new Pagereference('/'+rawAccountIded );
        ref.setRedirect(true);
        return ref;
    }
    
    //查找当前"保修期限变更申请"所有的附件
    public List<Attachment> seekAttachment() {
        return [select Id, ParentId, Name, OwnerId, CreatedDate, BodyLength FROM Attachment where ParentId = :Id];
    }
 
    //跳转到上传附件页面
    public PageReference uploadingAttachment(){
        PageReference ref = new Pagereference('/p/attach/NoteAttach?pid=' + Id +'&retURL=%2Fapex/AssetGuaranteePeriodAlteration?Id='+Id);
        ref.setRedirect(true);
        return ref;
    }
 
    //查找"保有设备"是否含有"QIS"和"修理"和"保有设备的保修期限变更过"
    public Boolean find_QIS_Repair(List<String> assetIdList) {
 
        assetIdMap = new Map<String,String>();
        List<Repair__c> repairList = [select Id,Delivered_Product__c,Delivered_Product__r.SerialNumber from Repair__c where Delivered_Product__c In :assetIdList];
        List<QIS_Report__c> qIS_ReportList = [select Id,nonyushohin__c,nonyushohin__r.SerialNumber from QIS_Report__c where nonyushohin__c In :assetIdList];
        List<Asset> assets = [Select Id,Name,WarrantyChanges__c,SerialNumber from Asset where WarrantyChanges__c = true And Id In:assetIdList];
        if (repairList.size() > 0 || qIS_ReportList.size() > 0 || assets.size() > 0) {
 
            if (repairList.size() > 0) {
                
                for (Repair__c repair :repairList) {
                    assetIdMap.put(repair.Delivered_Product__c,repair.Delivered_Product__r.SerialNumber+'(含修理)');
                }
            }
 
            if ( qIS_ReportList.size() > 0) {
 
                for (QIS_Report__c qIS_Report :qIS_ReportList) {
                    assetIdMap.put(qIS_Report.nonyushohin__c,qIS_Report.nonyushohin__r.SerialNumber+'(含QIS)');
                }
            }
 
            if (assets.size() > 0) {
 
                for (Asset ass: assets) {
                    assetIdMap.put(ass.Id,ass.SerialNumber+'(变更过)');
                }
            }
 
            return true;
        } 
        return false;
    }
 
     // 提交按钮
    public PageReference Sorder() {
        PageReference ref = new Pagereference('/'+Id);
        ref.setRedirect(true);
 
        Savepoint sp = Database.setSavepoint();
        GuaranteePeriodAlterationApplication__c  agAlteration = new GuaranteePeriodAlterationApplication__c();
        agAlteration.Id = Id;
        attachments = seekAttachment();
        if (ambc.ChangeReason__c == '集中采购,未分配到医院' && attachments.size() == 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请上传标书')); 
            return null;
        }  
 
        try{
 
            update agAlteration;
 
        }catch(Exception e){
            ApexPages.addmessages(e);
        }
        try {
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setObjectId(Id);
            Approval.ProcessResult submitResult = Approval.process(psr);
            return  ref; 
        }
        catch (Exception e) {
            //ApexPages.addmessages(e);
            Database.rollback(sp);
            return  null; 
        }
 
        //return null;
        //return  UnabletoEdit(); 
    }
 
    //验证字段
    public Boolean warning() {
 
        if (String.isBlank(ambc.ChangeReason__c)) {
            
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请输入变更原因')); 
            return true;
        } else if (ambc.ChangeReason__c == '其他' && String.isBlank(ambc.ChangeReason_text__c)) {
            
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '变更原因为"其他"时,请输入"变更原因其他"')); 
            return true;
        } else if (ambc.WarrantyStartDate__c == null) {
            
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请输入保修开始日')); 
            return true;
        }
 
        Integer FLG = 0;
        Integer Count = 0;
        for (GuaranteePeriodAlterationDetailInfo CheckCount : guaranteePeriodAlterationDetaiRecordsview) {
            FLG = FLG + 1;
            if(CheckCount.check == false){
                Count = Count + 1;
            }
        }
 
        if (Count == FLG) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择要修改的保有设备')); 
            return true;
        }
 
        return false;
 
    }
 
    // 保存按钮
    public PageReference save() {
        //return null;
        
        GuaranteePeriodAlterationApplication__c  agAlteration = new GuaranteePeriodAlterationApplication__c ();
        List<GuaranteePeriodAlterationDetail__c> Ins = New List<GuaranteePeriodAlterationDetail__c>();
        
        //新建变更单时
        Savepoint sp = Database.setSavepoint();
        try {
 
            agAlteration.ChangeReason_text__c = ambc.ChangeReason_text__c;
            agAlteration.WarrantyStartDate__c = ambc.WarrantyStartDate__c;
            agAlteration.ChangeReason__c = ambc.ChangeReason__c;
            
            if (warning()) {
 
                return null;
            } 
 
            if (String.isBlank(Id)) {
 
                Integer i = 1;
                agAlteration.Change_status__c = '草案中';
                agAlteration.GpaaStatu_Achievements__c = Statu_AchievementsId;
 
                try {
 
                    insert agAlteration;
                } catch (Exception e) {
 
                    ApexPages.addMessages(e);
                }
                             
 
                Map<String,String> assetGuaranteeTimeMap = new Map<String,String>();
                List<GuaranteePeriodAlterationApplication__c> saveGpaApplicationList = 
                                            [SELECT Id,Name,WarrantyStartDate__c 
                                            FROM GuaranteePeriodAlterationApplication__c 
                                            WHERE id = :agAlteration.id];
                
                if ( saveGpaApplicationList.size() > 0) {
 
                    List<Asset> assetList = [Select Id,Guarantee_period_for_products__c,guarantee_period__c,RepairQuantity__c,SerialNumber
                                                 from Asset
                                                 where Backorder__c = :Statu_AchievementsId];
                    Map<String,Integer> assetMap = new Map<String,Integer>();                             
                    for (Asset ass :assetList) {
                        assetMap.put(ass.Id,Integer.valueOf(ass.guarantee_period__c));
                    } 
 
 
                    List<String> assetIds = new List<String>();
                    for (GuaranteePeriodAlterationDetailInfo ass : guaranteePeriodAlterationDetaiRecordsview)  {
 
                        if(ass.check == true){
                            assetGuaranteeTimeMap.put(saveGpaApplicationList[0].Id,ass.ast.Id);
                            GuaranteePeriodAlterationDetail__c gpaDetail = new GuaranteePeriodAlterationDetail__c();
                            String str = string.valueOf(i);
                            if(str.length() == 1){
                            str = '0' + str;
                            }
                            gpaDetail.Name = saveGpaApplicationList[0].Name + '-'+ str;
                            gpaDetail.GuaranteePeriodAlteration__c = saveGpaApplicationList[0].Id;
                            gpaDetail.Asset__c = ass.ast.Id;
                            gpaDetail.GuaranteeTime__c  =  saveGpaApplicationList[0].WarrantyStartDate__c.addDays(assetMap.get(ass.ast.Id));                
                            i++;
                            Ins.add(gpaDetail);
                            assetIds.add(ass.ast.Id);
 
                        }
                    }
                    
                    if (find_QIS_Repair(assetIds)) {
 
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '以下保有设备含有"QIS"或"修理"或"保修期限变更过"不适合变更"保修期限"' + assetIdMap.values())); 
                        return null;
                    } 
 
                    //update saveGpaApplicationList;
                }                            
                
 
                Id = agAlteration.id;
 
                if(Ins.size()>0){
                    insert Ins;
                    return  UnabletoEdit();
                }
            }
            //修改之后 保存订单
            else if(!String.isBlank(Id)){   
               
                agAlteration.Id = Id;
                agAlteration.GpaaStatu_Achievements__c = ambc.GpaaStatu_Achievements__c; 
                          
                update agAlteration;
 
                List<GuaranteePeriodAlterationDetail__c> delIns = 
                        [select Id from GuaranteePeriodAlterationDetail__c
                            where GuaranteePeriodAlteration__r.Id = :Id];
                
                if(delIns.size()>0){
 
                    delete delIns;
                }
 
                Integer i = 1;
                List<GuaranteePeriodAlterationApplication__c> saveGpaApplicationList = 
                                [SELECT Id,Name 
                                FROM GuaranteePeriodAlterationApplication__c 
                                WHERE id =:agAlteration.id];
 
                List<String> assetIds = new List<String>();
                for (GuaranteePeriodAlterationDetailInfo ass : guaranteePeriodAlterationDetaiRecordsview)  {
 
                    if(ass.check == true){
 
                        GuaranteePeriodAlterationDetail__c gpaDetail = new GuaranteePeriodAlterationDetail__c();
                        String str = string.valueOf(i);
                        
                        if(str.length() == 1){
                        
                            str = '0' + str;
                        }
 
                        gpaDetail.Name = saveGpaApplicationList[0].Name + '-'+ str;
                        gpaDetail.GuaranteePeriodAlteration__c = saveGpaApplicationList[0].Id;
                        gpaDetail.Asset__c = ass.ast.Id;                      
                        i++;
                        Ins.add(gpaDetail);
                        assetIds.add(ass.ast.Id);
                    }
                }
 
                if (find_QIS_Repair(assetIds)) {
 
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '以下保有设备含有"QIS"或"修理"或"保修期限变更过"不适合变更"保修期限"' + assetIdMap.values())); 
                    return null;
                }
               
                if(Ins.size()>0){
 
                    insert Ins;
                    return  UnabletoEdit();
                }
 
            }
 
        }catch(Exception e){
 
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return  null;
        }
 
        return UnabletoEdit();
    }
 
    // 返回不可编辑状态
    public PageReference UnabletoEdit(){
        PageReference ref = new Pagereference('/apex/AssetGuaranteePeriodAlteration?Id='+Id);
        ref.setRedirect(true);
        return ref;
    }
    // Data Bean
    class GuaranteePeriodAlterationDetailInfo  {
        public Boolean check { get; set; }
        public Boolean oldCheck { get; set; }
        public Boolean canSelect { get; set; }
 
 
        public Asset ast { get; set; }
        public GuaranteePeriodAlterationDetail__c gpad { get; set; }
 
 
 
        public GuaranteePeriodAlterationDetailInfo(GuaranteePeriodAlterationDetail__c gpad) {
            check = false;
            oldCheck = false;
            ast = gpad.Asset__r;
            canSelect = true;
 
        }
 
        public GuaranteePeriodAlterationDetailInfo(Asset e) {
            check = false;
            oldCheck = false;
            ast = e;
            canSelect = true;
        }
        
    }
}