buli
2023-07-14 5b5c1e16deaa3a9d6d0ed1ffca390655ed103df7
force-app/main/default/lwc/lexNewAndEditContactPIPL/lexNewAndEditContactPIPL.js
@@ -5,7 +5,9 @@
import { AWSService } from 'c/piUtils';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class LexNewAndEditContactPIPL extends NavigationMixin(LightningElement) {
export default class LexNewAndEditContactPIPL extends NavigationMixin(
    LightningElement
) {
    sobjectType = 'Contact';
    @api recordId;
@@ -16,7 +18,6 @@
    @track isShowSpinner = true;
    @track layout = [];
    @track isDoctor = true;
    @track staticResource;
    @track staticResourceContact;
@@ -54,7 +55,14 @@
            this.recordTypeId = this.pageRef.state.recordTypeId;
            console.log('this.recordTypeId = ' + this.recordTypeId);
        }
        console.log('recordId = ' + this.recordId + ' recordTypeId = ' + this.recordTypeId + ' sobjectType = ' + this.sobjectType);
        console.log(
            'recordId = ' +
                this.recordId +
                ' recordTypeId = ' +
                this.recordTypeId +
                ' sobjectType = ' +
                this.sobjectType
        );
        initData({
            rid: this.recordId,
            recordTypeId: this.recordTypeId,
@@ -64,18 +72,16 @@
            if (r.status == 'Success') {
                this.isDoctor = r.entity.isDoctor;
                console.log('this.isDoctor = ' + this.isDoctor);
                if(this.isEditMode){
                if (this.isEditMode) {
                    this.recordTypeId = r.entity.recordTypeId;
                }
                console.log('this.recordTypeId = ' + this.recordTypeId);
                debugger
                debugger;
                //如果不是医院类型,使用标准页面
                if (!this.isDoctor) {
                    if(this.isNewMode)
                        this.RedirectStandardNewPage();
                    if(this.isEditMode)
                        this.RedirectStandardEditPage();
                    return
                    if (this.isNewMode) this.RedirectStandardNewPage();
                    if (this.isEditMode) this.RedirectStandardEditPage();
                    return;
                }
                this.AWSDataId = r.entity.AWSDataId;
                let layout = JSON.parse(r.entity.layout);
@@ -83,21 +89,24 @@
                console.log('layout = ' + typeof layout);
                let index = 1;
                this.sectionName = layout.map(s => s.label);
                this.sectionName = layout.map((s) => s.label);
                for (let s of layout) {
                    for (let c of s.layoutColumns) {
                        c['index'] = index++;
                        if (c.layoutItems) {
                            for (let item of c.layoutItems) {
                                item['isDisable'] = item.behavior == 'Readonly' ? true : false;
                                item['isRequired'] = item.behavior == 'Required' ? true : false;
                                item['isDisable'] =
                                    item.behavior == 'Readonly' ? true : false;
                                item['isRequired'] =
                                    item.behavior == 'Required' ? true : false;
                                item['isModify'] = false;
                                if (item['field'] == 'Name') {
                                    item.field = 'LastName';
                                    item.fieldLabel = '姓名'
                                    item.fieldLabel = '姓名';
                                    item['isModify'] = true;
                                    if (this.isEditMode) {
                                        item['value'] = this.recordData.LastName
                                        item['value'] =
                                            this.recordData.LastName;
                                    }
                                }
                            }
@@ -106,9 +115,10 @@
                }
                this.layout = layout;
                console.log('this.layout = ' + JSON.stringify(this.layout));
                this.AWSToSobjectMap = JSON.parse(JSON.stringify(r.entity.AWSToSobjectNonEncryptedMap));
                this.AWSToSobjectMap = JSON.parse(
                    JSON.stringify(r.entity.AWSToSobjectNonEncryptedMap)
                );
                this.staticResource = JSON.parse(r.entity.staticResource);
                //编辑
                if (this.isEditMode) {
@@ -119,54 +129,72 @@
            } else {
                this.showToast('Error', r.msg);
            }
        })
        });
    }
    querySobjectFromAWS() {
        debugger
        debugger;
        var that = this;
        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 (c['field'] == 'LastName') {
                                            that.modifyObj['LastName'] = data.object[f] == null ? '' : data.object[f];
        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 (c['field'] == 'LastName') {
                                                that.modifyObj['LastName'] =
                                                    data.object[f] == null
                                                        ? ''
                                                        : data.object[f];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // for (var s of that.layout) {
                    //     for (var c of s.layoutFields) {
                    //         for (let f in that.AWSToSobjectMap) {
                    //             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];
                    //                 }
                    //             }
                    //         }
                    //     }
                    // }
                }
                // for (var s of that.layout) {
                //     for (var c of s.layoutFields) {
                //         for (let f in that.AWSToSobjectMap) {
                //             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];
                //                 }
                //             }
                //         }
                //     }
                // }
            }
        }, this.staticResource.token)
            },
            this.staticResource.token
        );
    }
    dataChange(event) {
        let fieldName = event.target.getAttribute("data-field");
        let fieldName = event.target.getAttribute('data-field');
        let value = event.detail.value;
        console.log("fieldName = " + fieldName + " value = " + event.detail.value);
        console.log(
            'fieldName = ' + fieldName + ' value = ' + event.detail.value
        );
        switch (fieldName) {
            case "LastName":
            case 'LastName':
                this.modifyObj[fieldName] = value;
                break;
        }
@@ -182,21 +210,26 @@
        Object.assign(fields, this.modifyObj);
        console.log('fields = ' + JSON.stringify(fields));
        debugger
        debugger;
        //2. select cannot actively select redaction option
        let validationResultMessage = this.validateFieldValueFormate(fields);
        console.log(validationResultMessage);
        if (validationResultMessage) {
            this.showToast('Error', validationResultMessage);
            return
            return;
        }
        //3. Check Required Field
        let checkRequiredFieldMsgResult = this.checkRequiredFieldMsg(fields);
        console.log('checkRequiredFieldMsgResult = ' + checkRequiredFieldMsgResult);
        console.log(
            'checkRequiredFieldMsgResult = ' + checkRequiredFieldMsgResult
        );
        if (checkRequiredFieldMsgResult != '') {
            this.showToast('Error', checkRequiredFieldMsgResult + '需要进行填写');
            return
            this.showToast(
                'Error',
                checkRequiredFieldMsgResult + '需要进行填写'
            );
            return;
        }
        //4. Prepare the payload for New PI API To AWS - To Do
@@ -219,11 +252,9 @@
        let error_msg = '';
        let b = false;
        for (var key in fields) {
            if (fields[key] == "*****")
                b = true;
            if (fields[key] == '*****') b = true;
        }
        if (b)
            error_msg = '下拉框不能主动选择密文选项';
        if (b) error_msg = '下拉框不能主动选择密文选项';
        return error_msg;
    }
@@ -235,7 +266,12 @@
                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] == '')) {
                            if (
                                !c.isDisable &&
                                c.isRequired &&
                                (fields[c.field] == null ||
                                    fields[c.field] == '')
                            ) {
                                msg += ';' + c.fieldLabel;
                            }
                        }
@@ -243,7 +279,7 @@
                }
            }
        } catch (err) {
            this.showToast('Error',err.message);
            this.showToast('Error', err.message);
        }
        msg = msg.substring(1);
        return msg;
@@ -251,15 +287,14 @@
    //获取PI字段
    getPIPayload(sobjJsonLwc) {
        console.log()
        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 {
                leadPIData[f] = sobjJsonLwc[this.AWSToSobjectMap[f]];
            } else {
                console.log(this.AWSToSobjectMap[f] + 'is not in sobjJsonLwc');
            }
        }
@@ -274,27 +309,40 @@
    //新建保存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]]);
        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]');
                        }
                    }
                    else {
                        console.log(f + 'is not in result.object[0]');
                    }
                    fields['AWS_Data_Id__c'] = result.object[0].dataId;
                    this.txId = result.txId;
                    //保存到后端
                    this.template
                        .querySelector('lightning-record-edit-form')
                        .submit(fields);
                } else {
                    console.log('result = ' + JSON.stringify(result));
                }
                fields['AWS_Data_Id__c'] = result.object[0].dataId;
                this.txId = result.txId;
                //保存到后端
                this.template.querySelector('lightning-record-edit-form').submit(fields);
            } else {
                console.log('result = ' + JSON.stringify(result));
            }
        }, this.staticResource.token);
            },
            this.staticResource.token
        );
    }
    //编辑保存ToAWS
@@ -302,33 +350,49 @@
        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]]);
        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]');
                        }
                    }
                    else {
                        console.log(f + 'is not in result.object[0]');
                    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) {
                        fields['RecordTypeId'] = fields.RecordTypeId.substring(
                            1,
                            fields.RecordTypeId.length - 1
                        );
                    }
                    //保存到后端
                    console.log('update submit = ' + JSON.stringify(fields));
                    this.template
                        .querySelector('lightning-record-edit-form')
                        .submit(fields);
                } else {
                    console.log('result = ' + JSON.stringify(result));
                }
                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) {
                    fields['RecordTypeId'] = fields.RecordTypeId.substring(1, fields.RecordTypeId.length - 1);
                }
                //保存到后端
                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);
            },
            this.staticResource.token
        );
    }
    //提交保存成功
@@ -337,39 +401,55 @@
        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
                }
            });
        });
        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))
        });
        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));
            }
        );
    }
    //change事件
    dataChange(event) {
        let fieldName = event.target.getAttribute("data-field");
        let fieldName = event.target.getAttribute('data-field');
        let value = event.detail.value;
        console.log("fieldName = " + fieldName + " value = " + event.detail.value);
        console.log(
            'fieldName = ' + fieldName + ' value = ' + event.detail.value
        );
        if (this.modifyArray.indexOf(fieldName) != -1) {
            switch (fieldName) {
                case "LastName":
                case 'LastName':
                    this.modifyObj[fieldName] = value;
                    break;
            }
@@ -404,10 +484,10 @@
            attributes: {
                objectApiName: 'Contact',
                recordId: this.recordId,
                actionName: 'edit',
                actionName: 'edit'
            },
            state: {
                nooverride: '1',
                nooverride: '1'
            }
        });
    }
@@ -422,4 +502,4 @@
        });
        this.dispatchEvent(event);
    }
}
}