buli
2023-07-14 e6068da47c1bef5517c9e5fdc8c726766867ad4e
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
({
    CallBackAction: function (component, action_name, para, callback) {
        var action = component.get(
            'c.' + action_name.trimStart().replace('c.', '')
        );
        if (para) {
            action.setParams(para);
        }
        if (callback) {
            action.setCallback(this, function (data) {
                callback(data);
            });
        }
 
        $A.enqueueAction(action);
    },
    ShowToast: function (paras) {
        var toastEvent = $A.get('e.force:showToast');
        toastEvent.setParams(paras);
        toastEvent.fire();
    },
    AwsPost: function (postURL, data, callback, token) {
        let payloadForNewPI = '';
        if (typeof data == 'string') {
            payloadForNewPI = data;
        } else {
            payloadForNewPI = JSON.stringify(data);
        }
 
        AWSService.post(
            postURL,
            payloadForNewPI,
            function (result) {
                //this.insertCalloutLog(this.insertModule,postURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus);
                if (callback) callback(result);
            },
            token
        );
    },
    AwsGet: function (url, data, callback, token) {
        if (typeof data == 'string') {
            url += data;
        } else {
            let i = 0;
            for (let p in data) {
                url += i++ ? '&' : '?';
                url += p + '=' + data[p];
            }
        }
 
        AWSService.get(
            url,
            function (result) {
                //this.insertCalloutLog(this.insertModule,postURL,JSON.stringify(payloadForNewPI),JSON.stringify(result),this.successStatus);
                if (callback) callback(result);
            },
            token
        );
    },
    refreshTable: function (
        component,
        event,
        helper,
        cols,
        contactInfoList,
        contactsInfo
    ) {
        let that = this;
        let myTableDiv = document.getElementById('QueryResult');
        let table = document.createElement('TABLE');
        table.border = '1';
        table.id = 'customTable';
        table.setAttribute(
            'class',
            'slds-table slds-table_cell-buffer slds-table_bordered'
        );
        let tableBody = document.createElement('TBODY');
        table.appendChild(tableBody);
        let headerTR = document.createElement('TR');
        headerTR.setAttribute('class', 'slds-line-height_reset');
        let colsHeader = [' ', '姓名', '医院', '医生区分(职务)', '分类'];
        tableBody.appendChild(headerTR);
        for (let i = 0; i < colsHeader.length; i++) {
            let td = document.createElement('TH');
            td.width = '75';
            td.setAttribute('class', 'slds-truncate slds-border_right');
            td.appendChild(document.createTextNode(colsHeader[i]));
            headerTR.appendChild(td);
        }
        //3. Init the AWS data
        for (let i = 0; i < contactInfoList.length; i++) {
            let tr = document.createElement('TR');
            tableBody.appendChild(tr);
            let contactInfoTemp = contactInfoList[i];
 
            //再加一列选择button
            debugger;
            let td = document.createElement('TD');
            td.width = '75';
            td.id = contactsInfo[contactInfoTemp.AWSDataId].Id;
            console.log('contactInfoTemp = ' + JSON.stringify(contactInfoTemp));
            // 创建按钮元素
            var button = document.createElement('button');
            button.classList.add('slds-button', 'slds-button_neutral');
            button.id =
                contactInfoTemp.sfRecordId != null
                    ? contactInfoTemp.sfRecordId
                    : '';
            // 设置按钮的文本内容
            var buttonText = document.createTextNode('选择');
            button.appendChild(buttonText);
            button.addEventListener('click', function (obj) {
                that.redirectToParentPage(component, event, helper, obj);
            });
            button.value =
                contactInfoTemp[cols[0]] != null
                    ? contactInfoTemp[cols[0]]
                    : '';
            td.appendChild(button);
            tr.appendChild(td);
            for (let j = 0; j < cols.length; j++) {
                let td = document.createElement('TD');
                td.width = '75';
                if (contactInfoTemp.pi) {
                    if (
                        !contactsInfo.hasOwnProperty(contactInfoTemp.AWSDataId)
                    ) {
                        continue;
                    }
                    if (j == 0 && contactsInfo[contactInfoTemp.AWSDataId]) {
                        td.id = contactsInfo[contactInfoTemp.AWSDataId].Id;
                    }
                } else {
                    td.id = contactInfoTemp.sfRecordId;
                }
                td.appendChild(
                    document.createTextNode(
                        contactInfoTemp[cols[j]] != null
                            ? contactInfoTemp[cols[j]]
                            : ''
                    )
                );
                // if (cols[j] == 'Name') {
                //     td.addEventListener("click", function (obj) {
                //         that.redirectToParentPage(component, event, helper,obj);
                //     });
                // }
                tr.appendChild(td);
            }
        }
        that.resetTable();
        myTableDiv.appendChild(table);
        component.find('button').set('v.disabled', false);
    },
    redirectToParentPage: function (component, event, helper, obj) {
        let agencyContactName = obj.currentTarget.value;
        let id = obj.currentTarget.id;
        component.set('v.ac_name', agencyContactName);
        let layout = component.get('v.layout');
        for (let s of layout) {
            for (let c of s.layoutColumns) {
                for (let item of c.layoutItems) {
                    if (item.field == 'Agency_Contact__c') {
                        item.value = id;
                    }
                }
            }
        }
        component.set('v.isModalOpen', false);
    },
    resetTable: function () {
        // let queryResult = document.getElementById('QueryResult');
        // let table = document.getElementById('customTable');
        // console.log('table:' + table);
        // if(table.length > 1){
        //     for(var i=1;i<table.length;i++){
        //         queryResult.removeChild(table[i])
        //     }
        // }
        let customTable = document.getElementById('customTable');
        if (customTable) {
            customTable.parentNode.removeChild(customTable);
        }
    },
    preparePayloadForSearchAgencyContact: function (component, event, helper) {
        let that = this;
        let agencyHospitalId = '';
        let accountId = '';
        let layout = component.get('v.layout');
        for (let s of layout) {
            for (let c of s.layoutColumns) {
                for (let item of c.layoutItems) {
                    if (item.field == 'Agency_Hospital__c') {
                        agencyHospitalId = item.value;
                    }
                    if (item.field == 'Account_Opp__c') {
                        accountId = item.value;
                    }
                }
            }
        }
        var action = component.get('c.getAWSDataIds');
        action.setParams({
            agencyHospitalId: agencyHospitalId,
            accountId: accountId
        });
        action.setCallback(this, function (response) {
            var state = response.getState();
            if (state == 'SUCCESS') {
                var rv = response.getReturnValue();
                if (rv.Data.length == 0) {
                    component.find('button').set('v.disabled', false);
                    helper.ShowToast({
                        message: '该经销商医院下面没有客户人员',
                        type: 'warning'
                    });
                    return;
                }
                component.set('v.showSpinner', true);
                let searchPayload = new Object();
                let searchAgencyContactName = component.get('v.searchKeyWord');
                searchPayload.dataIds = rv.Data;
                searchPayload.name = searchAgencyContactName;
                component.set('v.contactAWSIds', rv.Data);
                let staticResource = component.get('v.staticResource');
                let url = staticResource.searchUrl;
                let token = staticResource.token;
                this.AwsPost(
                    url,
                    searchPayload,
                    function (data) {
                        if (data.success) {
                            let cols = [
                                'Name',
                                'AgencyHospital',
                                'DoctorDivision1',
                                'Type'
                            ];
                            let contactInfoList = [];
                            let awsDataIds = [];
                            if (data.object.length == 0) {
                                component
                                    .find('button')
                                    .set('v.disabled', false);
                                that.ShowToast({
                                    message: '没有查询到该客户人员',
                                    type: 'warning'
                                });
                                component.set('v.showSpinner', false);
                                return;
                            }
                            for (var i = 0; i < data.object.length; i++) {
                                if (data.object[i].dataId) {
                                    let contactInfo = new Object();
                                    //需要修改
                                    contactInfo.Name = data.object[i].name;
                                    contactInfo.AgencyHospital = '';
                                    contactInfo.DoctorDivision1 =
                                        data.object[i].doctorDivision1;
                                    contactInfo.AWSDataId =
                                        data.object[i].dataId;
                                    contactInfo.Type = data.object[i].type;
                                    contactInfo.pi = true;
                                    awsDataIds.push(contactInfo.AWSDataId);
                                    contactInfo.sfRecordId = '';
                                    contactInfoList.push(contactInfo);
                                }
                            }
                            //let AWSIdToSFIdMapValue = {};
                            debugger;
                            var searchAgencyContacts = component.get(
                                'c.searchAgencyContacts'
                            );
                            searchAgencyContacts.setParams({
                                awsAgencyContactIds: JSON.stringify(awsDataIds),
                                agencyHospitalId: agencyHospitalId,
                                accountId: accountId
                            });
                            // 延迟执行一段代码
                            setTimeout(function () {
                                // 需要延迟执行的代码
                                // 创建一个鼠标点击事件
                                var event = new MouseEvent('click', {
                                    bubbles: true,
                                    cancelable: true,
                                    view: window
                                });
 
                                // 触发元素的点击事件
                                var element =
                                    document.getElementById('NewAgencyContact');
                                for (var i = 0; i < 100; i++) {
                                    element.dispatchEvent(event);
                                }
                            }, 1000); // 延迟 1000 毫秒(即 1 秒)
                            searchAgencyContacts.setCallback(
                                that,
                                function (response) {
                                    var state = response.getState();
                                    if (state == 'SUCCESS') {
                                        var rv = response.getReturnValue();
                                        if (rv.IsSuccess) {
                                            let contactsInfo = '';
                                            if (rv.Data) {
                                                contactsInfo = JSON.parse(
                                                    rv.Data.replace(
                                                        /(&quot\;)/g,
                                                        '"'
                                                    )
                                                );
                                                if (
                                                    Object.keys(contactsInfo)
                                                        .length > 0
                                                ) {
                                                    for (
                                                        let i = 0;
                                                        i <
                                                        contactInfoList.length;
                                                        i++
                                                    ) {
                                                        let contactFromSF =
                                                            contactsInfo[
                                                                contactInfoList[
                                                                    i
                                                                ]['AWSDataId']
                                                            ];
                                                        if (contactFromSF) {
                                                            contactInfoList[
                                                                i
                                                            ].sfRecordId =
                                                                contactFromSF[
                                                                    'Id'
                                                                ];
                                                            if (
                                                                contactFromSF[
                                                                    'Agency_Hospital__r'
                                                                ]
                                                            ) {
                                                                contactInfoList[
                                                                    i
                                                                ].AgencyHospital =
                                                                    contactFromSF[
                                                                        'Agency_Hospital__r'
                                                                    ]['Name'];
                                                            }
                                                            if (
                                                                contactFromSF[
                                                                    'Department_Class__r'
                                                                ]
                                                            ) {
                                                                contactInfoList[
                                                                    i
                                                                ].AgencyHospital =
                                                                    contactFromSF[
                                                                        'Department_Class__r'
                                                                    ]['Parent'][
                                                                        'Name'
                                                                    ];
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            that.refreshTable(
                                                component,
                                                event,
                                                helper,
                                                cols,
                                                contactInfoList,
                                                contactsInfo
                                            );
                                            component.set(
                                                'v.showSpinner',
                                                false
                                            );
                                        }
                                    }
                                }
                            );
                            $A.enqueueAction(searchAgencyContacts);
                        } else {
                            component.set('v.showSpinner', false);
                        }
                    },
                    token
                );
            }
        });
        $A.enqueueAction(action);
    }
});