buli
2023-04-20 f0bccccbb88d93ac05010c17d4b2e0cb22a2ce9a
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
import { LightningElement, api, track, wire } from "lwc";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import init from "@salesforce/apex/LexOutboundorderImportController.init";
import importCSVFile from "@salesforce/apex/LexOutboundorderImportController.importCSVFile";
import dataImport from "@salesforce/apex/LexOutboundorderImportController.dataImport";
 
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,
  },
];
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"];
  }
 
  //列表
  columns = columns;
  @track data = [];
 
  //数据
  @track accountid = "";
  @track userWorkLocation = "";
  @track agencyProType = "";
  @track accountName = "";
  @track sqlagencyProType = "";
  @track csvRecordStr = [];
  @track saveFLGbln = false;
 
  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;
        } else {
          console.log("r = " + JSON.stringify(r));
          this.showToast("Error", r.msg);
        }
      })
      .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("文件过大");
        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);
        }
      })
      .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);
        }
      })
      .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;
    }
  }
 
  showToast(type, msg) {
    this.isShowSpinner = false;
    const event = new ShowToastEvent({
      title: type,
      variant: type,
      message: msg,
    });
    this.dispatchEvent(event);
  }
}