binxie
2023-06-26 8c8f51f10bb9e158cb8b89cb8d58e4ce7d467583
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
import { LightningElement, api, track, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
import initData from '@salesforce/apex/LexNewAndEditContactPIPLController.initData';
import { NavigationMixin } from 'lightning/navigation';
import { AWSService } from 'c/piUtils';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 
export default class LexNewAndEditContactPIPL extends NavigationMixin(LightningElement) {
    sobjectType = 'Contact';
 
    @api recordId;
    @track recordTypeId = '';
    @wire(CurrentPageReference) pageRef;
    @track recordData = {};
    @track title = '';
    @track isShowSpinner = true;
    @track layout = [];
    @track isDoctor = true;
 
 
    @track staticResource;
    @track staticResourceContact;
    @track piFieldsMap;
    @track abstractData = '';
    @track data = {};
    @track piplData = {};
    @track isNewMode = false;
    @track isCloneMode = false;
    @track isEditMode = false;
    @track sectionName = [];
    @track AWSToSobjectMap = {};
    @track AWSDataId = '';
    @track txId = '';
    @track isShowSearchAccount = false;
    @track hospitalId = '';
    @track contactId = '';
    AWSService;
 
    @track modifyObj = {};
    modifyArray = ['LastName'];
 
    connectedCallback() {
        console.log('enter connectedCallback');
        this.AWSService = new AWSService();
        if (!this.recordId || this.isCloneMode) {
            this.title = '新建客户人员';
            this.isNewMode = true;
        }
        if (this.recordId) {
            this.title = '编辑客户人员';
            this.isEditMode = true;
        }
        if (this.pageRef && this.pageRef.state) {
            this.recordTypeId = this.pageRef.state.recordTypeId;
            console.log('this.recordTypeId = ' + this.recordTypeId);
        }
        console.log('recordId = ' + this.recordId + ' recordTypeId = ' + this.recordTypeId + ' sobjectType = ' + this.sobjectType);
        initData({
            rid: this.recordId,
            recordTypeId: this.recordTypeId,
            sobjectType: this.sobjectType
        }).then((r) => {
            r = JSON.parse(JSON.stringify(r));
            if (r.status == 'Success') {
                this.isDoctor = r.entity.isDoctor;
                console.log('this.isDoctor = ' + this.isDoctor);
                if(this.isEditMode){
                    this.recordTypeId = r.entity.recordTypeId;
                }
                console.log('this.recordTypeId = ' + this.recordTypeId);
                debugger
                //如果不是医院类型,使用标准页面
                if (!this.isDoctor) {
                    if(this.isNewMode)
                        this.RedirectStandardNewPage();
                    if(this.isEditMode)
                        this.RedirectStandardEditPage();
                    return
                }
                this.AWSDataId = r.entity.AWSDataId;
                let layout = JSON.parse(r.entity.layout);
                console.log('layout = ' + JSON.stringify(layout));
                console.log('layout = ' + typeof layout);
 
                let index = 1;
                this.sectionName = layout.map(s => s.label);
                for (let s of layout) {
                    for (let c of s.layoutColumns) {
                        c['index'] = index++;
                        if (c.layoutItems) {
                            for (let item of c.layoutItems) {
                                item['isDisable'] = item.behavior == 'Readonly' ? true : false;
                                item['isRequired'] = item.behavior == 'Required' ? true : false;
                                item['isModify'] = false;
                                if (item['field'] == 'Name') {
                                    item.field = 'LastName';
                                    item.fieldLabel = '姓名'
                                    item['isModify'] = true;
                                    if (this.isEditMode) {
                                        item['value'] = this.recordData.LastName
                                    }
                                }
                            }
                        }
                    }
                }
                this.layout = layout;
                console.log('this.layout = ' + JSON.stringify(this.layout));
                this.AWSToSobjectMap = JSON.parse(JSON.stringify(r.entity.AWSToSobjectNonEncryptedMap));
                this.staticResource = JSON.parse(r.entity.staticResource);
                
 
                //编辑
                if (this.isEditMode) {
                    //解密客户的加密字段
                    this.querySobjectFromAWS();
                }
                this.isShowSpinner = false;
            } else {
                this.showToast('Error', r.msg);
            }
        })
    }
 
    querySobjectFromAWS() {
        debugger
        var that = this;
        this.AWSService.query(this.staticResource.queryUrl, this.AWSDataId, function (data) {
            console.log('queryLeadFromAWSIFS data = ' + JSON.stringify(data));
            if (data.object) {
                for (let s of that.layout) {
                    for (let lc of s.layoutColumns) {
                        if(lc.layoutItems){
                            for (let c of lc.layoutItems) {
                                for (let f in that.AWSToSobjectMap) {
                                    if (data.object.hasOwnProperty(f) && c['field'] == that.AWSToSobjectMap[f]) {
                                        c['value'] = data.object[f] == null ? '' : data.object[f];
                                        if (c['field'] == 'LastName') {
                                            that.modifyObj['LastName'] = data.object[f] == null ? '' : data.object[f];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // for (var s of that.layout) {
                //     for (var c of s.layoutFields) {
                //         for (let f in that.AWSToSobjectMap) {
                //             if (data.object.hasOwnProperty(f) && c['fieldAPI'] == that.AWSToSobjectMap[f]) {
                //                 c['value'] = data.object[f] == null ? '' : data.object[f];
                //                 if (c['fieldAPI'] == 'LastName') {
                //                     that.modifyObj['LastName'] = data.object[f] == null ? '' : data.object[f];
                //                 }
                //             }
                //         }
                //     }
                // }
            }
        }, this.staticResource.token)
    }
 
 
    dataChange(event) {
        let fieldName = event.target.getAttribute("data-field");
        let value = event.detail.value;
        console.log("fieldName = " + fieldName + " value = " + event.detail.value);
        switch (fieldName) {
            case "LastName":
                this.modifyObj[fieldName] = value;
                break;
        }
    }
 
    handleSubmit(event) {
        this.isShowSpinner = true;
        //1. Get Sobject Information from Form
        console.log('handleSubmit');
        event.preventDefault();
        const fields = event.detail.fields;
        console.log('this.modifyObj = ' + JSON.stringify(this.modifyObj));
        Object.assign(fields, this.modifyObj);
        console.log('fields = ' + JSON.stringify(fields));
 
        debugger
        //2. select cannot actively select redaction option
        let validationResultMessage = this.validateFieldValueFormate(fields);
        console.log(validationResultMessage);
        if (validationResultMessage) {
            this.showToast('Error', validationResultMessage);
            return
        }
 
        //3. Check Required Field
        let checkRequiredFieldMsgResult = this.checkRequiredFieldMsg(fields);
        console.log('checkRequiredFieldMsgResult = ' + checkRequiredFieldMsgResult);
        if (checkRequiredFieldMsgResult != '') {
            this.showToast('Error', checkRequiredFieldMsgResult + '需要进行填写');
            return
        }
 
        //4. Prepare the payload for New PI API To AWS - To Do
        let payloadForNewPI = this.getPIPayload(fields);
        console.log('payloadForNewPI = ' + payloadForNewPI);
 
        //5. PI To AWS
        //新建
        if (this.isNewMode) {
            this.NewPIToAWS(payloadForNewPI, fields);
        }
        //编辑
        if (this.isEditMode) {
            this.UpdatePIToAWS(payloadForNewPI, fields);
        }
    }
 
    //验证字段
    validateFieldValueFormate(fields) {
        let error_msg = '';
        let b = false;
        for (var key in fields) {
            if (fields[key] == "*****")
                b = true;
        }
        if (b)
            error_msg = '下拉框不能主动选择密文选项';
        return error_msg;
    }
 
    //验证required字段需要进行填写
    checkRequiredFieldMsg(fields) {
        let msg = '';
        try {
            for (let s of this.layout) {
                for (let lc of s.layoutColumns) {
                    if (lc.layoutItems) {
                        for (let c of lc.layoutItems) {
                            if (!c.isDisable && c.isRequired && (fields[c.field] == null || fields[c.field] == '')) {
                                msg += ';' + c.fieldLabel;
                            }
                        }
                    }
                }
            }
        } catch (err) {
            this.showToast('Error',err.message);
        }
        msg = msg.substring(1);
        return msg;
    }
 
    //获取PI字段
    getPIPayload(sobjJsonLwc) {
        console.log()
        let leadPayloadList = [];
        let leadPIData = {};
 
        for (let f in this.AWSToSobjectMap) {
            if (sobjJsonLwc.hasOwnProperty(this.AWSToSobjectMap[f])) {
                leadPIData[f] = sobjJsonLwc[this.AWSToSobjectMap[f]]
            }
            else {
                console.log(this.AWSToSobjectMap[f] + 'is not in sobjJsonLwc');
            }
        }
 
        leadPIData.medicalStaffFullName = leadPIData.lastName;
        leadPIData.sfRecordId = '';
        console.log('Sobject PI Data x :' + leadPIData);
        leadPayloadList.push(leadPIData);
        console.log('leadPayloadList = ' + JSON.stringify(leadPayloadList));
        return JSON.stringify(leadPayloadList);
    }
 
    //新建保存ToAWS
    NewPIToAWS(payloadForNewPI, fields) {
        this.AWSService.post(this.staticResource.newUrl, payloadForNewPI, (result) => {
            if (result && result.object) {
                console.log('result = ' + JSON.stringify(result));
                for (let f in this.AWSToSobjectMap) {
                    if (result.object[0].hasOwnProperty(f)) {
                        fields[this.AWSToSobjectMap[f]] = result.object[0][f];
                        console.log('this.AWSToSobjectMap[f] = ' + this.AWSToSobjectMap[f]);
                        console.log('fields[this.AWSToSobjectMap[f]] = ' + fields[this.AWSToSobjectMap[f]]);
                    }
                    else {
                        console.log(f + 'is not in result.object[0]');
                    }
                }
                fields['AWS_Data_Id__c'] = result.object[0].dataId;
                this.txId = result.txId;
                //保存到后端
                this.template.querySelector('lightning-record-edit-form').submit(fields);
            } else {
                console.log('result = ' + JSON.stringify(result));
            }
        }, this.staticResource.token);
    }
 
    //编辑保存ToAWS
    UpdatePIToAWS(payloadForNewPI, fields) {
        let obj = JSON.parse(payloadForNewPI);
        obj[0].dataId = this.AWSDataId;
        let payloadForNewPIJson = JSON.stringify(obj);
        this.AWSService.post(this.staticResource.updateUrl, payloadForNewPIJson, (result) => {
            if (result && result.object) {
                console.log('result = ' + JSON.stringify(result));
                for (let f in this.AWSToSobjectMap) {
                    if (result.object[0].hasOwnProperty(f)) {
                        fields[this.AWSToSobjectMap[f]] = result.object[0][f];
                        console.log('this.AWSToSobjectMap[f] = ' + this.AWSToSobjectMap[f]);
                        console.log('fields[this.AWSToSobjectMap[f]] = ' + fields[this.AWSToSobjectMap[f]]);
                    }
                    else {
                        console.log(f + 'is not in result.object[0]');
                    }
                }
                fields['AWS_Data_Id__c'] = this.AWSDataId;
                this.txId = result.txId;
                console.log('this.txId = ' + this.txId);
                console.log('PI After fields = ' + JSON.stringify(fields));
                if (fields.RecordTypeId) {
                    fields['RecordTypeId'] = fields.RecordTypeId.substring(1, fields.RecordTypeId.length - 1);
                }
                //保存到后端
                console.log('update submit = ' + JSON.stringify(fields));
                this.template.querySelector('lightning-record-edit-form').submit(fields);
            } else {
                console.log('result = ' + JSON.stringify(result));
            }
        }, this.staticResource.token);
    }
 
    //提交保存成功
    handleSuccess(event) {
        let updatedRecord = event.detail.id;
        console.log('onsuccess: ', updatedRecord);
        console.log('confirmTrans');
        let that = this;
        this.AWSService.confirm(true, updatedRecord, this.txId, this.staticResource.token, this.staticResource.transactionUrl, function (result) {
            console.log('result = ' + JSON.stringify(result))
            that.showToast('Success', '保存成功');
            console.log('updatedRecord = ' + updatedRecord)
            that[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    actionName: "view",
                    recordId: updatedRecord,
                    objectApiName: that.sobjectType
                }
            });
        });
    }
 
    //提交保存失败
    handleError(event) {
        event.preventDefault();
        event.stopImmediatePropagation();
        this.showToast("Error", event.detail.detail);
        this.AWSService.confirm(false, '', this.txId, this.staticResource.token, this.staticResource.transactionUrl, function (result) {
            console.log('result = ' + JSON.stringify(result))
        });
    }
 
    //change事件
    dataChange(event) {
        let fieldName = event.target.getAttribute("data-field");
        let value = event.detail.value;
        console.log("fieldName = " + fieldName + " value = " + event.detail.value);
        if (this.modifyArray.indexOf(fieldName) != -1) {
            switch (fieldName) {
                case "LastName":
                    this.modifyObj[fieldName] = value;
                    break;
            }
        }
    }
 
    //取消
    cancel() {
        console.log('cancel');
        window.history.back();
    }
 
    //标准新建页面跳转
    RedirectStandardNewPage() {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Contact',
                actionName: 'new'
            },
            state: {
                nooverride: '1',
                recordTypeId: this.recordTypeId
            }
        });
    }
 
    //标准编辑页面跳转
    RedirectStandardEditPage() {
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                objectApiName: 'Contact',
                recordId: this.recordId,
                actionName: 'edit',
            },
            state: {
                nooverride: '1',
            }
        });
    }
 
    //显示信息
    showToast(type, msg) {
        this.isShowSpinner = false;
        const event = new ShowToastEvent({
            title: type,
            variant: type,
            message: msg
        });
        this.dispatchEvent(event);
    }
}