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