高章伟
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
/**
 * wangweipeng    2021/08/04
 * 打印点检计划报告书的pdf
 */
public without sharing class PrintInspectupReportPDFController {
    public String loginPerson{get;set;}//当前登录人id
    public String loginPersonName{get;set;}//当前登录人名字
    public String parameter{get;set;}//父页面传过来的参数
    public String produceText {get;set;}//生成类型
    public String assetType{get;set;}//保有设备类型
    public List<String> assetList{get;set;}//保有设备具体内容
    public Boolean isPaging{get;set;}//是否分页
    public Boolean activeOn {get;set;}//洗消信息
    public List<String> inspectupPlanIdList {get;set;}//点检计划id
    public String maintenanceContractId{get;set;}//合同id
    public Integer pageCnt{get;set;}//如果分页,那么需要分几页
 
    //维修合同信息
    public Maintenance_Contract__c maintenanceContract{get;set;}
    //洗消信息
    public Maintenance_Contract__c InspectionReportData{get;set;}
    //产品信息和产品点检信息
    public List<List<DataObject>> listData{get;set;}
    //key
    public List<String> inspectupPlanKey{get;set;}
    //是否展示结果
    public Boolean resultFlag{get;set;}
 
    public PrintInspectupReportPDFController() {
        //获取当前登录人id
        loginPerson = (Userinfo.getUserId()).substring(0,15);
        //获取当前登录人中文名
        loginPersonName = Userinfo.getName();
        //获取参数
        parameter = System.currentPageReference().getParameters().get('parameter');
        maintenanceContractId = '';//合同id
        produceText = '';//生成类型
        inspectupPlanIdList = new List<String>();//点检信息
        listData = new List<List<DataObject>>();
        maintenanceContract = new Maintenance_Contract__c();
        InspectionReportData = new Maintenance_Contract__c();
        inspectupPlanKey = new List<String>();
        assetList = new List<String>();
        assetType = '';
        activeOn = false;
        isPaging = false;
        pageCnt = 0;
    }
    public PageReference init(){
        //获取参数
        getParameterData();
        try{
            //获取合同数据
            String maintenanceContractSQL = makeTextMaintenanceContractSQl();
            List<Maintenance_Contract__c> mcList = new List<Maintenance_Contract__c>();
            mcList = Database.query(maintenanceContractSQL);
            if (mcList.size() > 0) {
                maintenanceContract = mcList[0];
                InspectionReportData = mcList[0];
            }
            //首先点检计划
            String inspectupPlanSQl = makeTextInspectupPlanSQl();
            List<Inspectup_Plan__c> inspectupPlanList = Database.query(inspectupPlanSQl);
            //存放点检技术的id
            List<String> inspectupPlanId = new List<String>();
            if(inspectupPlanList != null && inspectupPlanList.size() > 0){
                for(Integer i = 0;i< inspectupPlanList.size();i++){
                    inspectupPlanId.add(inspectupPlanList[i].Id);
                    inspectupPlanKey.add(inspectupPlanList[i].Chack_Plan_NO__c+'次点检日');
                }
            }
            //获取点检报告书
            List<Inspection_Report__c> inspectionReportList =new List<Inspection_Report__c>();
            List<String> inspectionReportId = new List<String>();
            if(inspectupPlanId.size() > 0){
                String inspectionReportSQl = makeTextInspectionReportSQl(inspectupPlanId);
                inspectionReportList = Database.query(inspectionReportSQl);
                //存放点检报告书id
                if(inspectionReportList != null && inspectionReportList.size() > 0){
                    for(Integer i =0;i< inspectionReportList.size();i++){
                        inspectionReportId.add(inspectionReportList[i].Id);
                    }
                }
            }
            //获取点检报告书明细,包括设备明细
            List<Inspection_Item__c> inspectionItemList = new List<Inspection_Item__c>();
            if(inspectionReportId.size()>0){
                String inspectionItemSQl = makeTestInspectionItemSQl(inspectionReportId);
                inspectionItemList = Database.query(inspectionItemSQl);
            }
                        
            //获取维修合同设备信息
            String maintenanceContractAssetSQl = makeTestMaintenanceContractAsset();
            List<Maintenance_Contract_Asset__c> maintenanceContractAssetList = new List<Maintenance_Contract_Asset__c>();
            maintenanceContractAssetList = Database.query(maintenanceContractAssetSQl);
 
            if(maintenanceContractAssetList != null && maintenanceContractAssetList.size() > 0){
                //分组把保有设备分组
                //由于客户需要有分页功能,所以要首先要进行分组,查询每页展示的是那种类型的数据
                //如果客户没有选择分页,那么就把所有数据放到第一页里面
                List<String> assetNameList = new List<String>();
                //isPaging  是否分页(只有战略科室、科室和装机地点可以分页)  
                //assetList  如果有值,那么证明需要根据里面的数据进行分页
                if(isPaging && assetList.size() > 0){
                    //循环  维修合同/保有设备
                    for(Maintenance_Contract_Asset__c mcac : maintenanceContractAssetList){
                        String grouping = '';
                        //assetType  根据那种类型进行分页
                        if('1'.equals(assetType)){//战略科室分组
                            grouping = mcac.Asset__r.Acc_Department_Class_Name__c;
                        }else if('2'.equals(assetType)){//科室分组
                            grouping = mcac.Asset__r.Department_Name__c;
                        }else if('3'.equals(assetType)){//装机地点分组
                            grouping = mcac.Asset__r.Installation_Site__c;
                        }
                        if(assetNameList.size() > 0){
                            //用于判断在 assetNameList 分组的集合里面是否有重复的
                            boolean assetNameFlag = true;
                            for(String anl : assetNameList){
                                if(anl == grouping){
                                    assetNameFlag = false;
                                }
                            }
                            if(assetNameFlag){
                                if(grouping != ''){
                                    assetNameList.add(grouping);
                                }
                            }
                        }else{
                            if(grouping != ''){
                                assetNameList.add(grouping);
                            }
                        }
                    }
                }
 
                //不需要分页那么循环一回数据
                //如果 assetNameList 有数据,那么证明是需要给保有设备分页
                //如果没有数据,证明不需要分页,所以只循环一回就可以
                boolean falg = true;
                integer indexA = 0;
                integer indexSize = assetNameList.size();//用于还需要循环几次
                while(indexSize > 0 || falg){
                    falg = false;
                    if(indexSize > 0){
                        indexSize--;
                    }
                    //存放每一页的数据
                    List<DataObject> onePageList = new List<DataObject>();
                    //首先循环设备
                    for(Maintenance_Contract_Asset__c mcac : maintenanceContractAssetList){
                        //存放一条数据
                        DataObject dObj = new DataObject(inspectupPlanIdList);
                        //设备名称
                        String assetName = mcac.Asset__r.name;
                        //由于pdf 页面的宽度有限,那么设备名称太长的话会把表格撑大,所以需要截取换行
                        //目前设定名字长度超过 14个字符时,换行
                        if(assetName != null && assetName != ''){
                            if(assetName.length() > 14){
                                assetName = assetName.substring(0,13)+' \n ' + assetName.substring(13,assetName.length());
                            }
                        }
                        //设备编码
                        String assetNumber = mcac.Asset__r.SerialNumber;
                        dObj.deviceName = assetName;
                        dObj.serialNumber = assetNumber;
                        //判断是否需要分页
                        if(isPaging && assetList.size() > 0){
                            if('1'.equals(assetType)){//战略科室分组
                                if(assetNameList.size() <= 0 || assetNameList[indexA] != mcac.Asset__r.Acc_Department_Class_Name__c){
                                    continue;
                                }
                            }else if('2'.equals(assetType)){//科室分组
                                if(assetNameList.size() <= 0 || assetNameList[indexA] != mcac.Asset__r.Department_Name__c){
                                    continue;
                                }
                            }else if('3'.equals(assetType)){//装机地点分组
                                if(assetNameList.size() <= 0 || assetNameList[indexA] != mcac.Asset__r.Installation_Site__c){
                                    continue;
                                }
                            }
                        }
                        //判断是第几个点检,我们设定最多只能显示4次点检计划
                        integer index = 0;
                        //循环点检计划
                        for(Inspectup_Plan__c ipc : inspectupPlanList){
                            index++;
                            //由于一个设备可能在一个点检计划中多次使用,所以我们只需要获取最新的一个就可以
                            //获取到最新的以后,其他的就不需要了
                            boolean facilityFalg = false;
                            //循环点检报告书
                            for(Inspection_Report__c irc:inspectionReportList){
                                //跳出循环,我们已经获取到当前设备当前点检计划的最新的结果和检测日
                                if(facilityFalg){
                                    break;
                                }
                                String inspectupPlan = irc.Inspectup_Plan__c;
                                //判断是否是当前点检计划的报告书吗
                                if(ipc.id == inspectupPlan){
                                    //循环点检报告书明细
                                    for(Inspection_Item__c iic : inspectionItemList){
                                        //判断是否是当前的报告书明细
                                        if(irc.id == iic.Inspection_ReportId__c){
                                            String iicAssetId = iic.AssetId__c;
                                            //设备id
                                            String assetId = mcac.Asset__c;
                                            if(iicAssetId != '' && iicAssetId != null && assetId != null && assetId != ''){
                                                iicAssetId = iicAssetId.substring(0,15);
                                                assetId = assetId.substring(0,15);
                                                if(assetId == iicAssetId){//查看设备是否一样
                                                    facilityFalg = true;
                                                    if(irc.Inspection_Date__c != null){
                                                        if(index == 1){
                                                            dObj.spotCheck1 = String.valueOf(irc.Inspection_Date__c);
                                                        }else if(index == 2){
                                                            dObj.spotCheck2 = String.valueOf(irc.Inspection_Date__c);
                                                        }else if(index == 3){
                                                            dObj.spotCheck3 = String.valueOf(irc.Inspection_Date__c);
                                                        }else if(index == 4){
                                                            dObj.spotCheck4 = String.valueOf(irc.Inspection_Date__c);
                                                        }
                                                    }
                                                    if(resultFlag){//判断是否需要结果值
                                                        if(iic.ItemStatus__c != '' && iic.ItemStatus__c != null){
                                                            if(index == 1){
                                                                dObj.spocCheckResult1 = iic.ItemStatus__c;
                                                            }else if(index == 2){
                                                                dObj.spocCheckResult2 = iic.ItemStatus__c;
                                                            }else if(index == 3){
                                                                dObj.spocCheckResult3 = iic.ItemStatus__c;
                                                            }else if(index == 4){
                                                                dObj.spocCheckResult4 = iic.ItemStatus__c;
                                                            }
                                                        }
                                                    }
                                                    //跳出循环,我们已经获取到当前设备当前点检计划的最新的结果和检测日
                                                    if(facilityFalg){
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        onePageList.add(dObj);
                    }
                    indexA++;
                    listData.add(onePageList);
                }
            }
        }catch(exception e){
            //失败提示
            ApexPages.addMessages(e);
        }
        return null;
    }
 
    //获取参数的值
    //参数时一个字符串,使用 ,, 两个逗号分割
    //第一个参数:   是否空白单据    
    //第二个参数:设备的限制,分为战略科室、科室、装机地点和机身号
    //第三个参数: 如果第二个参数为战略科室、科室和装机地点,那么就需要选择是那些具体的科室或地点
    //第四个参数: 如果第二个参数为战略科室、科室和装机地点  那么是可以分页的
    //第五个参数:是否需要展示  洗消信息条件
    //第六个参数:点检计划
    //第七个参数:维修合同id
    public void getParameterData(){
        if(parameter != null && parameter != ''){
            List<String> parameterList = parameter.split(',,');
            if(parameterList != null && parameterList.size() == 7){
                produceText = parameterList[0];//生成类型
                //控制结果是否展示
                if('1'.equals(produceText)){//空白单据
                    resultFlag = false;
                }else if('2'.equals(produceText)){//合同全部
                    resultFlag = true;
                }else{
                    resultFlag = false;
                }
                //类型
                //设备 1、战略科室 2、装机地点 3和机身号 4
                assetType = parameterList[1];
                if(assetType != '' && assetType != null){
                    //如果为机身号,那么不需要分页和控制设备的科室不需要
                    if(!'4'.equals(assetType)){
                        //科室、战略科室和装机地点的具体内容
                        String assetStr = parameterList[2];
                        if(assetStr != '' && assetStr != null){
                            if(assetStr.indexOf(',') != -1){
                                assetList = assetStr.split(',');
                            }else{
                                assetList.add(assetStr);
                            }
                            pageCnt = assetList.size();
                        }
                        //是否分页
                        String isPagingStr = parameterList[3];
                        if(isPagingStr == 'true'){
                            isPaging = true;
                        }
                    }
                }
                //洗消信息
                String falgStr = parameterList[4];
                if(falgStr == 'true'){
                    activeOn = true;
                }
                //点检计划id
                String inspectupPlan = parameterList[5];
                if(inspectupPlan != null && inspectupPlan != ''){
                    if(inspectupPlan.indexOf(',') != -1){
                        inspectupPlanIdList = inspectupPlan.split(',');
                    }else{
                        inspectupPlanIdList.add(inspectupPlan);
                    }
                }
                maintenanceContractId = parameterList[6];//合同id
            }
        }
    }
 
    //查询合同单信息
    public String makeTextMaintenanceContractSQl(){
        String maintenanceContractSQL = 'SELECT ID,NAME,Hospital__c,Hospital__r.Name,Department__c,Department__r.Name,Maintenance_Contract_No__c '
                                    +' ,CleaningFluid__c,SterilizationMethod__c,Remarks__c,Disinfectant__c,UsedMachine__c,Used_ET__c,Mode__c'
                                    +' FROM Maintenance_Contract__c WHERE id = :maintenanceContractId ';
        return maintenanceContractSQL;
    }
    
    //获取点检计划
    public String makeTextInspectupPlanSQl(){
        String inspectupPlanSQl = 'select id,name,Chack_Plan_NO__c  from Inspectup_Plan__c  where Maintenance_Contract__c = :maintenanceContractId  and id in :inspectupPlanIdList ';
        return inspectupPlanSQl;
    }
    //获取点检报告书
    //根据检测日时从大到小排序,如果检测日相同,再根据申请日排序
    public String makeTextInspectionReportSQl(List<String> inspectupPlanId){
        String inspectionReportSQl = 'SELECT ID,NAME,Contract__c,Inspectup_Plan__c,Others__c,Contract__r.name,Inspection_Date__c,CleaningFluid__c,Mode__c,SterilizationMethod__c,Remarks__c,Disinfectant__c,UsedMachine__c,Used_ET__c '
                                + ' from Inspection_Report__c where Inspectup_Plan__c in :inspectupPlanId and Status__c = \'批准\' '
                                + ' order by Inspection_Date__c desc,Submit_date__c desc';
        return inspectionReportSQl;
    }
 
    //获取点检报告书明细
    public String makeTestInspectionItemSQl(List<String> inspectionReportId){
        String inspectionItemSQl = 'select id,name,ItemStatus__c,AssetId__c,AssetId__r.name,SerialNumber__c,Inspection_ReportId__c from Inspection_Item__c '
                                + ' where Inspection_ReportId__c in :inspectionReportId ';
        return inspectionItemSQl;
    }
 
    //获取点检的设备
    //Check_object__c = true and
    public String makeTestMaintenanceContractAsset(){
        String maintenanceContractAssetSQl = 'select id,name,Asset__c,Asset__r.name,Asset__r.SerialNumber,Asset__r.Strategic_department_Class_Name__c,Asset__r.Acc_Department_Class_Name__c,Asset__r.Department_Name__c,Asset__r.Installation_Site__c '
                                        +' from Maintenance_Contract_Asset__c '
                                        +' where Maintenance_Contract__c = :maintenanceContractId and isdianjian__c != 0 ';
        //保有设备的筛选条件
        if(assetType != '' && assetType != null){
            //战略科室
            if('1'.equals(assetType)){
                if(assetList != null && assetList.size() > 0){
                    maintenanceContractAssetSQl += ' and Asset__r.Acc_Department_Class_Name__c in :assetList ';
                }
            }else if('2'.equals(assetType)){//科室
                if(assetList != null && assetList.size() > 0){
                    maintenanceContractAssetSQl += ' and Asset__r.Department_Name__c in :assetList ';
                }
            }else if('3'.equals(assetType)){//装机地点
                if(assetList != null && assetList.size() > 0){
                    maintenanceContractAssetSQl += ' and Asset__r.Installation_Site__c  in :assetList ';
                }
            }else if('4'.equals(assetType)){//机身号
 
            }
 
            //是否需要分页
            if(isPaging){
                if('1'.equals(assetType)){//战略科室分页
                    maintenanceContractAssetSQl += ' order by Asset__r.Acc_Department_Class_Name__c desc ';
                }else if('2'.equals(assetType)){//科室分页
                    maintenanceContractAssetSQl += ' order by Asset__r.Department_Name__c desc ';
                }else if('3'.equals(assetType)){//装机地点分页
                    maintenanceContractAssetSQl += ' order by Asset__r.Installation_Site__c desc ';
                }else{
                   maintenanceContractAssetSQl += ' order by Asset__r.Acc_Department_Class_Name__c desc,Asset__r.Department_Name__c desc '; 
                }
            }else{
                maintenanceContractAssetSQl += ' order by Asset__r.Acc_Department_Class_Name__c desc,Asset__r.Department_Name__c desc '; 
            }
        }else{
            maintenanceContractAssetSQl += ' order by Asset__r.Acc_Department_Class_Name__c desc,Asset__r.Department_Name__c desc '; 
        }
        return maintenanceContractAssetSQl;
    }
 
    /**
     * 为了方便前段table获取值
     */
    class DataObject {
        public String deviceName{ get; private set; }//产品名
        public String serialNumber{ get; private set; }//机身号
        public String spotCheck1{ get; private set; }//点检1
        public String spocCheckResult1{get;set;}//结果1
        public String spotCheck2{ get; private set; }//点检2
        public String spocCheckResult2{get;set;}//结果2
        public String spotCheck3{ get; private set; }//点检3
        public String spocCheckResult3{get;set;}//结果3
        public String spotCheck4{ get; private set; }//点检4
        public String spocCheckResult4{get;set;}//结果4
 
        public DataObject(){
            deviceName = ' ';
            serialNumber = ' ';
        }
        //全部初始化一下,用于占位
        public DataObject(List<String> inspectupPlanIdList) {
            deviceName = ' ';
            serialNumber = ' ';
            if(inspectupPlanIdList != null && inspectupPlanIdList.size() > 0){
                //控制初始化几个点检计划
                if(inspectupPlanIdList.size() >= 1){
                    spotCheck1 = ' ';
                    spocCheckResult1 = ' ';
                }
                if(inspectupPlanIdList.size() >= 2){
                    spotCheck2 = ' ';
                    spocCheckResult2 = ' ';
                }
                if(inspectupPlanIdList.size() >= 3){
                    spotCheck3 = ' ';
                    spocCheckResult3 = ' ';
                }
                if(inspectupPlanIdList.size() == 4){
                    spotCheck4 = ' ';
                    spocCheckResult4 = ' ';
                }
            }
        }
    }
}