buli
2023-06-05 0f44cb1a0b7b36860e9ce81c1eac9e395d87f0b4
force-app/main/default/lwc/lexOutboundorderImport/lexOutboundorderImport.js
@@ -3,231 +3,268 @@
import init from "@salesforce/apex/LexOutboundorderImportController.init";
import importCSVFile from "@salesforce/apex/LexOutboundorderImportController.importCSVFile";
import dataImport from "@salesforce/apex/LexOutboundorderImportController.dataImport";
//table css
import { loadStyle } from "lightning/platformResourceLoader";
import WrappedHeaderTable from "@salesforce/resourceUrl/lexdatatable";
const columns = [
  {
    label: "出库单名称",
    fieldName: "orderName",
    hideDefaultActions: true,
  },
  {
    label: "目的",
    fieldName: "orderSummonsForDirction",
    hideDefaultActions: true,
  },
  {
    label: "医院编码",
    fieldName: "hospitalCode",
    hideDefaultActions: true,
  },
  {
    label: "医院名称",
    fieldName: "hospitalName",
    hideDefaultActions: true,
  },
  {
    label: "科室",
    fieldName: "orderOrderForCustomerText",
    hideDefaultActions: true,
  },
  {
    label: "二级经销商",
    fieldName: "agencyName",
    hideDefaultActions: true,
  },
  {
    label: "出库/销售日期",
    fieldName: "orderOutboundDate",
    hideDefaultActions: true,
  },
    {
        label: "出库单名称",
        fieldName: "orderName",
        hideDefaultActions: true,
        initialWidth: 200
    },
    {
        label: "目的",
        fieldName: "orderSummonsForDirction",
        hideDefaultActions: true,
    },
    {
        label: "医院编码",
        fieldName: "hospitalCode",
        hideDefaultActions: true,
    },
    {
        label: "医院名称",
        fieldName: "hospitalName",
        hideDefaultActions: true,
        initialWidth: 300
    },
    {
        label: "科室",
        fieldName: "orderOrderForCustomerText",
        hideDefaultActions: true,
    },
    {
        label: "二级经销商",
        fieldName: "agencyName",
        hideDefaultActions: true,
    },
    {
        label: "出库/销售日期",
        fieldName: "orderOutboundDate",
        hideDefaultActions: true,
    },
];
export default class LexOutboundorderImport extends LightningElement {
  @track isShowSpinner = true;
  //文件上传
  @track showLoadingSpinner = false;
  @track UploadFile = "Upload File";
  @track fileName = "";
  @track fileData = [];
  @track fileColumns = [
    {
      label: "标题",
      fieldName: "url",
      type: "url",
      typeAttributes: { label: { fieldName: "Title" }, target: "_blank" },
      hideDefaultActions: true,
    },
    {
      label: "创建人",
      fieldName: "CreatedByName",
      hideDefaultActions: true,
    },
  ];
  filesUploaded = [];
  fileContents;
  fileReader;
  content;
  MAX_FILE_SIZE = 1500000;
  get acceptedType() {
    return [".csv"];
  }
    @track isShowSpinner = true;
    //文件上传
    @track showLoadingSpinner = false;
    @track UploadFile = "Upload File";
    @track fileName = "";
    @track fileData = [];
    @track fileColumns = [
        {
            label: "标题",
            fieldName: "url",
            type: "url",
            typeAttributes: { label: { fieldName: "Title" }, target: "_blank" },
            hideDefaultActions: true,
        },
        {
            label: "创建人",
            fieldName: "CreatedByName",
            hideDefaultActions: true,
        },
    ];
    filesUploaded = [];
    fileContents;
    fileReader;
    content;
    MAX_FILE_SIZE = 1500000;
    get acceptedType() {
        return [".csv"];
    }
  //列表
  columns = columns;
  @track data = [];
    //列表
    columns = columns;
    @track data = [];
  //数据
  @track accountid = "";
  @track userWorkLocation = "";
  @track agencyProType = "";
  @track accountName = "";
  @track sqlagencyProType = "";
  @track csvRecordStr = [];
  @track saveFLGbln = false;
    //数据
    @track accountid = "";
    @track userWorkLocation = "";
    @track agencyProType = "";
    @track accountName = "";
    @track sqlagencyProType = "";
    @track csvRecordStr = [];
    @track saveFLGbln = false;
    @track secondAgencyMap = {};
    @track hospitalSysMap = {};
  connectedCallback() {
    init()
      .then((r) => {
        r = JSON.parse(JSON.stringify(r));
        console.log("r = " + JSON.stringify(r));
        if (r.status == "Success") {
          this.accountid = r.entity.accountid;
          this.userWorkLocation = r.entity.userWorkLocation;
          this.agencyProType = r.entity.agencyProType;
          this.accountName = r.entity.accountName;
          this.sqlagencyProType = r.entity.sqlagencyProType;
          this.isShowSpinner = false;
    stylesLoaded = false;
    renderedCallback() {
        if (!this.stylesLoaded) {
            Promise.all([loadStyle(this, WrappedHeaderTable)])
                .then(() => {
                    console.log("Custom styles loaded");
                    this.stylesLoaded = true;
                })
                .catch((error) => {
                    console.error("Error loading custom styles");
                });
        }
    }
    connectedCallback() {
        init().then((r) => {
            r = JSON.parse(JSON.stringify(r));
            console.log("r = " + JSON.stringify(r));
            if (r.status == "Success") {
                this.accountid = r.entity.accountid;
                this.userWorkLocation = r.entity.userWorkLocation;
                this.agencyProType = r.entity.agencyProType;
                console.log('this.agencyProType = ' + this.agencyProType);
                this.accountName = r.entity.accountName;
                this.sqlagencyProType = r.entity.sqlagencyProType;
                this.isShowSpinner = false;
            } else {
                console.log("r = " + JSON.stringify(r));
                this.showMyToast('初始化失败', r.msg, 'Error')
            }
        }).catch((error) => {
            console.log("error = " + JSON.stringify(error));
            this.showMyToast('错误', '初始化失败', 'Error')
        });
    }
    getCsvFile() {
        this.isShowSpinner = true;
        if (this.filesUploaded.length > 0) {
            this.file = this.filesUploaded[0];
            if (this.file.size > this.MAX_FILE_SIZE) {
                window.console.log("文件过大");
                this.isShowSpinner = false;
                return;
            }
            this.fileReader = new FileReader();
            this.fileReader.onloadend = () => {
                this.fileContents = this.fileReader.result;
                let base64 = "base64,";
                this.content = this.fileContents.indexOf(base64) + base64.length;
                this.fileContents = this.fileContents.substring(this.content);
                console.log('getCsvFile')
                this.importCSVFile();
            };
            this.fileReader.readAsDataURL(this.file);
        } else {
          console.log("r = " + JSON.stringify(r));
          this.showToast("Error", r.msg);
            this.fileName = "选择一个csv文件上传";
            this.showMyToast('上传失败', '选择一个csv文件上传', 'Error')
        }
      })
      .catch((error) => {
        console.log("error = " + JSON.stringify(error));
        this.showToast("Error", error.message);
      });
  }
    }
  getCsvFile() {
    this.isShowSpinner = true;
    if (this.filesUploaded.length > 0) {
      this.file = this.filesUploaded[0];
      if (this.file.size > this.MAX_FILE_SIZE) {
        window.console.log("文件过大");
    importCSVFile() {
        console.log('importCSVFile')
        console.log('this.sqlagencyProType = ' + this.sqlagencyProType);
        console.log('this.userWorkLocation = ' + this.userWorkLocation);
        console.log('this.accountName = ' + this.accountName);
        importCSVFile({
            base64DataLwc: encodeURIComponent(this.fileContents),
            sqlagencyProTypeLwc: this.sqlagencyProType,
            userWorkLocationLwc: this.userWorkLocation,
            accountNameLwc: this.accountName,
        }).then((r) => {
            r = JSON.parse(JSON.stringify(r));
            console.log("r = " + JSON.stringify(r));
            if (r.status == "Success" && r.msg == "") {
                console.log("importCSVFile success");
                this.saveFLGbln = r.entity.saveFLGbln;
                this.csvRecordStr = r.entity.csvRecordStr;
                this.secondAgencyMap = r.entity.secondAgencyMap;
                this.hospitalSysMap = r.entity.hospitalSysMap;
                this.data = r.entity.orderRecords;
                for (var i in this.data) {
                    this.data[i]['orderName'] = this.data[i].order.Name;
                    this.data[i]['orderSummonsForDirction'] = this.data[i].order.SummonsForDirction__c;
                    this.data[i]['orderOrderForCustomerText'] = this.data[i].order.Order_ForCustomerText__c;
                    this.data[i]['orderOutboundDate'] = this.data[i].order.Outbound_Date__c;
                }
                this.isShowSpinner = false;
            } else if (r.msg != "") {
                console.log("r.msg = " + JSON.stringify(r.msg));
                this.saveFLGbln = r.entity.saveFLGbln;
                this.data = r.entity.orderRecords;
                for (var i in this.data) {
                    this.data[i]['orderName'] = this.data[i].order.Name;
                    this.data[i]['orderSummonsForDirction'] = this.data[i].order.SummonsForDirction__c;
                    this.data[i]['orderOrderForCustomerText'] = this.data[i].order.Order_ForCustomerText__c;
                    this.data[i]['orderOutboundDate'] = this.data[i].order.Outbound_Date__c;
                }
                this.showMyToast('导入失败', r.msg, 'Error')
            } else {
                console.log("r.msg = " + JSON.stringify(r.msg));
                this.showMyToast('导入失败', r.msg, 'Error')
            }
        }).catch((error) => {
            console.log("error = " + JSON.stringify(error.message));
            this.showMyToast('导入错误', '导入失败', 'Error')
        });
    }
    dataImport() {
        this.isShowSpinner = true;
        let cloneData = this.data;
        for (var i in cloneData) {
            delete cloneData[i].orderName;
            delete cloneData[i].orderSummonsForDirction;
            delete cloneData[i].orderOrderForCustomerText;
            delete cloneData[i].orderOutboundDate;
        }
        console.log('this.agencyProType = ' + this.agencyProType);
        dataImport({
            csvRecordStrLwc: this.csvRecordStr,
            orderRecordsLwc: JSON.stringify(cloneData),
            sqlagencyProTypeLwc: this.sqlagencyProType,
            userWorkLocationLwc: this.userWorkLocation,
            accountNameLwc: this.accountName,
            accountidLwc: this.accountid,
            agencyProTypeLwc: this.agencyProType,
            secondAgencyMapLwc: this.secondAgencyMap,
            hospitalSysMapLwc: this.hospitalSysMap,
        }).then((r) => {
            r = JSON.parse(JSON.stringify(r));
            console.log("r = " + JSON.stringify(r));
            if (r.status == "Success" && r.msg == "") {
                this.saveFLGbln = true;
                console.log("dataImport success");
                this.showMyToast('成功', '保存成功', 'Success')
            } else if (r.msg != "") {
                console.log("r.msg = " + JSON.stringify(r.msg));
                this.showMyToast('保存失败', r.msg, 'Error')
            } else {
                console.log("r.msg = " + JSON.stringify(r.msg));
                this.showMyToast('保存失败', r.msg, 'Error')
            }
        }).catch((error) => {
            console.log("error = " + JSON.stringify(error.message));
            this.showMyToast('错误', '保存失败', 'Error')
        });
    }
    handleFilesChange(event) {
        console.log("handleFilesChange");
        if (event.target.files.length > 0) {
            this.filesUploaded = event.target.files;
            this.fileName = event.target.files[0].name;
        }
    }
    showMyToast(title, message, variant) {
        this.isShowSpinner = false;
        return;
      }
      this.fileReader = new FileReader();
      this.fileReader.onloadend = () => {
        this.fileContents = this.fileReader.result;
        let base64 = "base64,";
        this.content = this.fileContents.indexOf(base64) + base64.length;
        this.fileContents = this.fileContents.substring(this.content);
        console.log('getCsvFile')
        this.importCSVFile();
      };
      this.fileReader.readAsDataURL(this.file);
    } else {
      this.fileName = "选择一个csv文件上传";
      this.showToast("Error", '选择一个csv文件上传');
    }
  }
  importCSVFile() {
    console.log('importCSVFile')
    importCSVFile({
      base64Data: encodeURIComponent(this.fileContents),
      sqlagencyProType: this.sqlagencyProType,
      userWorkLocation: this.userWorkLocation,
      accountName: this.accountName,
    }).then((r) => {
        r = JSON.parse(JSON.stringify(r));
        console.log("r = " + JSON.stringify(r));
        if (r.status == "Success" && r.msg == "") {
            console.log("importCSVFile success");
            this.saveFLGbln = r.entity.saveFLGbln;
            this.csvRecordStr = r.entity.csvRecordStr;
            this.data = r.entity.orderRecords;
            for(var i in this.data){
                this.data[i]['orderName'] = this.data[i].order.Name;
                this.data[i]['orderSummonsForDirction'] = this.data[i].order.SummonsForDirction__c;
                this.data[i]['orderOrderForCustomerText'] = this.data[i].order.Order_ForCustomerText__c;
                this.data[i]['orderOutboundDate'] = this.data[i].order.Outbound_Date__c;
            }
            this.isShowSpinner = false;
        }else if(r.msg != ""){
            console.log("r.msg = " + JSON.stringify(r.msg));
            this.saveFLGbln = r.entity.saveFLGbln;
            this.data = r.entity.orderRecords;
            for(var i in this.data){
                this.data[i]['orderName'] = this.data[i].order.Name;
                this.data[i]['orderSummonsForDirction'] = this.data[i].order.SummonsForDirction__c;
                this.data[i]['orderOrderForCustomerText'] = this.data[i].order.Order_ForCustomerText__c;
                this.data[i]['orderOutboundDate'] = this.data[i].order.Outbound_Date__c;
            }
            this.showToast("Error", r.msg);
        }else{
            console.log("r.msg = " + JSON.stringify(r.msg));
            this.showToast("Error", r.msg);
        console.log('show custom message');
        var iconName = '';
        var content = '';
        if (variant.toLowerCase() == 'success') {
            iconName = 'utility:check';
        } else {
            iconName = 'utility:error';
        }
      })
      .catch((error) => {
        console.log("error = " + JSON.stringify(error.message));
        this.showToast("Error", error.message);
      });
  }
  dataImport(){
    this.isShowSpinner = true;
    let cloneData = this.data;
    for(var i in cloneData){
      delete cloneData[i].orderName;
      delete cloneData[i].orderSummonsForDirction;
      delete cloneData[i].orderOrderForCustomerText;
      delete cloneData[i].orderOutboundDate;
  }
    dataImport({
      csvRecordStr: this.csvRecordStr,
      orderRecords: cloneData,
      sqlagencyProType: this.sqlagencyProType,
      userWorkLocation: this.userWorkLocation,
      accountName : this.accountName
    }).then((r) => {
        r = JSON.parse(JSON.stringify(r));
        console.log("r = " + JSON.stringify(r));
        if (r.status == "Success" && r.msg == "") {
            console.log("dataImport success");
            this.showToast("Success", '保存成功');
        }else if(r.msg != ""){
            console.log("r.msg = " + JSON.stringify(r.msg));
            this.showToast("Error", r.msg);
        }else{
            console.log("r.msg = " + JSON.stringify(r.msg));
            this.showToast("Error", r.msg);
        if (message != '') {
            content = '<h2><strong>' + title + '<strong/></h2><h5>' + message + '</h5>';
        } else {
            content = '<h2><strong>' + title + '<strong/></h2>';
        }
      })
      .catch((error) => {
        console.log("error = " + JSON.stringify(error.message));
        this.showToast("Error", error.message);
      });
  }
  handleFilesChange(event) {
    console.log("handleFilesChange");
    if (event.target.files.length > 0) {
      this.filesUploaded = event.target.files;
      this.fileName = event.target.files[0].name;
        this.template.querySelector('c-common-toast').showToast(variant, content, iconName, 10000);
    }
  }
  showToast(type, msg) {
    this.isShowSpinner = false;
    const event = new ShowToastEvent({
      title: type,
      variant: type,
      message: msg,
    });
    this.dispatchEvent(event);
  }
}