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
import { LightningElement, track,api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import Init from '@salesforce/apex/LexSearchContactPIPLController.Init';
import searchContacts from '@salesforce/apex/LexSearchContactPIPLController.searchContacts';
import { AWSService } from 'c/piUtils';
 
const cols = [
    //update by Link 2023-10-16
    {label:'姓名', fieldName:'nameUrl', type:'url',typeAttributes:{label: { fieldName: 'Name' },target:'_blank'},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 LexSearchAWSContactByNamePIPL extends LightningElement {
    @track searchKey;
    @track contactsInfo;
    @track contactAWSIds;
    @track staticResources;
    @track Contacts;
    @track showSpinner;
    cols = cols;
    AWSService;
 
 
    connectedCallback() {
        this.AWSService = new AWSService();
        console.log('进入 search');
        Init({
            accountId:'',
            searchKey:''
        }).then(result=>{
            console.log('r==>'+JSON.stringify(result));
            if (result.status == 'success') {
                this.contactsInfo = result.contactsInfo;
                this.contactAWSIds = result.contactAWSIds;
                this.staticResources = JSON.parse(result.staticResource);
            }
        }).catch((error) => {
                console.log('error = ' + JSON.stringify(error));
            });
    }
 
   //dataChange
    handelSearchKey(event){
        this.searchKey = event.target.value;
        console.log('this.searchKey'+this.searchKey);  
    }
    SearchContact(){
        console.log('进入搜索');
        this.showSpinner = true;
        if(this.searchKey.length<2){
            this.showToast('请输入至少两个关键字','','error');
            this.Contacts = [];
            this.showSpinner = false;
            return;
        }
            //1. Prepare the payload for contact search
            let requestSearchPayload = this.preparePayloadForSearchContact();
            console.log('request payload body:'+requestSearchPayload);
            //2. Invoke AWS Service
            fetch(this.staticResources.searchUrl,{
                method: 'POST',
                body: requestSearchPayload,
                headers: {
                    'Content-Type': 'application/json',
                    'pi-token': this.staticResources.token
                }
            }).then((data) => {
                console.log('data==>'+JSON.stringify(data));
                return data.json();
 
            }).then((result) => {
                console.log('JSON Result from aws:'+JSON.stringify(result));
                if(result.object&&result.object.length>0){
                    console.log('result'+result);
                    this.initContactTable(result);
                }else{
                    alert('没有查到该联系人');
                    this.showSpinner = false;
                }               
            })
    }
    preparePayloadForSearchContact(){
        let searchPayload = new Object();
        searchPayload.dataIds = [];
        searchPayload.contactName = this.searchKey;
        return JSON.stringify(searchPayload);
    }
 
    initContactTable(data){
        let contactInfoList = [];
        let awsDataIds = [];
        for(let i=0;i<data.object.length;i++){
            if(data.object[i].dataId){
                let contactInfo = new Object();
                contactInfo.Name = data.object[i].lastName;
                contactInfo.Email = data.object[i].email;
                contactInfo.MobilePhone = data.object[i].mobilePhone;
                contactInfo.Phone = data.object[i].phone;
                contactInfo.AWSDataId = data.object[i].dataId;
                awsDataIds.push(contactInfo.AWSDataId);
                contactInfo.sfRecordId = '';
                contactInfoList.push(contactInfo);
            }                    
        }
        let AWSIdToSFIdMapValue = {};
        console.log('Contact Info from AWS:'+JSON.stringify(contactInfoList));
            searchContacts({
            awsContactIds:JSON.stringify(awsDataIds),
            searchContactName:'',
            accountId:'',
 
        }).then(result=>{
            console.log('r==>'+JSON.stringify(result));
            if(result.status == 'success'){                               
                this.contactsInfo = JSON.parse(result.message.replace(/(&quot\;)/g,"\""));
                console.log('Contact Info from SF:'+JSON.stringify(this.contactsInfo));
                if(Object.keys(this.contactsInfo).length>0){
                    for(let i=0;i<contactInfoList.length;i++){
                        let contactFromSF = this.contactsInfo[contactInfoList[i]['AWSDataId']];
                        if(contactFromSF){
                            contactInfoList[i].sfRecordId = contactFromSF['Id'];
                            contactInfoList[i].AccountName = contactFromSF['Account']['Name'];
                            //add by Link 2023-10-16
                            contactInfoList[i]['nameUrl'] = '/lightning/r/Contact/'+contactFromSF['Id']+'/view';
                        }
                    }
                }
                let newConList = [];
                for(var i = 0; i < contactInfoList.length; i++){
                    if(contactInfoList[i].sfRecordId != ''){
                        newConList.push(contactInfoList[i]);
                    }
                }
                contactInfoList = newConList;
                console.log('contactInfoList = '+JSON.stringify(contactInfoList));
                this.Contacts = contactInfoList;
                this.showSpinner = false;
            }else{
                alert('没查询到该联系人');
                console.log('No result');
                this.Contacts = [];
                this.showSpinner = false;
                // refreshTable(cols,[]);
            }  
        }).catch((error) => {
                console.log('error = ' + JSON.stringify(error));
                this.showSpinner = false;
            });
    }
    showToast(title,Message,variant) {
        const event = new ShowToastEvent({
            title: title,
            message: Message,
            variant: variant,
            mode: 'dismissable'
        });
        this.dispatchEvent(event);
    }
 
 
 
}