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
import { LightningElement,api, track } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';    
import sheet from '@salesforce/resourceUrl/sheet';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import importSpecialPrice from '@salesforce/apex/ImportdataController.importSpecialPrice';
import submitApproval from '@salesforce/apex/ImportdataController.submitApproval';
const columns = [
    {
        label: '追溯特价申请',
        fieldName: 'name',
        type: 'text'
    },
    {
        label: '合同名称',
        fieldName: 'accountName',
        type: 'text'
    },
    {
        label: '状态',
        fieldName: 'status',
        type: 'text'
    },
    {
        label: '特价类型',
        fieldName: 'type',
        type: 'text'
    }
];
 
export default class LexImportSpecialPrice extends LightningElement {
    @track wrapperList;
    @track columns = columns;
    @track showLoadingSpinner = false;
    connectedCallback(){
        //导入xlsx文件
        Promise.all([
            loadScript(this, sheet),
        ]).then(() => {
           console.log('导入xlsx成功')
        })
        .catch(() => {
            console.log('导入xlsx失败')
        });
    }
 
    
    analyseExcelToJson(e){
        this.showLoadingSpinner = true;
        let file = this.template.querySelector('.fileUpload').files[0];
        return new Promise((resolve, reject) => {
            if (file instanceof File) {
            const reader = new FileReader();
            reader.onloadend = (progressEvent) => {
            const arrayBuffer = reader.result;
            // const options = { type: 'array binary' };
            const options = { type: 'array' };
            const workbook = XLSX.read(arrayBuffer, options);
            const sheetNames = workbook.SheetNames;
            const result = sheetNames.map((sheetName) => workbook.Sheets[sheetName]);
            console.log('result',result)
            let data=[];
            for(const sheet in workbook.Sheets){
                if(workbook.Sheets.hasOwnProperty(sheet)){
                    data.push(
                        XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
                    )
                }
            }
            resolve(data);
            console.log(data);
            this.importData(data[0]);
            };
            e.target.value='';
            reader.readAsArrayBuffer(file);
        } else {
              reject(new Error('入参不是 File 类型'));
              this.showLoadingSpinner = false;
        }
        });
    }
 
    async importData(data){
        
        const result = await importSpecialPrice({dataStr:JSON.stringify(data)});
        this.showLoadingSpinner = false;
        if(result.msg == 'Success'){
            this.wrapperList = result.data;
        }else{
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error',
                    message: result.msg,
                    variant: 'error'
                })
            );
        }
    }
 
    async massSubmit(event){
        this.showLoadingSpinner = true;
        console.log('test');
        var el = this.template.querySelector('lightning-datatable');
        var selectedrows = el.getSelectedRows();
        console.log(selectedrows);
        var processrows = [];
        for (var i = 0; i < selectedrows.length; i++) {
            // selectedrows[i].comments=this.comments;
            processrows.push(selectedrows[i]);
        }
        console.log(111);
        console.log(processrows)
        const result = await submitApproval(processrows);
        console.log(result)
        if(result == 'Success'){
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message:'提交成功',
                    variant: 'success'
                })
            );
        }else{
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error',
                    message: result,
                    variant: 'error'
                })
            );
        }
        this.showLoadingSpinner = false;
    }
 
 
    
}