import { LightningElement, api, track, wire } from 'lwc'; import { CurrentPageReference } from 'lightning/navigation'; import initData from '@salesforce/apex/LexNewAndEditConsumApplyPIPLController.initData'; import { NavigationMixin } from 'lightning/navigation'; import { AWSService } from 'c/piUtils'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import lexPIPLCustomStyle from '@salesforce/resourceUrl/lexPIPLCustomStyle' import { loadStyle } from 'lightning/platformResourceLoader'; import { decodeDefaultFieldValues } from "lightning/pageReferenceUtils"; import { getObjectInfo } from 'lightning/uiObjectInfoApi'; import CONSUM from '@salesforce/schema/Consum_Apply__c'; import { conmentCancel } from "c/lexPIPLConmentMethods"; // Add By Li Jun 20231025 Start import LexRecordNotCreateable from '@salesforce/label/c.LexRecordNotCreateable'; import LexNotCreateNote from '@salesforce/label/c.LexNotCreateNote'; const customLabel = { LexRecordNotCreateable, LexNotCreateNote } // Add By Li Jun 20231025 End export default class LexNewAndEditConsumApplyPIPL extends NavigationMixin(LightningElement) { sobjectType = 'Consum_Apply__c'; @track customLabel = customLabel; @api recordId; @track recordCloneId; @track recordTypeId = ''; @wire(CurrentPageReference) pageRef; @track recordData = {}; @track title; @track isLoading = false; @track layout = []; @track mode = ''; @track staticResource; @track staticResourceContact; @track staticResourcRentalApply; @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 = ''; @track loanerMedicalStaffId = ''; @track isEdit = false; @track departmentId = ''; @track contactAWSDataId = ''; @track saveAndNew = false; @track iconName = ""; @track phoneNumber = ''; @track directShippmentAddress = ''; AWSService; @track decryptContact = false; @track decryptRentalApply = false; @track rentalApplyAWSDataId = ''; @track modifyObj = {}; modifyArray = ['']; @track isInitSuccess = false; fieldLabel = {}; defaultValues = {}; @wire(getObjectInfo, { objectApiName: CONSUM }) getConsumInfo({data,error}){ if(data){ this.fieldLabel = data.fields; } } connectedCallback() { this.isLoading = true; console.log('enter connectedCallback'); loadStyle(this, lexPIPLCustomStyle); this.AWSService = new AWSService(); if (!this.recordId) { this.title = '新建耗材备品申请'; this.mode = 'new'; this.isNewMode = true; } if (this.recordId) { this.title = '编辑耗材备品申请'; this.mode = 'edit'; this.isEditMode = true; } if (this.pageRef && this.pageRef.state) { this.recordTypeId = this.pageRef.state.recordTypeId; console.log('this.recordTypeId = ' + this.recordTypeId); this.isCloneMode = Boolean(this.pageRef.state.c__isClone); //克隆 if (this.isCloneMode) { this.isNewMode = false; this.isEditMode = false; this.recordCloneId = this.pageRef.state.c__recordId; console.log('recordCloneId = ' + this.recordCloneId); this.title = '克隆耗材备品申请'; } if (this.pageRef.state.defaultFieldValues) { this.defaultValues = decodeDefaultFieldValues( this.pageRef.state.defaultFieldValues ); console.log('this.contactAWSDataId = ' + this.contactAWSDataId); } console.log("defaultValues:", JSON.stringify(this.defaultValues)); } console.log('recordId = ' + this.recordId + ' recordCloneId = ' + this.recordCloneId + ' recordTypeId = ' + this.recordTypeId + ' sobjectType = ' + this.sobjectType); let id = this.isCloneMode == true ? this.recordCloneId : this.recordId; debugger initData({ rid: id, recordTypeId: this.recordTypeId, sobjectType: this.sobjectType }).then((r) => { r = JSON.parse(JSON.stringify(r)); if (r.status == 'Success') { // if(r.entity.recordTypeId == '012000000000000AAA'){ // this.recordTypeId = null; // }else{ // this.recordTypeId = r.entity.recordTypeId; // } this.recordTypeId = null; this.AWSDataId = r.entity.AWSDataId; console.log('this.recordTypeId = ' + this.recordTypeId); let layout = JSON.parse(r.entity.layout); layout = this.sortMetaLayouts(layout); console.log('layout = ' + JSON.stringify(layout)); this.recordData = r.entity.data; console.log('this.recordData = ' + JSON.stringify(this.recordData)); if(this.recordData && this.recordData.Loaner_medical_Staff__c != null){ this.iconName = 'utility:close'; }else{ this.iconName = 'utility:search'; } let index = 1; this.sectionName = layout.map(s => s.label); debugger for (let s of layout) { s['showSection'] = false; if(s['editHeading'] || s['detailHeading']){ s['showSection'] = true; } s['showLabel'] = false; //add by xq 2023/07/20 start if(s.label == 'Information'){ s.label ='信息'; } //add by xq 2023/07/20 end for (let c of s.layoutColumns) { c['index'] = index++; if (c.layoutItems.length != 0 && s['showLabel'] == false) { s['showLabel'] = true; } 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; let fieldName = item['field']; if (this.isCloneMode) { item['value'] = this.recordData[fieldName]; } if (item['field'] == 'Loaner_medical_Staff__c') { item['isModify'] = true; item['isShowIcon'] = true; item.fieldLabel = '科室负责人'; if (this.isEditMode || this.isCloneMode) { item['value'] = this.recordData.Loaner_medical_Staff__r == null ? '' : this.recordData.Loaner_medical_Staff__r.LastName; this.loanerMedicalStaffId = this.recordData.Loaner_medical_Staff__c == null ? '' : this.recordData.Loaner_medical_Staff__c; this.contactAWSDataId = this.recordData.Loaner_medical_Staff__r == null ? '' : this.recordData.Loaner_medical_Staff__r.AWS_Data_Id__c; } } if (item['field'] == 'Account__c' && (this.isEditMode || this.isCloneMode)) { this.departmentId = this.recordData.Account__c; console.log('this.departmentId = ' + this.departmentId); } if (this.isNewMode && this.defaultValues) { debugger if(this.defaultValues[item["field"]]){ item["value"] = this.defaultValues[item["field"]]; if (item['field'] == 'Loaner_medical_Staff__c'){ item['value'] = this.defaultValues['loanerMedicalStaffName']; this.loanerMedicalStaffId = this.defaultValues['Loaner_medical_Staff__c'] this.iconName = "utility:close"; this.contactAWSDataId = this.defaultValues['rentalLoanerMedicalStaffAWSDataId']; this.decryptContact = true; } if (item['field'] == 'Account__c'){ this.departmentId = this.defaultValues[item["field"]]; } } if(this.defaultValues['rentalApplyAWSDataId'] != ''){ this.rentalApplyAWSDataId = this.defaultValues['rentalApplyAWSDataId']; this.decryptRentalApply = true; } } if((this.isNewMode || this.isCloneMode) && item['field'] == 'Name'){ item['value'] = '*'; } } } } } this.layout = layout; this.AWSToSobjectMap = JSON.parse(JSON.stringify(r.entity.AWSToSobjectNonEncryptedMap)); this.staticResource = JSON.parse(r.entity.staticResource); this.staticResourceContact = JSON.parse(r.entity.staticResourceContact); this.staticResourcRentalApply = JSON.parse(r.entity.staticResourcRentalApply); if (r.entity.isEdit != null) this.isEdit = r.entity.isEdit console.log('this.isEdit = ' + this.isEdit); //编辑 if (this.isEditMode || this.isCloneMode) { //解密客户人员 this.queryContactName(); //解密加密字段 this.querySobjectFromAWS(); } if(this.decryptContact){ //解密客户人员 this.queryContactName(); } if(this.decryptRentalApply){ //解密加密字段 this.queryRentalApplyFromAWS(); } this.isInitSuccess = true; } else { //this.showToast('Error', r.msg); this.showMyToast(r.msg , '' , 'error') } }) } queryRentalApplyFromAWS(){ var that = this; debugger; this.AWSService.query(this.staticResourcRentalApply.queryUrl,this.rentalApplyAWSDataId,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) { if(c['field'] == 'Phone_number__c'){ c['value'] = data.object['phoneNumber'] == null ? '' : data.object['phoneNumber']; } if(c['field'] == 'direct_shippment_address__c'){ c['value'] = data.object['directShippmentAddress'] == null ? '' : data.object['directShippmentAddress']; } } } } } } },this.staticResourcRentalApply.token) } sortMetaLayouts(layout){ layout.forEach(s => { let c1 = (s.layoutColumns[0].layoutItems != null) ? s.layoutColumns[0].layoutItems : []; let c2 = []; if(s.layoutColumns.length > 1){ c2 = (s.layoutColumns[1].layoutItems != null) ? s.layoutColumns[1].layoutItems : []; } else{ s.layoutColumns.push({ "reserved": null, "layoutItems": [] }); } let emptySpaceItem = { "width": null, "showScrollbars": null, "showLabel": null, "scontrol": null, "reportChartComponent": null, "page_x": null, "height": null, "field": null, "emptySpace": true, "customLink": null, "component": null, "canvas": null, "behavior": null, "analyticsCloudComponent": null } let maxSize = 0 if(c1.length > c2.length){ maxSize = c1.length; for(let i = 0; i < maxSize; i++){ if(c2.length == maxSize){ break; } c2.push(emptySpaceItem); } }else{ maxSize = c2.length; for(let i = 0; i < maxSize; i++){ if(c1.length == maxSize){ break; } c1.push(emptySpaceItem); } } let tempList = []; let index = 0; for(let i = 0; i < maxSize; i++){ tempList.push(c1[i]); if(tempList.length == maxSize){ s.layoutColumns[index].layoutItems = tempList; tempList = []; tempList.push(c2[i]); index++; }else{ tempList.push(c2[i]); if(tempList.length == maxSize){ s.layoutColumns[index].layoutItems = tempList; tempList = []; index++; } } } }) layout = [...layout]; return layout; } searchHospitalNameModal(event) { let fieldName = event.target.getAttribute("data-field"); console.log('fieldName = ' + fieldName) console.log('fieldName = ' + typeof fieldName) if (fieldName == 'Loaner_medical_Staff__c') { if (this.departmentId == "000000000000000" || this.departmentId == '') { //this.showToast('Error', '请先输入战略科室分类!'); this.showMyToast('请先输入战略科室分类!' , '' , 'error') return } this.lostFocus(event); this.isShowSearchAccount = true; } } //失去焦点 lostFocus(event){ // 获取data-*属性值,即要失去焦点的元素的标识符 const elementToBlur = event.target.dataset.elementToBlur; // 选择要失去焦点的元素 const element = this.template.querySelector(`[data-element-to-blur="${elementToBlur}"]`); if(element) element.blur(); } //解密客户人员 queryContactName() { let that = this; this.AWSService.query(this.staticResourceContact.queryUrl, this.contactAWSDataId, function (data) { console.log('data = ' + JSON.stringify(data)); if (data.object) { debugger for (let s of that.layout) { for (let c of s.layoutColumns) { if (c.layoutItems) { for (let item of c.layoutItems) { if (item['field'] == 'Loaner_medical_Staff__c') { item['value'] = data.object.lastName; } } } } } } }, this.staticResourceContact.token) } handleLoad() { console.log('handleLoad') console.log('this.isInitSuccess = ' + this.isInitSuccess) if (this.isInitSuccess) { let time = 1000; if (!this.isNewMode) { time = 2000; } let that = this; setTimeout(() => { that.isLoading = false; }, time); } } querySobjectFromAWS() { let that = this; debugger 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(data.object.phoneNumber) this.phoneNumber = data.object.phoneNumber; if(data.object.directShippmentAddress) this.directShippmentAddress = data.object.directShippmentAddress; console.log('this.phoneNumber = ' + this.phoneNumber); console.log('this.directShippmentAddress = ' + this.directShippmentAddress); } }, this.staticResource.token) } dataChange(event) { let fieldName = event.target.getAttribute("data-field"); let value = event.detail.value; console.log("fieldName = " + fieldName + " value = " + value); switch (fieldName) { case "Account__c": this.departmentId = value; console.log('this.departmentId = ' + this.departmentId) break; } } clickSubmit() { console.log('clickSubmit'); const btn = this.template.querySelector(".hidden"); if (btn) { btn.click(); } } clickSubmitAndNew(){ console.log('clickSubmitAndNew'); this.saveAndNew = true; const btn = this.template.querySelector(".hidden"); if (btn) { btn.click(); } } handleSubmit(event) { this.isLoading = 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); fields['Loaner_medical_Staff__c'] = this.loanerMedicalStaffId; console.log('fields = ' + JSON.stringify(fields)); //2. select cannot actively select redaction option let validationResultMessage = this.validateFieldValueFormate(fields); console.log(validationResultMessage); if (validationResultMessage) { //this.showToast('Error', validationResultMessage); this.showMyToast(validationResultMessage , '' , 'error') this.isLoading = false; return } //3. Check Required Field let checkRequiredFieldMsgResult = this.checkRequiredFieldMsg(fields); console.log('checkRequiredFieldMsgResult = ' + checkRequiredFieldMsgResult); if (checkRequiredFieldMsgResult != '') { //this.showToast('Error', checkRequiredFieldMsgResult + '需要进行填写'); this.showMyToast(checkRequiredFieldMsgResult + '需要进行填写' , '' , 'error') this.isLoading = false; return } //4. Prepare the payload for New PI API To AWS - To Do let payloadForNewPI = this.getPIPayload(fields); console.log('payloadForNewPI = ' + payloadForNewPI); //zhj MEBG新方案改造 2022-11-29 start if (this.isEdit && (fields.Phone_number__c != this.phoneNumber || fields.direct_shippment_address__c != this.directShippmentAddress)) { this.showMyToast('申请单批准后,不能编辑耗材备品借用信息','','error'); this.isLoading = false; return } //zhj MEBG新方案改造 2022-11-29 end debugger //5. PI To AWS //新建/克隆 if (this.isNewMode || this.isCloneMode || this.AWSDataId == '') { this.NewPIToAWS(payloadForNewPI, fields); return } //编辑 if (this.isEditMode) { this.UpdatePIToAWS(payloadForNewPI, fields); } } //验证字段 validateFieldValueFormate(fields) { let error_msg = ''; let b = false; for (let 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 += ";" + this.fieldLabel[c.field].label ; } } } } } } catch (err) { //this.showToast('Error', err.message); this.showMyToast(err.message , '' , 'error') } 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) { let that = this; this.AWSService.post(this.staticResource.newUrl, payloadForNewPI, (result) => { if (result && result.object) { console.log('result = ' + JSON.stringify(result)); for (let f in that.AWSToSobjectMap) { if (result.object[0].hasOwnProperty(f)) { fields[that.AWSToSobjectMap[f]] = result.object[0][f]; console.log('that.AWSToSobjectMap[f] = ' + that.AWSToSobjectMap[f]); console.log('fields[that.AWSToSobjectMap[f]] = ' + fields[that.AWSToSobjectMap[f]]); } else { console.log(f + 'is not in result.object[0]'); } } fields['AWS_Data_Id__c'] = result.object[0].dataId; that.txId = result.txId; console.log('fields = ' + JSON.stringify(fields)) debugger that.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); if(this.AWSDataId){ obj[0].dataId = this.AWSDataId; }else{ this.showMyToast("Error", '未找到该记录的AWS Id,请确认记录完整性', 'error'); return; } obj[0].dataId = this.AWSDataId; let payloadForNewPIJson = JSON.stringify(obj); let that = this; this.AWSService.post(this.staticResource.updateUrl, payloadForNewPIJson, (result) => { if (result && result.object) { console.log('result = ' + JSON.stringify(result)); for (let f in that.AWSToSobjectMap) { if (result.object[0].hasOwnProperty(f)) { fields[that.AWSToSobjectMap[f]] = result.object[0][f]; console.log('that.AWSToSobjectMap[f] = ' + that.AWSToSobjectMap[f]); console.log('fields[that.AWSToSobjectMap[f]] = ' + fields[that.AWSToSobjectMap[f]]); } else { console.log(f + 'is not in result.object[0]'); } } fields['AWS_Data_Id__c'] = that.AWSDataId; that.txId = result.txId; console.log('that.txId = ' + that.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)); that.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) if(that.saveAndNew){ console.log('that.recordTypeId = ' + that.recordTypeId); that[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: that.sobjectType, actionName: 'new', }, state: { recordTypeId: that.recordTypeId } }); }else{ that[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { actionName: "view", recordId: updatedRecord, objectApiName: that.sobjectType } }); } }); } //提交保存失败 handleError(event) { event.preventDefault(); event.stopImmediatePropagation(); console.log('event.detail = ' + JSON.stringify(event.detail)) //字段级别的报错 if (event.detail.output && event.detail.output.fieldErrors && Object.keys(event.detail.output.fieldErrors).length != 0) { let msg = ''; for (let key in event.detail.output.fieldErrors) { msg += ',' + event.detail.output.fieldErrors[key][0].message; } msg = msg.substring(1); this.showMyToast(event.detail.message + ' : ' + msg, '', 'error'); } else { //add by Li Jun 20231025 Start if(event.detail.message != '' && this.customLabel.LexRecordNotCreateable.indexOf(event.detail.message) != -1){ this.showMyToast("Error", this.customLabel.LexNotCreateNote, 'error'); }else{ this.showMyToast("Error", event.detail.message + ' : ' + event.detail.detail, 'error'); } //add by Li Jun 20231025 End } //add by Li Jun 20231025 Start if(this.txId && this.staticResource){ this.AWSService.confirm(false,'',this.txId,this.staticResource.token,this.staticResource.transactionUrl,function(result){ console.log('result = ' + JSON.stringify(result)) }); } //add by Li Jun 20231025 End this.isLoading = false; } //取消 cancel() { this.template.querySelector('c-common-toast').closeModel(); conmentCancel(this.defaultValues); } //标准新建页面跳转 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.isLoading = false; const event = new ShowToastEvent({ title: msg, variant: type, // message: msg }); this.dispatchEvent(event); } //关闭客户人员根据医院模态框 closeHospitalNameModal() { this.isShowSearchAccount = false; this.template.querySelector(".slds-modal").style = ""; } //选择客户人员后进行赋值 handleSelectContact(event) { this.isLoading = true; console.log('enter handleSelectContact '); const selectContact = event.detail; console.log('selectContact = ' + JSON.stringify(selectContact)) for (let s of this.layout) { for (let c of s.layoutColumns) { if (c.layoutItems) { for (let item of c.layoutItems) { //科室负责人 if (item['field'] == 'Loaner_medical_Staff__c') { item['value'] = selectContact.data.medicalStaffFullName; this.loanerMedicalStaffId = selectContact.data.sfRecordId; console.log('selectContact.data.sfRecordId = ' + selectContact.data.sfRecordId); console.log('this.loanerMedicalStaffId = ' + this.loanerMedicalStaffId); } } } } } this.layout = [...this.layout]; this.isLoading = false; this.closeHospitalNameModal(); this.iconName = "utility:close"; } //清空客户姓名 clearName() { console.log("enter clearName"); if(this.loanerMedicalStaffId != ''){ for (let s of this.layout) { for (let c of s.layoutColumns) { if (c.layoutItems) { for (let item of c.layoutItems) { //科室负责人 if (item['field'] == 'Loaner_medical_Staff__c') { item['value'] = ''; this.loanerMedicalStaffId = ''; } } } } } } this.layout = [...this.layout]; this.iconName = "utility:search"; } //显示信息 showMyToast(title, message, variant) { console.log('show custom message'); let iconName = ''; let content = ''; if (variant == 'success') { iconName = 'utility:check'; } else { iconName = 'utility:error'; } if (message != '') { content = '

' + title + '

' + message + '
'; } else { content = '

' + title + '

'; } this.template .querySelector('c-common-toast') .showToast(variant, content, iconName, 10000); this.isLoading = false; } }