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
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
public with sharing class RepairAgainAnHandler extends Oly_TriggerHandler{
    private Map<Id, RepairAgainAn__c> newMap;
    private Map<Id, RepairAgainAn__c> oldMap;
    private List<RepairAgainAn__c> newList;
    private List<RepairAgainAn__c> oldList;
    public RepairAgainAnHandler() {
        this.newMap = (Map<Id, RepairAgainAn__c>) Trigger.newMap;
        this.oldMap = (Map<Id, RepairAgainAn__c>) Trigger.oldMap;
        this.newList = (List<RepairAgainAn__c>) Trigger.new;
        this.oldList = (List<RepairAgainAn__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
 
        Set<Id> repairSet = new Set<Id>();
 
        for(RepairAgainAn__c ra : newList){
            repairSet.add(ra.Repair__c);
        }
 
        Map<Id,Repair__c> repairMap = new Map<Id,Repair__c>([SELECT Id,Name,SAP_Service_Repair_No__c,(select Id from reparirAgain01__r where RepairAgain_Status__c != '取消') from Repair__c where id in :repairSet]);
        //Map<Id,Repair__c> repairConplateMap = new Map<Id,Repair__c>([SELECT Id,Name,SAP_Service_Repair_No__c,(select Id from reparirAgain01__r) from Repair__c where id in :repairSet]);
 
        for(RepairAgainAn__c ra : newList){
 
            //修理没上传SAP之前,不能新建 再修理分析
            System.debug('=======repairMap' + repairMap);
            System.debug('=======newList' + newList);
 
            //已存在再修理分析的修理不能新建  存在取消的也可以新建再修理
            if( repairMap.containsKey(ra.Repair__c) && repairMap.get(ra.Repair__c).reparirAgain01__r.size() >= 1 ){
                ra.addError('该修理已存在再修理返品分析,不能再新建再修理返品分析');
            }else if(repairMap.containsKey(ra.Repair__c) && repairMap.get(ra.Repair__c).SAP_Service_Repair_No__c == null){
                ra.addError('该修理还没有上传SAP,不能够新建再修理分析');
            }else{
                setPhotoImgText();
                setRepairAgainNameAndReport();
            }
        }
    }
    protected override void beforeUpdate() {
        setPhotoImgText();
        setStatusChange();
        //提交审批之后更新字段
        updateExaminePeople();
        //附件更新
        //updateAttachment();
    }
 
    protected override void afterUpdate(){
        submitRepair();
 
        //发送邮件
        sendIssueEmail();
    }
    
    public static Set<Id> RA_Ids = new Set<Id>();
 
    //再注册分析 需求 新增字段 20220811 sx start
    public static List<Id> openRepairAgainRepairIds = new List<Id>();
    public static List<Id> oSHRepairAgainRepairIds = new List<Id>();
    public static List<Id> oSHWrittenRepairAgainRepairIds = new List<Id>();
    public static List<Id> finalRepairAgainRepairIds = new List<Id>();
    public static List<Id> cleanRepairIds = new List<Id>();
 
    //再注册分析 需求 新增字段 20220811 sx end
    
    
    private void sendIssueEmail(){
        List<Id> repairAgainIds = new List<Id>();
        List<Id> repairAgainIdsSH = new List<Id>();
        List<Id> repairAgainIdsGZ = new List<Id>();
        for(RepairAgainAn__c ra : newList){
            //一次二次通过都发送邮件通知对应的OCSM
            // TODO 发行投诉邮件在workflow,审批通过邮件在审批流,后续业务确认后再统一整合
            if(ra.RepairAgainAn_Approve_status__c != oldMap.get(ra.Id).RepairAgainAn_Approve_status__c && ra.RepairAgainAn_Approve_status__c == '批准'){
                repairAgainIds.add(ra.Id);
            }else if(ra.RepairAgainAn_Approve_status2__c != oldMap.get(ra.Id).RepairAgainAn_Approve_status2__c && ra.RepairAgainAn_Approve_status2__c == '批准'){
                repairAgainIds.add(ra.Id);
            }
 
            //工厂/RC发起再注册申请时,给对应的用户发送邮件提醒
            if(ra.RepairAgain_Status__c == 'RC填写完毕' && oldMap.get(ra.Id).RepairAgain_Status__c != 'RC填写完毕') {
                if (ra.SORC__c == 'OSH-SH') {
                    repairAgainIdsSH.add(ra.Id);
                } else if (ra.SORC__c == 'OSH-GZ') {
                    repairAgainIdsGZ.add(ra.Id);
                }
            }
        }
 
        /*if (!repairAgainIds.isEmpty()) {
            email(repairAgainIds, '再修理分析审批通过', System.Label.RepairAgainAn_Issue_Sendmail);
        }*/
 
        if (!repairAgainIdsSH.isEmpty()) {
            email(repairAgainIdsSH, '工厂/RC发起再注册申请通知', System.Label.SendRepairAgainAn_OSH_SH);
        }
 
        if (!repairAgainIdsGZ.isEmpty()) {
            email(repairAgainIdsGZ, '工厂/RC发起再注册申请通知', System.Label.SendRepairAgainAn_OSH_GZ);
        }
    }
 
    private void email(List<Id> neadUpdateList, String emailTemplateName, String toEmailAddress){
 
        EmailTemplate et=[Select id from EmailTemplate where name =: emailTemplateName limit 1];
 
        String[] toAddresses = toEmailAddress.split(';');
 
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
 
        for(Id repairAgainId: neadUpdateList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail = Messaging.renderStoredEmailTemplate(et.Id, null , repairAgainId);
            mail.setSaveAsActivity(false);
 
            mail.setToAddresses(toAddresses);
            emails.add(mail);
        }
        Messaging.sendEmail(emails);
    }
 
    private void setRepairAgainNameAndReport() {
 
        Date todayTemp = Date.today();
        Set<Id> repairSet = new Set<Id>();
 
        for(RepairAgainAn__c ra : newList){
            repairSet.add(ra.Repair__c);
            // 报告者、报告日自动设值,创建者自动设置
            ra.Reporter__c = UserInfo.getUserId();
            ra.Report_Date__c = todayTemp;
        }
 
        Map<Id,Repair__c> repairMap = new Map<Id,Repair__c>([SELECT Id,Name,Repair_Detail__c,InspectionFailureCause__c,SerialNumber__c,Delivered_Product__c,Incharge_Staff__c,(select Id from reparirAgain01__r) from Repair__c where id in :repairSet]);
 
        System.debug('repairMap========='+repairMap);
        for(RepairAgainAn__c ra : newList){
 
            if(repairMap.containsKey(ra.Repair__c)){
 
                Integer repairSize = repairMap.get(ra.Repair__c).reparirAgain01__r.size() + 1;
                String repairSizeString = '000' + repairSize;
                String cutString = repairSizeString.substring(repairSizeString.length()-3, repairSizeString.length());
 
                if(repairSize == 0) {
                    ra.Name = repairMap.get(ra.Repair__c).Name + '_001';
                }else{
                    ra.Name = repairMap.get(ra.Repair__c).Name + '_' + cutString;
                }
 
                //修理上字段带过来  客户确认 = 修理.问题内容描述  RC确认 = 修理.检测故障内容 机身号码
                ra.RC_Describe__c = repairMap.get(ra.Repair__c).InspectionFailureCause__c;
                ra.Customer_Describe__c = repairMap.get(ra.Repair__c).Repair_Detail__c;
                ra.SerialNumber1__c = repairMap.get(ra.Repair__c).SerialNumber__c;
                ra.Delivered_Product__c = repairMap.get(ra.Repair__c).Delivered_Product__c;
                ra.Incharge_Staff__c = repairMap.get(ra.Repair__c).Incharge_Staff__c;
            }
        }
 
    }
 
    //通过审批流判断确认者和审核人员的值
    private void updateExaminePeople(){
        for(RepairAgainAn__c ram : newList){
            RepairAgainAn__c ramOld = oldMap.get(ram.Id);
            if(ram.OSH_Confirmation_Date__c != ramOld.OSH_Confirmation_Date__c && ram.OSH_Confirmation_Date__c != null){
                //审批提交日修改  OSH确认者 = 操作人员
                ram.OSH_Confirmer__c = UserInfo.getUserId();
            }else if(ram.RepairAgainAn_Approve_Confirm__c != ramOld.RepairAgainAn_Approve_Confirm__c && ram.RepairAgainAn_Approve_Confirm__c != null){
                //OSH审批完了日修改  OSH审核人员 = 操作人员
                ram.OSH_Auditor__c = UserInfo.getUserId();
            }else if(ram.OSH_Confirmation_Date2__c != ramOld.OSH_Confirmation_Date2__c  && ram.OSH_Confirmation_Date2__c != null){
                //审批提交日(二次)修改  OSH确认者(二次) = 操作人员
                ram.OSH_Confirmer2__c = UserInfo.getUserId();
            }else if(ram.RepairAgainAn_Approve_Confirm2__c != ramOld.RepairAgainAn_Approve_Confirm2__c && ram.RepairAgainAn_Approve_Confirm2__c != null){
                //OSH审批完了日(二次)修改  OSH审核人员(二次) = 操作人员
                ram.OSH_Auditor2__c = UserInfo.getUserId();
            }
 
            // OCSM确认者
            if (ram.OCSM_Approve_Status__c == '批准' && ram.OCSM_Approve_Status__c != ramOld.OCSM_Approve_Status__c) {
                ram.OCM_Confirmer__c = UserInfo.getUserId();
            }
        }
    }
 
    private void setPhotoImgText() {
        for (RepairAgainAn__c ra : newList) {
            System.debug('ra.Dectect_Picture1__c2:' + ra.Dectect_Picture1__c);
            // 禁止重复执行
            if (RA_Ids.contains(ra.Id)) continue;
            // 本次报价检测图片1
            if (Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture1__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture1__c != ra.Dectect_Picture1__c)
                    || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture1__c) == false && String.isBlank(ra.Dectect_Picture1_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.Dectect_Picture1__c) == false) {
                    Integer pStt = ra.Dectect_Picture1__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.Dectect_Picture1__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.Dectect_Picture1__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.Dectect_Picture1__c.substring(pStt + 5, pEnd);
                                System.debug('ra.Dectect_Picture1__c1:' + ra.Dectect_Picture1__c);
                                System.debug('phtTxt1:' + phtTxt);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                                System.debug('phtTxt2:' + phtTxt);
                            }
                        }
                    }
                }
                ra.Dectect_Picture1_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // 本次报价检测图片2
            if (Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture2__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture2__c != ra.Dectect_Picture2__c)
                    || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture2__c) == false && String.isBlank(ra.Dectect_Picture2_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.Dectect_Picture2__c) == false) {
                    Integer pStt = ra.Dectect_Picture2__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.Dectect_Picture2__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.Dectect_Picture2__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.Dectect_Picture2__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.Dectect_Picture2_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // 本次报价检测图片3
            if(Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture3__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture3__c != ra.Dectect_Picture3__c)
                    || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture3__c) == false && String.isBlank(ra.Dectect_Picture3_URL__c))){
                ra.Dectect_Picture3_URL__c = setPictureUrl(ra.Dectect_Picture3__c);
                RA_Ids.add(ra.Id);
            }
            // 本次报价检测图片4
            if(Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture4__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture4__c != ra.Dectect_Picture4__c)
                    || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture4__c) == false && String.isBlank(ra.Dectect_Picture4_URL__c))){
                ra.Dectect_Picture4_URL__c = setPictureUrl(ra.Dectect_Picture4__c);
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片1
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture1__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture1__c != ra.OSH_Dectect_Picture1__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture1__c) == false && String.isBlank(ra.OSH_Dectect_Picture1_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture1__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture1__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture1__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture1__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture1__c.substring(pStt + 5, pEnd);
                                System.debug('ra.OSH_Dectect_Picture1__c1:' + ra.OSH_Dectect_Picture1__c);
                                System.debug('phtTxt3:' + phtTxt);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                                System.debug('phtTxt4:' + phtTxt);
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture1_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片2
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture2__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture2__c != ra.OSH_Dectect_Picture2__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture2__c) == false && String.isBlank(ra.OSH_Dectect_Picture2_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture2__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture2__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture2__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture2__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture2__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture2_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片3
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture3__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture3__c != ra.OSH_Dectect_Picture3__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture3__c) == false && String.isBlank(ra.OSH_Dectect_Picture3_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture3__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture3__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture3__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture3__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture3__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture3_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片4
            if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture4__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture4__c != ra.OSH_Dectect_Picture4__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture4__c) == false && String.isBlank(ra.OSH_Dectect_Picture4_URL__c))
            ) {
                String phtTxt = '';
                if (String.isBlank(ra.OSH_Dectect_Picture4__c) == false) {
                    Integer pStt = ra.OSH_Dectect_Picture4__c.indexOf('<img');
                    if (pStt >= 0) {
                        pStt = ra.OSH_Dectect_Picture4__c.indexOf('src="');
                        if (pStt >= 0) {
                            Integer pEnd = ra.OSH_Dectect_Picture4__c.indexOf('"', pStt + 5);
                            if (pEnd >= 0) {
                                phtTxt = ra.OSH_Dectect_Picture4__c.substring(pStt + 5, pEnd);
                                //富文本反转义
                                phtTxt = phtTxt.unescapeHtml4();
                            }
                        }
                    }
                }
                ra.OSH_Dectect_Picture4_URL__c = phtTxt;
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片5
            if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture5__c) == false
                    || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture5__c != ra.OSH_Dectect_Picture5__c)
                    || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture5__c) == false && String.isBlank(ra.OSH_Dectect_Picture5_URL__c))
            ){
                ra.OSH_Dectect_Picture5_URL__c = setPictureUrl(ra.OSH_Dectect_Picture5__c);
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片6
            if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture6__c) == false
                || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture6__c != ra.OSH_Dectect_Picture6__c)
                || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture6__c) == false && String.isBlank(ra.OSH_Dectect_Picture6_URL__c))
            ){
                ra.OSH_Dectect_Picture6_URL__c = setPictureUrl(ra.OSH_Dectect_Picture6__c);
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片7
            if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture7__c) == false
                || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture7__c != ra.OSH_Dectect_Picture7__c)
                || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture7__c) == false && String.isBlank(ra.OSH_Dectect_Picture7_URL__c))
            ){
                ra.OSH_Dectect_Picture7_URL__c = setPictureUrl(ra.OSH_Dectect_Picture7__c);
                RA_Ids.add(ra.Id);
            }
            // OSH技术检测图片8
            if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture8__c) == false
                || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture8__c != ra.OSH_Dectect_Picture8__c)
                || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture8__c) == false && String.isBlank(ra.OSH_Dectect_Picture8_URL__c))
            ){
                ra.OSH_Dectect_Picture8_URL__c = setPictureUrl(ra.OSH_Dectect_Picture8__c);
                RA_Ids.add(ra.Id);
            }
        }
    }
 
    public String setPictureUrl(String pic){
 
        String phtTxt = '';
        if(String.isBlank(pic) == false) {
            Integer pStt = pic.indexOf('<img');
            if(pStt >= 0){
                pStt = pic.indexOf('src="');
                if(pStt >= 0){
                    Integer pEnd = pic.indexOf('"', pStt + 5);
                    if(pEnd >= 0){
                        phtTxt = pic.substring(pStt + 5, pEnd);
                        //富文本反转义
                        phtTxt = phtTxt.unescapeHtml4();
                    }
                }
            }
        }
        return phtTxt;
 
    }
 
    public void setStatusChange() {
        Map<String,id> rtmaps = new Map<String,id>();
        List<RecordType> rtIds = [SELECT Id,Name FROM RecordType where SobjectType='RepairAgainAn__c'];
 
        Date todayTemp = Date.today();
        for (RecordType rt : rtIds) {
           rtmaps.put(rt.Name, rt.Id);
        }
        for (RepairAgainAn__c raa : newList) {
            RepairAgainAn__c raaOld = oldMap.get(raa.id);
            //状态变更为草案中
            if (String.isBlank(raa.RepairAgain_Status__c)) {
                raa.RepairAgain_Status__c = '草案中';
            }
 
            //当RC的内容填写完毕
            if(raa.RepairAgain_Status__c == 'RC填写完毕' && raaOld.RepairAgain_Status__c != 'RC填写完毕') {
                raa.RecordTypeId = rtmaps.get('2.OSH');
                // 发起再注册分析时间
                raa.Start_RepairAgainAn_Day__c = todayTemp;
                // 和SAP交互  OPEN
                openRepairAgainRepairIds.add(raa.Repair__c);
            }
 
            //当OSH受理
            if(raa.RepairAgain_Status__c == 'OSH受理' && raaOld.RepairAgain_Status__c != 'OSH受理') {
                // OSH受理日
                raa.OSH_Accept_Day__c = todayTemp;
                // 和SAP交互
                oSHRepairAgainRepairIds.add(raa.Repair__c);
            }
 
            //当OSH内容填写完毕
            if(raa.RepairAgain_Status__c == 'OSH填写完毕' && raaOld.RepairAgain_Status__c != 'OSH填写完毕') {
                raa.RecordTypeId = rtmaps.get('3.OCSM');
                // 和SAP交互
                oSHWrittenRepairAgainRepairIds.add(raa.Repair__c);
            }
 
            //当OCSM跟进完毕
            if(raa.RepairAgain_Status__c == '完毕' && raaOld.RepairAgain_Status__c != '完毕') {
                raa.RecordTypeId = rtmaps.get('4.Final完毕');
                // 和SAP交互
                finalRepairAgainRepairIds.add(raa.Repair__c);
            }
            //当再修理状态变成取消时
            if(raa.RepairAgain_Status__c == '取消' && raaOld.RepairAgain_Status__c != '取消'){
                cleanRepairIds.add(raa.Repair__c);
            }
        }
    }
 
    public void submitRepair(){
 
        List<Repair__c> repairUpdateList = new List<Repair__c>();
 
        if(openRepairAgainRepairIds.size() > 0 || oSHRepairAgainRepairIds.size() > 0 || oSHWrittenRepairAgainRepairIds.size() > 0 || finalRepairAgainRepairIds.size() > 0 || cleanRepairIds.size() > 0){
 
            BatchIF_Log__c iflog = new BatchIF_Log__c();
                iflog.Type__c = 'NFM103';
                iflog.Log__c  = 'callout start\n';
                insert iflog;
 
            if(openRepairAgainRepairIds.size() > 0) {
                setRepairList(openRepairAgainRepairIds, repairUpdateList, 'OCSM已发起');
                //关联一个open的再修理分析,传X  2023/3/30 NFM103Controller.callout  参数方法有误暂注释
                NFM103Controller.callout(iflog.Id, openRepairAgainRepairIds,'OCSM已发起');
            }
 
            if(oSHRepairAgainRepairIds.size() > 0 ){
                setRepairList(oSHRepairAgainRepairIds, repairUpdateList, '品保已受理');
                NFM103Controller.callout(iflog.Id, oSHRepairAgainRepairIds,'品保已受理');
            }
 
            if(oSHWrittenRepairAgainRepairIds.size() > 0 ){
                setRepairList(oSHWrittenRepairAgainRepairIds, repairUpdateList, '品保出结论');
                NFM103Controller.callout(iflog.Id, oSHWrittenRepairAgainRepairIds,'品保出结论');
            }
 
            if(finalRepairAgainRepairIds.size() > 0) {
                setRepairList(finalRepairAgainRepairIds, repairUpdateList, 'OCSM出结论');
                //关联再修理分析完毕,传F
                NFM103Controller.callout(iflog.Id, finalRepairAgainRepairIds,'OCSM出结论');
            }
 
            if(cleanRepairIds.size() > 0 ){
                setRepairList(cleanRepairIds, repairUpdateList, '');
            }
 
            if(repairUpdateList.size() > 0 ){
                Update repairUpdateList;
            }
        }
    }
 
    public void setRepairList(List<Id> repairIdList, list<Repair__c> repairList, String status){
 
        for(Id repairId : repairIdList){
            Repair__c tempRepair = new Repair__c();
            tempRepair.Id = repairId;
            tempRepair.RepairAgainAn_Status__c = status;
            repairList.add(tempRepair);
        }
    }
 
    // public void updateAttachment(){
    //     for(RepairAgainAn__c ram : newList){
    //         String pid = ram.Id;
 
    //         String sql = 'SELECT Name,Body,ContentType from Attachment where ParentId =\'' + pid + '\'';
 
 
    //         Attachment att = Database.query(sql);
 
    //         System.debug('bName====='+att.Body);
    //     }
    // }
}