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;
|
}
|
|
|
|
}
|