import { LightningElement, api, track, wire } from 'lwc';
|
import { CurrentPageReference } from 'lightning/navigation';
|
import initData from '@salesforce/apex/LexNewAndEditLeadPIPLController.initData';
|
import queryAccount from '@salesforce/apex/LexNewAndEditLeadPIPLController.queryAccount';
|
import { NavigationMixin } from 'lightning/navigation';
|
import { AWSService } from 'c/piUtils';
|
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
|
|
export default class LexNewAndEditLeadPIPL extends NavigationMixin(LightningElement) {
|
sobjectType = 'Lead';
|
|
@api recordId;
|
@wire(CurrentPageReference) pageRef;
|
@track recordData = {};
|
@track title;
|
@track recordTypeId = '';
|
@track isShowSpinner = true;
|
@track layout = [];
|
@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 contactAWSDataId = '';
|
@track txId = '';
|
@track isShowSearchAccount = false;
|
@track hospitalId = '';
|
@track contactId = '';
|
AWSService;
|
|
//姓名
|
//@track lastName = '';
|
|
@track modifyObj = {};
|
modifyArray = ['LastName', 'Hospital_Name__c'];
|
|
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 + ' pid = ' + this.pid + ' 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') {
|
let layout = JSON.parse(r.entity.layout);
|
console.log('layout = ' + JSON.stringify(layout));
|
this.layout = layout;
|
this.recordData = r.entity.data;
|
console.log('this.recordData = ' + JSON.stringify(this.recordData));
|
this.AWSDataId = r.entity.AWSDataId;
|
for (var s of layout) {
|
this.sectionName.push(s.name);
|
for (var c of s.layoutFields) {
|
c['isModify'] = false;
|
c['isDisable'] = !c.editableField;
|
//名字,只留LastName
|
if(c['fieldAPI'] == 'Name') {
|
c.fieldAPI = 'LastName';
|
c.fieldLabel = '姓名'
|
c['isModify'] = true;
|
if(this.isEditMode){
|
c['value'] = this.recordData.LastName
|
}
|
}
|
//客户人员名,因为要进行联动和解密所以变为普通input,自己控制值
|
if(c['fieldAPI'] == 'Contact_Name__c'){
|
c['isModify'] = true;
|
c['isShowIcon'] = true;
|
if(this.isEditMode){
|
c['value'] = this.recordData.Contact_Name__r == null ? '' : this.recordData.Contact_Name__r.LastName;
|
this.contactId = this.recordData.Contact_Name__c == null ? '' : this.recordData.Contact_Name__c;
|
this.contactAWSDataId = this.recordData.Contact_Name__r == null ? '' : this.recordData.Contact_Name__r.AWS_Data_Id__c;
|
}
|
}
|
if(c['fieldAPI'] == 'Hospital_Name__c' && this.isEditMode){
|
this.hospitalId = this.recordData.Hospital_Name__r == null ? '' : this.recordData.Hospital_Name__r.Id;
|
}
|
}
|
}
|
|
this.AWSToSobjectMap = JSON.parse(JSON.stringify(r.entity.AWSToSobjectNonEncryptedMap));
|
this.staticResource = JSON.parse(r.entity.staticResource);
|
this.staticResourceContact = JSON.parse(r.entity.staticResourceContact);
|
this.recordTypeId = r.entity.recordTypeId;
|
//编辑
|
if(this.isEditMode){
|
//解密客户人员
|
this.queryContactName();
|
//解密意向的加密字段
|
this.queryLeadFromAWSIFS();
|
}
|
this.isShowSpinner = false;
|
} else {
|
this.isShowSpinner = false;
|
this.showToast('Error', r.Msg);
|
}
|
})
|
}
|
|
//解密客户人员
|
queryContactName(){
|
var that = this;
|
this.AWSService.query(this.staticResourceContact.queryUrl,this.contactAWSDataId,function(data){
|
console.log('data = ' + JSON.stringify(data));
|
if (data.object) {
|
for (var s of that.layout) {
|
for (var c of s.layoutFields) {
|
if(c['fieldAPI'] == 'Contact_Name__c'){
|
c['value'] = data.object.lastName;
|
}
|
}
|
}
|
}
|
},this.staticResourceContact.token)
|
}
|
|
//解密意向的加密字段
|
queryLeadFromAWSIFS(){
|
var that = this;
|
debugger
|
this.AWSService.query(this.staticResource.queryUrl,this.AWSDataId,function(data){
|
console.log('queryLeadFromAWSIFS data = ' + JSON.stringify(data));
|
if (data.object) {
|
for (var s of that.layout) {
|
for (var c of s.layoutFields) {
|
for (let f in that.AWSToSobjectMap) {
|
debugger
|
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];
|
}
|
}
|
}
|
// if(c['fieldAPI'] == 'LastName'){
|
// console.log('data.object.lastName = ' + data.object.lastName);
|
// c['value'] = data.object.lastName == null ? '' : data.object.lastName;
|
// that.modifyObj['LastName'] = data.object.lastName == null ? '' : data.object.lastName;
|
// console.log('this.modifyObj[LastName] = ' + that.modifyObj['LastName']);
|
// }
|
// if(c['fieldAPI'] == 'Phone'){
|
// console.log('data.object.phone = ' + data.object.phone);
|
// c['value'] = data.object.phone == null ? '' : data.object.phone;
|
// }
|
// if(c['fieldAPI'] == 'Email'){
|
// console.log('data.object.email = ' + data.object.email);
|
// c['value'] = data.object.email == null ? '' : data.object.email;
|
// }
|
}
|
}
|
}
|
},this.staticResource.token)
|
}
|
|
//取消
|
cancel() {
|
console.log('cancel');
|
window.history.back();
|
}
|
|
//change事件
|
dataChange(event) {
|
let fieldName = event.target.getAttribute("data-field");
|
let value = event.detail.value;
|
console.log("fieldName = " + fieldName + " value = " + event.detail.value);
|
for (var s of this.layout) {
|
for (var c of s.layoutFields) {
|
if(c.fieldAPI == fieldName){
|
console.log('c.fieldAPI = ' + c.fieldAPI);
|
c['value'] = value;
|
}
|
}
|
}
|
if(this.modifyArray.indexOf(fieldName) != -1){
|
switch(fieldName){
|
case "LastName":
|
this.modifyObj[fieldName] = value;
|
break;
|
case "Hospital_Name__c":
|
//需要给战略科室分类和公司赋值
|
if(value != "000000000000000" && value != ''){
|
let ls = ['Department_Class__c'];
|
this.hospitalId = value;
|
this.setVlookup(ls,value+'');
|
}else{
|
//清空战略科室分类
|
this.clearVlookup();
|
}
|
break;
|
}
|
|
}
|
}
|
|
//战略科室分类和公司赋值
|
setVlookup(ls,hospitalId){
|
this.isShowSpinner = true;
|
console.log('ls = ' + JSON.stringify(ls));
|
console.log('hospitalId = ' + hospitalId);
|
queryAccount({
|
accountTypes : JSON.stringify(ls),
|
accountId : hospitalId
|
}).then((r) => {
|
r = JSON.parse(JSON.stringify(r));
|
console.log('r = ' + JSON.stringify(r));
|
if (r.status == 'Success') {
|
console.log('queryAccount success');
|
|
for (var s of this.layout) {
|
for (var c of s.layoutFields) {
|
if(c['fieldAPI'] == 'Department_Class__c'){
|
console.log('m = ' + JSON.stringify(r.entity.m))
|
if(JSON.stringify(r.entity.m) != '{}'){
|
c['value'] = r.entity.m.Department_Class__c.Id;
|
}
|
}
|
if(c['fieldAPI'] == 'Company'){
|
c['value'] = r.entity.account.Name;
|
}
|
}
|
}
|
//1.5秒后将会调用执行remind()函数
|
var that = this;
|
setTimeout(function() { that.isShowSpinner = false; }, 1500);
|
//this.isShowSpinner = false;
|
} else {
|
this.showToast('Error', r.Msg);
|
}
|
})
|
}
|
|
//清空战略科室分类
|
clearVlookup(){
|
console.log('clearVlookup')
|
for (var s of this.layout) {
|
for (var c of s.layoutFields) {
|
if(c['fieldAPI'] == 'Department_Class__c'){
|
c['value'] =''
|
}
|
}
|
}
|
//this.layout = [...this.layout];
|
}
|
|
//提交保存
|
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);
|
fields['Contact_Name__c'] = this.contactId;
|
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);
|
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
|
//新建
|
debugger
|
if(this.isNewMode){
|
this.NewPIToAWS(payloadForNewPI,fields);
|
}
|
//编辑
|
if(this.isEditMode){
|
this.UpdatePIToAWS(payloadForNewPI,fields);
|
}
|
}
|
|
//提交保存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]');
|
}
|
}
|
if (this.isNewMode) {
|
fields['AWS_Data_Id__c'] = result.object[0].dataId;
|
} else {
|
//更新
|
}
|
this.txId = result.txId;
|
console.log('this.txId = ' + this.txId);
|
console.log('PI After fields = ' + JSON.stringify(fields));
|
if(fields.RecordTypeId){
|
console.log('length = ' + JSON.stringify(fields.RecordTypeId.length));
|
fields['RecordTypeId'] = fields.RecordTypeId.substring(1, fields.RecordTypeId.length - 1);
|
console.log('RecordTypeId = ' + JSON.stringify(fields['RecordTypeId']));
|
}
|
//保存到后端
|
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]');
|
}
|
}
|
if (this.isNewMode) {
|
fields['AWS_Data_Id__c'] = result.object[0].dataId;
|
} else {
|
//更新
|
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) {
|
console.log('length = ' + JSON.stringify(fields.RecordTypeId.length));
|
fields['RecordTypeId'] = fields.RecordTypeId.substring(1, fields.RecordTypeId.length - 1);
|
console.log('RecordTypeId = ' + JSON.stringify(fields['RecordTypeId']));
|
}
|
//保存到后端
|
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))
|
});
|
}
|
|
//验证字段
|
validateFieldValueFormate() {
|
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 = '';
|
for (var s of this.layout) {
|
for (var c of s.layoutFields) {
|
if(c.isRequired && c.editableField && (fields[c.fieldAPI] == null || fields[c.fieldAPI] == '')){
|
msg += ';' + c.fieldLabel;
|
}
|
}
|
}
|
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);
|
}
|
|
//查询客户人员根据医院
|
searchHospitalNameModal(event){
|
let fieldName = event.target.getAttribute("data-field");
|
if(fieldName == 'Contact_Name__c'){
|
if(this.hospitalId == "000000000000000" || this.hospitalId == ''){
|
this.showToast('Error','请先选择医院名');
|
return
|
}
|
this.isShowSearchAccount = true;
|
}
|
}
|
|
//选择客户人员后进行赋值
|
handleSelectContact(event){
|
this.isShowSpinner = true;
|
console.log('enter handleSelectContact ');
|
const selectContact = event.detail;
|
console.log('selectContact = ' + JSON.stringify(selectContact))
|
for (var s of this.layout) {
|
for (var c of s.layoutFields) {
|
if(c['fieldAPI'] == 'Contact_Name__c'){
|
c['value'] = selectContact.data.medicalStaffFullName;
|
this.contactId = selectContact.data.sfRecordId;
|
console.log('this.contactId = ' + this.contactId);
|
console.log('selectContact.data.sfRecordId = ' + selectContact.data.sfRecordId);
|
console.log('c[value] = ' + c['value']);
|
}
|
if(c['fieldAPI'] == 'LastName'){
|
c['value'] = selectContact.data.medicalStaffFullName;
|
this.modifyObj['LastName'] = selectContact.data.medicalStaffFullName;
|
console.log('c[value] = ' + c['value']);
|
}
|
}
|
}
|
this.layout = [...this.layout];
|
this.isShowSpinner = false;
|
this.closeHospitalNameModal();
|
}
|
|
//关闭客户人员根据医院模态框
|
closeHospitalNameModal(){
|
this.isShowSearchAccount = false;
|
}
|
|
//显示信息
|
showToast(type, msg) {
|
this.isShowSpinner = false;
|
const event = new ShowToastEvent({
|
title: type,
|
variant: type,
|
message: msg
|
});
|
this.dispatchEvent(event);
|
}
|
}
|