liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
/**
 * 2023-04-07  
 * 批量选择修理LWC处理
 */
public with sharing class lexBatchSelectRepairLWCController {
 
    public static List<Repair__c> RepairList;
    public static List<RepairData> RAInfoList;
    public static List<String> repairIdList;
 
    //查询Repair数据
    @AuraEnabled
    public static ReturnData search(String json) {
 
        //2023/04/07 LWC
        ReturnData returnData = new ReturnData();
        RetrievalData RevalInfo = (RetrievalData)System.JSON.deserialize(json,RetrievalData.class);
 
        //pageController 逻辑
        RepairList = new List<Repair__c>();
        RAInfoList = new List<RepairData>();
        if(String.isBlank(RevalInfo.Status1) && String.isBlank(RevalInfo.Status2) && String.isBlank(RevalInfo.RepairName) && String.isBlank(RevalInfo.SAPRepairNo) 
            && String.isBlank(RevalInfo.SerialNumber) && String.isBlank(RevalInfo.repair.Incharge_Staff__c) && String.isBlank(RevalInfo.onSiteRepair)
            && String.isBlank(String.valueOf(RevalInfo.repair.Aware_date__c)) && String.isBlank(String.valueOf(RevalInfo.repair.Aware_date2__c)) && String.isBlank(RevalInfo.workLocationSelect)
            && String.isBlank(RevalInfo.State_Hospital)){
            return null;
        }
        // 2023/04/10 add 型号,修理委托者 名称
        String sql = 'select Id, Name, SAP_Service_Repair_No__c, Status1__c, Status2__c, Delivered_Product__c, SerialNumber__c, Delivered_Product__r.Name,Incharge_Staff__r.Name,';
        sql += 'HP_Name__c, State_Hospital__c, Incharge_Staff__c, FSE_ApplyForRepair_Day__c, work_location_select__c, On_site_repair__c,';
        sql += 'Number_of_EffectiveContract__c, NewProductGuaranteeObject__c from Repair__c where Id != null ';
        if(String.isNotBlank(RevalInfo.Status2)){
            sql += ' and Status2__c = \'' + RevalInfo.Status2 + '\'';
        }
        if(String.isNotBlank(RevalInfo.Status1)){
            sql += ' and Status1__c = \'' + RevalInfo.Status1 + '\'';
        }
        if(String.isNotBlank(RevalInfo.HospitalName)){
 
            sql += ' and HP_Name__c like \'%' + RevalInfo.HospitalName.trim() + '%\'';
        }
        if(String.isNotBlank(RevalInfo.RepairName)){
            String splitStr = null;
            if(RevalInfo.RepairName.contains(' ')){
                splitStr = ' ';
            }else if(RevalInfo.RepairName.contains(',')){
                splitStr = ',';
            }
            if(splitStr != null && RevalInfo.RepairName.contains(splitStr)){
                sql += ' and (';
                list <String> nameList = RevalInfo.RepairName.split(splitStr);
                for(String name: nameList){
                   sql += 'Name like \'%' + name + '%\' or ';
                }
                sql = sql.removeEnd('or ');
                sql += ')';
            }else{
                sql += ' and Name like \'%' + RevalInfo.RepairName.trim() + '%\'';
            }
        }
        if(String.isNotBlank(RevalInfo.SAPRepairNo)){
            String splitStr = null;
            if(RevalInfo.SAPRepairNo.contains(' ')){
                splitStr = ' ';
            }else if(RevalInfo.SAPRepairNo.contains(',')){
                splitStr = ',';
            }
            if(splitStr != null && RevalInfo.SAPRepairNo.contains(splitStr)){
                sql += ' and (';
                
                list <String> noList = RevalInfo.SAPRepairNo.split(splitStr);
                for(String no: noList){
                   sql += 'SAP_Service_Repair_No__c like \'%' + no + '%\' or ';
                }
                sql = sql.removeEnd('or ');
                sql += ')';
            }else{
                sql += ' and SAP_Service_Repair_No__c like \'%' + RevalInfo.SAPRepairNo.trim() + '%\'';
            }
        }
        if(String.isNotBlank(RevalInfo.SerialNumber)){
            List<String> SerialNumberList = new List<String>();
            if(RevalInfo.SerialNumber.indexOf(',') != -1){
                SerialNumberList = RevalInfo.SerialNumber.split(',');
            }
            // 支持模糊查询多个,文本框输入用英文半角逗号分隔
            if(SerialNumberList.size() > 0){
                sql += ' and (SerialNumber__c like \'%';
                Integer i = 0;
                for(String SerialNumber : SerialNumberList){
                    if(i == 0){
                        sql += SerialNumber.trim() + '%\'';
                    }else{
                        sql += 'or SerialNumber__c like \'%' + SerialNumber.trim() + '%\'';
                    }
                    i++;
                }
                sql += ')';
            } else{
                sql += ' and SerialNumber__c like \'%' + RevalInfo.SerialNumber.trim() + '%\'';
            }
        }
        if(String.isNotBlank(RevalInfo.repair.Incharge_Staff__c)){
            sql += ' and Incharge_Staff__c = \'' + RevalInfo.repair.Incharge_Staff__c + '\'';
        }
        if(String.isNotBlank(String.valueof(RevalInfo.repair.Aware_date__c))&&String.isNotBlank(String.valueof(RevalInfo.repair.Aware_date2__c))){
            String day = String.valueof(RevalInfo.repair.Aware_date__c);
            String day2 = String.valueof(RevalInfo.repair.Aware_date2__c);
            day = day.substring(0,10);
            day2 = day2.substring(0,10);
            sql += ' and FSE_ApplyForRepair_Day__c >= ' + day + 'and FSE_ApplyForRepair_Day__c <=' +day2;
        }
        if(String.isNotBlank(RevalInfo.onSiteRepair)){
            sql += ' and On_site_repair__c = \'' + RevalInfo.onSiteRepair + '\'';
        }
        if(String.isNotBlank(RevalInfo.workLocationSelect)){
            List<String> workLocationSelectList = new List<String>();
            if(RevalInfo.workLocationSelect.indexOf(',') != -1){
                workLocationSelectList = RevalInfo.workLocationSelect.split(',');
            }
            if(workLocationSelectList.size() > 0){
                sql += ' and (work_location_select__c like \'%';
                Integer i = 0;
                for(String workLocationSelect : workLocationSelectList){
                    if(i == 0){
                        sql += workLocationSelect.trim() + '%\'';
                    }else{
                        sql += 'or work_location_select__c like \'%' + workLocationSelect.trim() + '%\'';
                    }
                    i++;
                }
                sql += ')';
            } else{
                sql += ' and work_location_select__c like \'%' + RevalInfo.workLocationSelect.trim() + '%\'';
            }
        }
        if(String.isNotBlank(RevalInfo.State_Hospital)){
            sql += ' and State_Hospital__c like \'%' + RevalInfo.State_Hospital.trim() + '%\'';
        }
        sql += 'and Status1__c not in(\'0.删除\',\'0.取消\') limit 200';
        if(String.isNotBlank(sql)){
            RepairList = Database.query(sql);
        }
        if(RepairList.size() == 200){
            //2023/04/07 returnData
            returnData.status = '检索数据太多,请缩小检索范围!';
        }
        if(RepairList.size() <= 0){
            //2023/04/07 returnData
            returnData.status = '没检索到任何修理';
            return returnData;
        }
        for(Repair__c ra : RepairList){
            RepairData raData = new RepairData(ra);
            RAInfoList.add(raData);
        }
 
        returnData.repairData = RAInfoList;
        return returnData;
    }
 
    //打印PDF  
    @AuraEnabled
    public static String showPDF(String json) {
        List<Repair__c> RepairList = (List<Repair__c>)System.JSON.deserialize(json,List<Repair__c>.class);
 
        if(RepairList == null || RepairList.isEmpty()){
            return '请先检索修理';
        }
        repairIdList = new List<String>();
        for(Repair__c rd : RepairList) {
                repairIdList.add(rd.Id);
        }
        String url = '=';
        if(repairIdList.size() > 0){
            if(repairIdList.size() <= 50){
                if(RepairList.size() > 0&&!System.Test.isRunningTest()){
                    //20231130 李文涛  批量打印PDF超时
                    // generateAttachment(RepairList);
                    Database.executeBatch(new SelectRepairPDFDeleteBatch(RepairList),1);
                }
                for(String Id : repairIdList){
                    url += Id + '=';
                }
                url = url.substring(0, url.lastIndexOf('='));
                String pdfURL = '';
                if(isSandbox()){
                    // 替换为自定义标签
                    pdfURL = System.Label.lexBatchSelectRepairLWCController_showPDF_IsSandbox + url;
                } else{
                    pdfURL = System.Label.lexBatchSelectRepairLWCController_showPDF_NotSandbox + url;
                }
                return pdfURL;
            } else{
                return '多单打印最大数量为50,请选择少于50个修理!';
            }
        } else{
            return '请至少选择一个修理';
        }
    }
 
 
    // 生成pdf添加到对应的修理中
    public static PageReference generateAttachment(List<Repair__c> repList){
        BatchSelectRepairPDFDelete pdf = new BatchSelectRepairPDFDelete();
        pdf.deletePDF(repList);
        if (!Test.isrunningTest()) {
            pdf.createPDF(repList);
        }
        
        return null;
    }
 
    // 判断是否是测试环境
    public static Boolean isSandbox() {
        return [SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox;
    }
 
    public class RetrievalData {
        @AuraEnabled
        public String Status1;
        @AuraEnabled
        public String Status2;
        @AuraEnabled
        public String RepairName;
        @AuraEnabled
        public String SAPRepairNo;
        @AuraEnabled
        public String SerialNumber;
        @AuraEnabled
        public String onSiteRepair;
        @AuraEnabled
        public String workLocationSelect;
        @AuraEnabled
        public String State_Hospital;
        @AuraEnabled
        public String HospitalName;
        @AuraEnabled
        public Repair__c repair;
 
        public RetrievalData(){
            repair = new Repair__c();
        }
    }
 
    public class RepairData {
        @AuraEnabled
        public Boolean IFCheck;
        @AuraEnabled
        public Repair__c repair;
        public RepairData(Repair__c RepairInfo){
            IFCheck = null;
            repair = RepairInfo;
        }
    }
    //2023/04/07 返回查询结果和状态
    public class ReturnData {
        @AuraEnabled
        public String status;
        @AuraEnabled
        public List<RepairData> repairData;
    }
}