import { LightningElement, api, track } from 'lwc'; import searchContactInit from '@salesforce/apex/LexNewAndEditLeadPIPLController.searchContactInit'; import searchContactInitV2 from '@salesforce/apex/LexNewAndEditLeadPIPLController.searchContactInitV2'; import getObjectInfo from '@salesforce/apex/LexSearchLookupController.getObjectInfo'; import getContactAWS from '@salesforce/apex/LexNewAndEditLeadPIPLController.getContactAWS';//getNoPIContact import getNoPIContact from '@salesforce/apex/LexNewAndEditLeadPIPLController.getNoPIContact'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { AWSService } from 'c/piUtils'; const contactColumns = [ { label: "选择", fieldName: "sfRecordId", type: "button", initialWidth: 120, hideDefaultActions: true, typeAttributes: { label: "选择", name: "sfRecordId", size: 1, }, }, { label: "姓名", fieldName: "medicalStaffFullName", initialWidth: 120, hideDefaultActions: true, wrapText: true, }, { label: "客户名称", fieldName: "accountName", hideDefaultActions: true, wrapText: true, }, { label: "邮箱", fieldName: "email", hideDefaultActions: true, wrapText: true, }, { label: "电话", fieldName: "phone", hideDefaultActions: true, wrapText: true, }, { label: "手机号", fieldName: "mobilePhone", hideDefaultActions: true, wrapText: true, }, ]; export default class LexSearchLookupLWC extends LightningElement { @track isLoading = true; @api title; @api searchNameLabel; @api accountId = ''; @api provinceId; @api hospitalId; @api searchType = 'Contact' @api searchNameValue = ''; @track awsIdToContactMap = []; @track conAWSIds = []; @track contactStaticResource = ''; @track data = []; @track noPIContactList = []; @track columns = []; //查询字段数组 @api searchFieldArray = []; //查询where条件 @api searchWhere; //搜索条件字段 @api searchField; //查询对象api @api sobjectType; connectedCallback() { console.log('searchFieldArray = ' + this.searchFieldArray); console.log('searchField = ' + this.searchField); // if (this.searchFieldArray == null || this.searchField == null) { // this.showToast('Error', '初始化失败'); // return // } this.AWSService = new AWSService(); console.log('成功进入 searchLookupLwc'); console.log('this.accountId = ' + this.accountId); console.log('this.searchNameValue = ' + this.searchNameValue); console.log('this.sobjectType = ' + this.sobjectType); if (this.sobjectType == 'Contact') { this.columns = contactColumns; } else { this.columns = [ { label: "选择", fieldName: "id", type: "button", initialWidth: 120, hideDefaultActions: true, typeAttributes: { label: "选择", name: "id", size: 1, }, }, ] } this.searchContacts(); } searchContacts() { console.log('enter searchContact'); console.log('this.accountId:' + this.accountId); console.log('this.searchNameValue:' + this.searchNameValue); this.isLoading = true; let that = this; if (this.sobjectType == 'Contact') { this.data = []; if (this.accountId) { searchContactInit({ accountId: this.accountId + '', searchKeyWord: this.searchNameValue }).then((r) => { r = JSON.parse(JSON.stringify(r)); console.log('r = ' + JSON.stringify(r)) if (r.status == 'Success') { this.awsIdToContactMap = r.entity.awsIdToContactMap; this.conAWSIds = r.entity.conAWSIds; this.contactStaticResource = JSON.parse(r.entity.contactStaticResource); this.noPIContactList = r.entity.noPIContactList; console.log('this.awsIdToContactMap = ' + JSON.stringify(this.awsIdToContactMap)); console.log('this.conAWSIds = ' + JSON.stringify(this.conAWSIds)); console.log('this.contactStaticResource.searchUrl = ' + this.contactStaticResource.searchUrl); console.log('this.noPIContactList = ' + JSON.stringify(this.noPIContactList)); let contactAwsToAccountNameMap = {}; let contactAwsToIdMap = {}; for (var key in this.awsIdToContactMap) { console.log('key = ' + key) if (this.awsIdToContactMap[key] && this.awsIdToContactMap[key].Account && this.awsIdToContactMap[key].Account.Name) { contactAwsToAccountNameMap[key] = this.awsIdToContactMap[key].Account.Name; contactAwsToIdMap[key] = this.awsIdToContactMap[key].Id; } } console.log('contactAwsToAccountNameMap = ' + JSON.stringify(contactAwsToAccountNameMap)) console.log('contactAwsToIdMap = ' + JSON.stringify(contactAwsToIdMap)) console.log('contactAwsToAccountNameMap[959875214861139978] = ' + JSON.stringify(contactAwsToAccountNameMap['959875214861139978'])) this.data = []; var that = this; this.AWSService.search(this.contactStaticResource.searchUrl, { dataIds: this.conAWSIds, contactName: this.searchNameValue }, function (data) { console.log('data = ' + JSON.stringify(data)); if (data.object) { for (var i = 0; i < data.object.length; i++) { if (data.object[i].dataId) { let info = new Object(); info.sfRecordId = contactAwsToIdMap[data.object[i].dataId] info.accountName = contactAwsToAccountNameMap[data.object[i].dataId] info.medicalStaffFullName = data.object[i].lastName; info.email = data.object[i].email; info.phone = data.object[i].phone; info.mobilePhone = data.object[i].mobilePhone; info.AWSDataId = data.object[i].dataId; that.data.push(info); } } //非敏感人员 for (var i in that.noPIContactList) { if(!that.noPIContactList[i].AWS_Data_Id__c){ let info = new Object(); info.sfRecordId = that.noPIContactList[i].Id; info.accountName = that.noPIContactList[i].Account.Name; info.medicalStaffFullName = that.noPIContactList[i].Name; info.email = that.noPIContactList[i].Email; info.phone = that.noPIContactList[i].Phone; info.mobilePhone = that.noPIContactList[i].MobilePhone; that.data.push(info); } } that.data = [...that.data]; } }, this.contactStaticResource.token) this.isLoading = false; } else { this.showToast('Error', r.msg); } }) } else { getContactAWS().then((r) => { r = JSON.parse(JSON.stringify(r)); if (r.status == 'Success') { that.contactStaticResource = JSON.parse(r.entity.contactStaticResource); that.AWSService.search(that.contactStaticResource.searchUrl, { dataIds: [], contactName: that.searchNameValue }, function (data) { console.log('data = ' + JSON.stringify(data)); if(data.status == 0){ if (data.object) { //有值,获取所有的AWSId去salesforce里面查询出来 let conAWSIds = []; let contactAwsToContactMap = {}; for (var i = 0; i < data.object.length; i++) { if (data.object[i].dataId) { conAWSIds.push(data.object[i].dataId); contactAwsToContactMap[data.object[i].dataId] = data.object[i]; } } //去后台查询PI + noPI searchContactInitV2({ accountId: that.accountId + '', conAWSIds: conAWSIds, searchKeyWord: that.searchNameValue }).then((dataV2) => { dataV2 = JSON.parse(JSON.stringify(dataV2)); if(dataV2.status == 'Success'){ //PI for (var i = 0; i < dataV2.entity.conList.length; i++) { let info = new Object(); info.sfRecordId = dataV2.entity.conList[i].Id; info.accountName = dataV2.entity.conList[i].Account.Name; info.medicalStaffFullName = contactAwsToContactMap[dataV2.entity.conList[i].AWS_Data_Id__c].lastName; info.email = contactAwsToContactMap[dataV2.entity.conList[i].AWS_Data_Id__c].email; info.phone = contactAwsToContactMap[dataV2.entity.conList[i].AWS_Data_Id__c].phone; info.mobilePhone = contactAwsToContactMap[dataV2.entity.conList[i].AWS_Data_Id__c].mobilePhone; info.AWSDataId = dataV2.entity.conList[i].AWS_Data_Id__c; that.data.push(info); } //noPI for (var i = 0; i < dataV2.entity.noPIContactList.length; i++) { if(!dataV2.entity.noPIContactList[i].AWS_Data_Id__c){ let info = new Object(); info.sfRecordId = dataV2.entity.noPIContactList[i].Id; info.accountName = dataV2.entity.noPIContactList[i].Account.Name; info.medicalStaffFullName = dataV2.entity.noPIContactList[i].Name; info.email = dataV2.entity.noPIContactList[i].Email; info.phone = dataV2.entity.noPIContactList[i].Phone; info.mobilePhone = dataV2.entity.noPIContactList[i].MobilePhone; that.data.push(info); } } that.data = [...that.data]; }else { this.showToast('Error', r.msg); } }) } else { //查询noPI getNoPIContact({ searchContactName : that.searchNameValue, accountId: that.accountId }).then((dataV2)=>{ dataV2 = JSON.parse(JSON.stringify(dataV2)); if(dataV2.status == 'Success'){ for (var i = 0; i < dataV2.entity.noPIContactList.length; i++) { if(!dataV2.entity.noPIContactList[i].AWS_Data_Id__c){ let info = new Object(); info.sfRecordId = dataV2.entity.noPIContactList[i].Id; info.accountName = dataV2.entity.noPIContactList[i].Account.Name; info.medicalStaffFullName = dataV2.entity.noPIContactList[i].Name; info.email = dataV2.entity.noPIContactList[i].Email; info.phone = dataV2.entity.noPIContactList[i].Phone; info.mobilePhone = dataV2.entity.noPIContactList[i].MobilePhone; that.data.push(info); } } that.data = [...that.data]; }else { this.showToast('Error', r.msg); } }) } }else{ that.showToast('Error', 'AWS search Error'); } that.isLoading = false; }, that.contactStaticResource.token) } else { that.showToast('Error', r.msg); } }) } } else { this.searchLookupInfo(); } } searchLookupInfo() { getObjectInfo({ objectName: this.sobjectType, searchFieldArray: this.searchFieldArray, searchWhere: this.searchWhere, searchField: this.searchField, searchNameValue: this.searchNameValue }).then((r) => { r = JSON.parse(JSON.stringify(r)); console.log('r = ' + JSON.stringify(r)); console.log('this.searchFieldArray=====' + this.searchFieldArray); if (r.status == 'Success') { console.log('getObjectInfo success'); let fieldName = Object.keys(r.entity.fieldMapWithLabels); console.log('fieldName=====' + fieldName); //构建colums let fieldInfo = r.entity.fieldMapWithLabels; let tempList = []; fieldName.forEach(name => { let tempColumns = { label: fieldInfo[name], fieldName: name, initialWidth: 160, hideDefaultActions: true, wrapText: true, }; tempList.push(tempColumns); console.log('tempColumns=====' + JSON.stringify(tempColumns)); }) this.columns = [...this.columns, ...tempList]; this.data = r.entity.objectInfo; this.data.forEach(data => { if (data.Account) { data.Account = data.Account.Name; } let datakey = Object.keys(data); datakey.forEach(key => { if (key.includes('__r')) { console.log('data[key].Name:', data[key].Name); data[key] = data[key].Name; } }) }) console.log('data=====' + JSON.stringify(this.data)); this.isLoading = false; } else { this.showToast('Error', r.msg); } }) .catch(error => { console.log('error:', JSON.stringify(error)); }) } 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 "searchNameDataField": this.searchNameValue = value; break; } } closeModal() { this.dispatchEvent(new CustomEvent('close', {})); } searchHandleRowAction(event) { debugger let row = event.detail.row; console.log('row = ' + JSON.stringify(row)); if (this.sobjectType == 'Contact') { let sfRecordId = row.sfRecordId; let medicalStaffFullName = row.medicalStaffFullName; let AWSDataId = row.AWSDataId; let phone = row.phone; let email = row.email; this.dispatchEvent(new CustomEvent('selectcontact', { composed: true, bubbles: true, cancelable: true, detail: { data: { sfRecordId: sfRecordId, medicalStaffFullName: medicalStaffFullName, AWSDataId: AWSDataId, phone: phone, email: email } } })); } else { let Id = row.Id; let Name = row.Name; this.dispatchEvent(new CustomEvent('selectlookup', { composed: true, bubbles: true, cancelable: true, detail: { data: { Id: Id, Name: Name } } })); } } showToast(type, msg) { this.isLoading = false; const event = new ShowToastEvent({ title: type, variant: type, message: msg }); this.dispatchEvent(event); } }