buli
2023-07-11 0c4796706fc9473d069b620321a54b20a119906c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import { LightningElement,wire,track,api} from 'lwc';
import { CurrentPageReference } from "lightning/navigation";
import { CloseActionScreenEvent } from 'lightning/actions';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 
import NAME_FIELD from '@salesforce/schema/Repair__c.Incharge_Staff__c';
import MY_CUSTOM_OBJECT from '@salesforce/schema/Repair__c';
 
import search from '@salesforce/apex/lexBatchSelectRepairLWCController.search';
import showPDF from '@salesforce/apex/lexBatchSelectRepairLWCController.showPDF';
 
/*
const actions = [
    { label: 'Show details', name: 'show_details' },
    { label: 'Delete', name: 'delete' },
    ];*/
 
const columns = [
        { label: '状态1', fieldName: 'Status1__c'},
        { label: '状态2', fieldName: 'Status2__c' },
        { label: 'RS修理单号', fieldName: 'Name' },
        { label: 'SAP修理单号', fieldName: 'SAP_Service_Repair_No__c' },
        // { label: '型号', fieldName: 'deliveredProductName' },
        { label: '型号', fieldName: 'deliveredProductUrl',type:'url',
            typeAttributes:{
                label:{
                    fieldName:'deliveredProductName'
                },
                target:'_balank'
            }
        },
        { label: '机身编号', fieldName: 'SerialNumber__c' },
        { label: '医院名称', fieldName: 'HP_Name__c' },
        { label: '省份', fieldName: 'State_Hospital__c' },
        // { label: '修理委托者', fieldName: 'Incharge_Staff_Name' },
        { label: '修理委托者', fieldName: 'inchargeStaffUrl',type:'url',
            typeAttributes:{
                label:{
                    fieldName:'inchargeStaffName'
                },
                target:'_balank'
            }
        },
        { label: 'FSE申请日期', fieldName: 'FSE_ApplyForRepair_Day__c' },
        { label: '维修中心', fieldName: 'work_location_select__c' },
        { label: '服务方式', fieldName: 'On_site_repair__c' },
        { label: '有无维修合同对象', fieldName: 'Number_of_EffectiveContract__c' },
        { label: '无偿区别标志', fieldName: 'NewProductGuaranteeObject__c' }
   /* {
    type: 'action',
    typeAttributes: { rowActions: actions },
    },*/
    ];
 
export default class lexBatchSelectRepairLWC extends LightningElement {
    // Expose a field to make it available in the template
    nameField = NAME_FIELD;
 
    // Flexipage provides recordId and objectApiName
    @api recordId;
    @api objectApiName;
 
    data = [];
    columns = columns;
 
    //状态1
    status1Options = [{ label: '--无--', value: '' },
                    { label: '0.申请完毕', value: '0.申请完毕' },
                    { label: '2.维修报价阶段', value: '2.维修报价阶段' },
                    { label: '3.维修阶段', value: '3.维修阶段' },
                    { label: '4.修理品返送阶段', value: '4.修理品返送阶段' },
                    { label: '5.完毕', value: '5.完毕' },
                    { label: '0.删除', value: '0.删除' },
                    { label: '0.取消', value: '0.取消' }];
    //状态2
    status2Options = [{label:'00.申请完毕', value:'00.申请完毕'},
                    {label:'01.分公司受理完毕', value:'01.分公司受理完毕'},
                    {label:'02.RC受理完毕', value:'02.RC受理完毕'},
                    {label:'03.报价检查结束', value:'03.报价检查结束'},
                    {label:'04.报价跟进中', value:'04.报价跟进中'},
                    {label:'05.报价同意完备', value:'05.报价同意完备'},
                    {label:'06.零件齐备', value:'06.零件齐备'},
                    {label:'07.修理開始', value:'07.修理開始'},
                    {label:'08.修理预计完成', value:'08.修理预计完成'},
                    {label:'09.修理完成', value:'09.修理完成'},
                    {label:'10.最终检查完成', value:'10.最终检查完成'},
                    {label:'11.RC修理品已返送', value:'11.RC修理品已返送'},
                    {label:'12.发票已发送', value:'12.发票已发送'},
                    {label:'13.已返送到用户', value:'13.已返送到用户'},
                    {label:'14.收到验收单', value:'14.收到验收单'},
                    {label:'15.验收单签收复核通过', value:'15.验收单签收复核通过'},
                    {label:'16.部分付款', value:'16.部分付款'},
                    {label:'16.完毕', value:'16.完毕'},
                    {label:'17.付款完毕', value:'17.付款完毕'},
                    {label:'00.取消', value:'00.取消'},
                    {label:'00.删除', value:'00.删除'},
                    {label:'00.关闭', value:'00.关闭'}];
    //服务方式
    siteRepairItems = [{label:'--无--',value:''},
        {label:'RC修理',value:'RC修理'},
        {label:'直送SORC修理',value:'直送SORC修理'},
        {label:'直送OGZ修理',value:'直送OGZ修理'},
        {label:'办事处修理',value:'办事处修理'},
        {label:'现场修理',value:'现场修理'}];
 
    searchObj = {};
    repair = {};
    selectedRepairs;
 
    /* @wire(CurrentPageReference)
     getStateParameters(currentPageReference) {
             console.log(111);
             console.log(currentPageReference);
 
         if (currentPageReference) {
           const urlValue = currentPageReference.state.recordId;
           if (urlValue) {
             let str = `${urlValue}`;
             console.log("str");
             console.log(str);
             this.recordId = str;
           }
         }
     }*/
 
    connectedCallback(){ 
        // console.log(this.recordId);   
    }
    //状态1
    handleStatus1Change(event){
        /*console.log(event);
        console.log(event.detail);*/
        this.searchObj.Status1 = event.detail.value;
        // console.log(JSON.stringify(this.searchObj));
    }
    //状态2
    handleStatus2Change(event){
        this.searchObj.Status2 = event.detail.value;
        // console.log(this.searchObj);
    }
    //服务方式
    handleSiteChange(event){
        this.searchObj.onSiteRepair = event.detail.value;
        // console.log(this.searchObj);
    }
    //RS修理单号 RepairName
    handleRepairNameChange(event){
        this.searchObj.RepairName = event.detail.value;
        // console.log(this.searchObj);
    }
    //SAP修理单号 SAPRepairNo
    handleSAPRepairNoChange(event){
        this.searchObj.SAPRepairNo = event.detail.value;
        // console.log(this.searchObj);
    }
    //维修中心 workLocationSelect
    handleWorkLocationChange(event){
        this.searchObj.workLocationSelect = event.detail.value;
    }
    //机身编码 SerialNumber
    handleSerialNumberChange(event){
        this.searchObj.SerialNumber = event.detail.value;
    }
    // 省份  State_Hospital
    handleStateHospitalChange(event){
        this.searchObj.State_Hospital = event.detail.value;
    }
    //医院名称  HospitalName
    handleHospitalChange(event){
        this.searchObj.HospitalName = event.detail.value;
    }
    //FSE申请日 repair.Aware_date__c
    handleAwareDateDStart(event){
        this.repair.Aware_date__c = event.detail.value;
        console.log(this.repair.Aware_date__c);
    }
    // repair.Aware_date2__c
    handleAwareDateDEnd(event){
        this.repair.Aware_date2__c = event.detail.value;
        // console.log(this.repair.Aware_date2__c);
    }
    // 修理委托者 repair.Incharge_Staff__c
    handleInchargeStaffChange(event){
        this.repair.Incharge_Staff__c = event.detail.value;
        // console.log(this.repair.Aware_date2__c);
    }
 
   //检索
    searchRepair(event) {
        console.log(this.repair.Aware_date__c);
        if (JSON.stringify(this.repair) != "{}") {
            console.log('this.repair');
            this.searchObj.repair = this.repair;
            // fse 时间条件
            /*if (!(this.repair.Aware_date__c != null && this.repair.Aware_date2__c != null) || this.repair.Aware_date__c > this.repair.Aware_date2__c) {
                const event = new ShowToastEvent({
                             title: 'error',
                             message: 'FSE申请日输入有误',
                             variant:'error',
                            });
                this.dispatchEvent(event);
                return;
            }*/
        }
        let jsonStr = JSON.stringify(this.searchObj);
        if (jsonStr === "{}") {     
            const event = new ShowToastEvent({
                             title: 'error',
                             message: '请至少添加一个检索条件!',
                             variant:'error',
                            });
            this.dispatchEvent(event);
        }else{
            console.log(jsonStr);
            search({
                json: jsonStr
            }).then(result => {
                console.log(result.repairData);
                console.log('length:'+result.repairData.length);
                if (result.repairData.length == 0 || result.repairData.length == 200) {
                    const event = new ShowToastEvent({
                             title: 'warning',
                             message: result.status,
                             variant:'warning',
                            });
                    this.dispatchEvent(event);
                }
                let returnArr = [];
                result.repairData.forEach(function(v){ 
                    let objRepair = v.repair;
                    //添加修理委托者和型号名称
                    objRepair.deliveredProductName = objRepair.Delivered_Product__r.Name;
                    objRepair.deliveredProductUrl = "/"+objRepair.Delivered_Product__c;
                    objRepair.inchargeStaffName = objRepair.Incharge_Staff__r.Name;
                    objRepair.inchargeStaffUrl = "/"+objRepair.Incharge_Staff__c;
                    // console.log(objRepair);
                    returnArr.push(objRepair);
                }); 
                /*console.log('arr');
                console.log(arr);
                console.log(arr.length);*/
                this.data = returnArr;
                // console.log(this.data);
            }).catch(error => {
                console.log("error");
                console.log(error);
            }).finally(() => {
                
            });     
        }
        
        
    }
    //导出为Excel  画table
    exportContactData(){
        // Prepare a html table
        let doc = '<table>';
        // Add styles for the table
        doc += '<style>';
        doc += 'table, th, td {';
        doc += '    border: 1px solid black;';
        doc += '    border-collapse: collapse;';
        doc += '}';          
        doc += '</style>';
        // Add all the Table Headers
        doc += '<tr>';
        this.columns.forEach(element => {            
            doc += '<th>'+ element.label +'</th>'           
        });
        doc += '</tr>';
        // Add the data rows
        let fieldNameArr =[];
        this.columns.forEach(function(v){ 
            if (v['typeAttributes']) {
                fieldNameArr.push(v['typeAttributes'].label.fieldName);
            }else{
                fieldNameArr.push(v.fieldName);
            }
        }); 
        console.log(fieldNameArr);
        fieldNameArr.forEach(v=>{
            console.log(v);
            // console.log(this.data[0].v);
        })
        this.data.forEach(record => {
            doc += '<tr>';
            if (record.Status1__c != null) {
                doc += '<th>'+record.Status1__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.Status2__c != null) {
            doc += '<th>'+record.Status2__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.Name != null) {
            doc += '<th>'+record.Name+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.SAP_Service_Repair_No__c != null) {
            doc += '<th>'+record.SAP_Service_Repair_No__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.deliveredProductName != null) {
            doc += '<th>'+record.deliveredProductName+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.SerialNumber__c != null) {
            doc += '<th>'+record.SerialNumber__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.HP_Name__c != null) {
            doc += '<th>'+record.HP_Name__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.State_Hospital__c != null) {
            doc += '<th>'+record.State_Hospital__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.inchargeStaffName != null) {
            doc += '<th>'+record.inchargeStaffName+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.FSE_ApplyForRepair_Day__c != null) {
            doc += '<th>'+record.FSE_ApplyForRepair_Day__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.work_location_select__c != null) {
            doc += '<th>'+record.work_location_select__c+'</th>';
            }else{
                doc += '<th></th>'
            } 
            if (record.On_site_repair__c != null) {
            doc += '<th>'+record.On_site_repair__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.Number_of_EffectiveContract__c != null) {
            doc += '<th>'+record.Number_of_EffectiveContract__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
            if (record.NewProductGuaranteeObject__c != null) {
            doc += '<th>'+record.NewProductGuaranteeObject__c+'</th>'; 
            }else{
                doc += '<th></th>'
            }
                // fieldNameArr.forEach(fieldName =>{
                //     doc += '<th>'+record.fieldName+'</th>'; 
                // });
                doc += '</tr>';
        });
        doc += '</table>';
        var element = 'data:application/vnd.ms-excel,' + encodeURIComponent(doc);
        let downloadElement = document.createElement('a');
        downloadElement.href = element;
        downloadElement.target = '_self';
        // use .csv as extension on below line if you want to export data as csv
        downloadElement.download = '修理明细表.xls';
        document.body.appendChild(downloadElement);
        downloadElement.click();
    }
 
    // this method validates the data and creates the csv file to download  this.data的所有字段
     downloadCSVFile1() {   
        console.log("downloadCSVFile1");
        let rowEnd = '\n';
        let csvString = '';
        // this set elminates the duplicates if have any duplicate keys
        let rowData = new Set();
        let columnsData = new Set();
        let columnsLabelData = new Set();
 
        // getting keys from data
        this.data.forEach(function (record) {
            Object.keys(record).forEach(function (key) {
                rowData.add(key);
            });
        });
        console.log(rowData);
 
        // Array.from() method returns an Array object from any object with a length property or an iterable object.
        rowData = Array.from(rowData);
        console.log(rowData);
        
        // splitting using ','
        csvString += rowData.join(',');
        csvString += rowEnd;
        console.log(csvString);
        this.columns.forEach(function(v){ 
            columnsLabelData.add(v.label);
            if (v['typeAttributes']) {
                columnsData.add(v['typeAttributes'].label.fieldName);
            }else{
                columnsData.add(v.fieldName);
            }
        }); 
        console.log("columnsData");
        console.log(columnsLabelData);
        console.log(columnsData);
        console.log(rowData[1]);
        console.log(Array.from(columnsData));
        console.log(Array.from(columnsData).hasOwnProperty(rowData[1]));
        console.log(this.data[0].Name);
        console.log(this.data[0][rowData[1]]);
        // main for loop to get the data based on key value
        for(let i=0; i < this.data.length; i++){
            let colValue = 0;
 
            // validating keys in data
            for(let key in rowData) {
                if(rowData.hasOwnProperty(key)) {
                    // Key value 
                    // Ex: Id, Name
                    let rowKey = rowData[key];
                    // add , after every value except the first.
                    if(colValue > 0){
                        csvString += ',';
                    }
                    // If the column is undefined, it as blank in the CSV file.
                    let value = this.data[i][rowKey] === undefined ? '' : this.data[i][rowKey];
                    csvString += '"'+ value +'"';
                    colValue++;
                }
            }
            csvString += rowEnd;
        }
 
        // Creating anchor element to download
        let downloadElement = document.createElement('a');
 
        // This  encodeURI encodes special characters, except: , / ? : @ & = + $ # (Use encodeURIComponent() to encode these characters).
        downloadElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvString);
        downloadElement.target = '_self';
        // CSV File Name
        downloadElement.download = 'Account Data.csv';
        // below statement is required if you are using firefox browser
        document.body.appendChild(downloadElement);
        // click() Javascript function to download CSV file
        downloadElement.click(); 
    }
    //导出为Excel  
    downloadCSVFile() {   
        console.log("downloadCSVFile");
        let rowEnd = '\n';
        let csvString = '';
        // this set elminates the duplicates if have any duplicate keys
        //表格标题
        let columnsData = new Set();
        //表格字段
        let rowData = new Set();
 
        // getting keys from data
        this.columns.forEach(function(v){ 
            columnsData.add(v.label);
            if (v['typeAttributes']) {
                rowData.add(v['typeAttributes'].label.fieldName);
            }else{
                rowData.add(v.fieldName);
            }
        }); 
        console.log(columnsData);
        console.log(rowData);
        // Array.from() method returns an Array object from any object with a length property or an iterable object.
        rowData = Array.from(rowData);
        columnsData = Array.from(columnsData);
        // getting keys from data
       /* console.log("rowData");
        rowData.forEach(function(v){ 
            console.log(v);
        }); 
        console.log("columnsData");
        columnsData.forEach(function(v){ 
            console.log(v);
        }); */
        
        // splitting using ','
        csvString += columnsData.join(',');
        csvString += rowEnd;
        // console.log(csvString);
        // main for loop to get the data based on key value
        for(let i=0; i < this.data.length; i++){
            let colValue = 0;
 
            // validating keys in data
            for(let key in rowData) {
                if(rowData.hasOwnProperty(key)) {//del
                    // Key value 
                    // Ex: Id, Name
                    let rowKey = rowData[key];
                    // add , after every value except the first.
                    if(colValue > 0){
                        csvString += ',';
                    }
                    // If the column is undefined, it as blank in the CSV file.
                    let value = this.data[i][rowKey] === undefined ? '' : this.data[i][rowKey];
                    csvString += '"'+ value +'"';
                    colValue++;
                }
            }
            csvString += rowEnd;
        }
 
        // Creating anchor element to download
        let downloadElement = document.createElement('a');
 
        // This  encodeURI encodes special characters, except: , / ? : @ & = + $ # (Use encodeURIComponent() to encode these characters).
        downloadElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvString);
        downloadElement.target = '_self';
        // CSV File Name
        downloadElement.download = '修理明细表.csv';
        // below statement is required if you are using firefox browser
        document.body.appendChild(downloadElement);
        // click() Javascript function to download CSV file
        downloadElement.click(); 
    }
    //获取选中Repair
    getSelectedRows(event) {
        const selectedRows = event.detail.selectedRows;
        console.log(selectedRows);
        //Repair__c   selectedRepairs
        // Display that fieldName of the selected rows
        let selectedRepairsArr = [];
        for (let i = 0; i < selectedRows.length; i++) {
            selectedRepairsArr.push(selectedRows[i]);
        }
        this.selectedRepairs = selectedRepairsArr;
    }
 
    //跳转到pdf页面
    skipPage() {
        console.log("selectedRows");
        // console.log(jsonStr);
        if (this.selectedRepairs.length <= 0 ) {
            const event = new ShowToastEvent({
                                     title: 'error',
                                     message: '请至少选择一个修理',
                                     variant:'error',
                                    });
            this.dispatchEvent(event);
            return;
        }
        let jsonStr = JSON.stringify(this.selectedRepairs);
        showPDF({
            json: jsonStr
            }).then(result => {
                console.log(result);
                if (result.indexOf("https://ocsm") != -1) {
                     window.open(result, "维修委托书PDF", "");
                }else{
                    const event = new ShowToastEvent({
                                     title: 'error',
                                     message: result,
                                     variant:'error',
                                    });
                    this.dispatchEvent(event);
                }
            }).catch(error => {
                console.log("error");
                console.log(error);
            }).finally(() => {
                
            }); 
    }
 
}