沙世明
2022-03-23 34d9607524cad4f765299b28b4f9423cc2fa9e32
force-app/main/default/classes/NFM203Rest.cls
@@ -1,6 +1,9 @@
@RestResource(urlMapping = '/NFM203/*')
global with sharing class NFM203Rest {
    //add Response to aws 20220221 add sushanhu
    static Boolean SFStatus=true;
    static String SFMessage='';
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] Partners;
@@ -78,7 +81,7 @@
        public String BusinessAddress;          //经营场所(等同办公地址)
        //WLIG-CA48WC pk start 2022-1-7
        public String RegisterAddress;          //住所地址
        //WLIG-CA48WC pk end 2022-1-7
        //WLIG-CA48WC pk end 2022-1-7
        public String WarehouseAddress;         //库房地址
        public String BusinessScope;            //经营二类范围
        public String BusinessScope1;           //经营三类范围
@@ -93,10 +96,16 @@
    // 收货人层级
    global class Consignee_Info {
        public String ContactName;
        public String ContactNameEncrypted;//add 20220221
        public String ContactPhone;
        public String ContactPhoneEncrypted;//add 20220221
        public String PostalCode;
        public String PostalCodeEncrypted;//add 20220221
        public String ContactAddress;
        public String ContactAddressEncrypted;//add 20220221
        public String ContactCity; //有效收货人城市
        public String ContactDataId;// add  20220221 CONTACT aws 存储id
        public String AddressDataId;// add 2020221 adress aws 存储id
    }
    // 销售人层级
@@ -154,13 +163,18 @@
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
        NFMUtil.NFMResponse result = NFMUtil.getNFMResponse();
        if (ges == null ) {
            SFMessage+='参数为空';
            system.debug('SFMessage---'+SFMessage);
            return;
        }
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            SFMessage+='Monitoring为空';
            system.debug('SFMessage---'+SFMessage);
            return;
        }
@@ -173,7 +187,12 @@
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        //String jsonResponse = '{"status": "Success", "Message":'  + gedata + '}';
        String jsonResponse = '{"status": "Success", "Message":""}';
        // String jsonResponse = '{"status": "Success", "Message":""}';
        //updata response toAWS
        result.SFStatus=SFStatus;
        result.SFMessage=SFMessage;
        String jsonResponse =JSON.serialize(result);
        system.debug('result---'+jsonResponse);
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }
@@ -263,6 +282,11 @@
                    if (String.isNotBlank(result.split('/')[0])) {
                        iflog.ErrorLog__c += result.split('/')[0];
                        //add response to aws 2022022 sushanhu start
                        SFMessage=iflog.ErrorLog__c;
                        system.debug('ErrorLog__c--'+iflog.ErrorLog__c);
                        system.debug('SFMessage---'+SFMessage);
                        //add response to aws 20220222 sushanhu end
                    } else {
                        logstr += result.split('/')[1];
                    }
@@ -270,7 +294,13 @@
                }
                //WYAN-BS8B2A -XHL -20200925 -End
                if (String.isBlank(gda.BPType)) {
                    iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of BPType is required,This data is skipped.\n';
                    //add response to aws 20220221 sushanhu start
                    SFMessage=iflog.ErrorLog__c;
                    system.debug('SFMessage--'+SFMessage);
                    //add response to aws 20220221 sushanhu end
                    continue;
                }
                if (gda.BPType != '02' && gda.BPType != '03' && gda.BPType != '04' && gda.BPType != '05') {
@@ -278,16 +308,28 @@
                }
                if (String.isBlank(gda.BPCode)) {
                    iflog.ErrorLog__c += 'BPCode is required,This data is skipped.\n';
                    //add response to aws 20220221 sushanhu start
                    SFMessage=iflog.ErrorLog__c;
                    system.debug('SFMessage---'+SFMessage);
                    //add response to aws 20220221 sushanhu end
                    continue;
                }
                if (gda.BPType == '02') {
                    // 经销商的场合
                    if (String.isBlank(gda.CompanyName)) {
                        iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of CompanyName is required,This data is skipped.\n';
                        //add response to aws 20220221 sushanhu start
                        SFMessage=iflog.ErrorLog__c;
                        system.debug('SFMessage---'+SFMessage);
                        //add response to aws 20220221 sushanhu end
                        continue;
                    }
                    if (String.isBlank(gda.AgentType)) {
                        iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of AgentType is required,This data is skipped.\n';
                        //add response to aws 20220221 sushanhu start
                        SFMessage=iflog.ErrorLog__c;
                        system.debug('SFMessage---'+SFMessage);
                        //add response to aws 20220221 sushanhu end
                        continue;
                    }
                    //当经销商的分类为“特约经销商(区域)”、“特约经销商(产品)”、"特约经销商(区域+产品)"、"集采经销商"时,验证契约的必填字段
@@ -298,6 +340,10 @@
                            //CHAN-BF8CCU ---XHL---updateEnd---20190821
                            if (String.isBlank(contractInfos.TermContractNo)) {
                                iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of TermContractNo is required ,This data is skipped.\n';
                               //add response to aws 20220221 sushanhu start
                                SFMessage=iflog.ErrorLog__c;
                                system.debug('SFMessage---'+SFMessage);
                                //add response to aws 20220221 sushanhu end
                                flg = true;
                                continue;
                            }
@@ -321,6 +367,10 @@
                    applyTypeMap.put(gda.BPCode, gda.ApplyType);
                }
                //add result message 20220222 sushanhu satrt
                    SFMessage=iflog.ErrorLog__c;
                    system.debug('SFMessage---'+SFMessage);
                //add result message 20220222 sushanhu end
                bPCodeList.add(gda.BPCode);
                nfmgd.add(gda);
@@ -403,6 +453,10 @@
                    //判断BPCode是否已经存在,不存在的话会提示不存在,并且跳过
                    if (!accsMap.containsKey(gda.BPCode)) {
                        iflog.ErrorLog__c += ' This BPCode[' + gda.BPCode + '] is not exist,This data is skipped .\n';
                        //add response to aws 20220221 sushanhu start
                        SFMessage=iflog.ErrorLog__c;
                        system.debug('SFMessage---'+SFMessage);
                        //add response to aws 20220221 sushanhu end
                        continue;
                    }
                }
@@ -497,6 +551,9 @@
                    accountInfo.SPO_ApplicationNo__c = GetApplicationNo(gda);
                }
                accountUpsertList.add(accountInfo);
                // //add response to aws 20220221 sushanhu start
                // SFMessage=iflog.ErrorLog__c;
                // //add response to aws 20220221 sushanhu end
            }
            // 新建/更新客户主数据
            if (accountUpsertList.size() > 0) {
@@ -580,6 +637,10 @@
                                    } 
                                } else {
                                    iflog.ErrorLog__c += 'This Contract[ ' + contractInfos.TermContractNo + ' ] not exist,The Data Not  Cancel';
                                     //add response to aws 20220221 sushanhu start
                                    SFMessage=iflog.ErrorLog__c;
                                    system.debug('SFMessage---'+SFMessage);
                                    //add response to aws 20220221 sushanhu end
                                    continue ;
                                }
@@ -640,6 +701,9 @@
                                if (cdcErrorMap.size() > 0) {
                                    iflog.ErrorLog__c += ' This BPCode[' + gda.BPCode + '] of ContractDepartmentClass [' + cdcErrorMap.values() + '] noneffective, [' + cdcErrorMap.values() + '] skipped .\n';
                                     //add response to aws 20220221 sushanhu start
                                    SFMessage=iflog.ErrorLog__c;
                                    //add response to aws 20220221 sushanhu end
                                }
                                contractInfoAcc.Contract_Start_Date__c = NFMUtil.parseStr2Date(contractInfos.ContractStartDate);
                                contractInfoAcc.Contract_End_Date__c = NFMUtil.parseStr2Date(contractInfos.ContractEndDate);
@@ -805,6 +869,10 @@
                    if (String.isBlank(gdds.LicenseType)) {
                        iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of LicenseType is required ,This data is skipped.\n';
                        //add response to aws 20220221 sushanhu start
                        SFMessage=iflog.ErrorLog__c;
                        system.debug('SFMessage---'+SFMessage);
                        //add response to aws 20220221 sushanhu end
                        continue;
                    }
                    if (gdds.LicenseType == '23' || gdds.LicenseType == '24') { //3PL相关证
@@ -812,6 +880,10 @@
                    }
                    if (String.isBlank(gdds.BusLicenseNo)) {
                        iflog.ErrorLog__c += 'BPCode[ ' + gda.BPCode + ' ] of BusLicenseNo is required,This data is skipped .\n';
                        //add response to aws 20220221 sushanhu start
                        SFMessage=iflog.ErrorLog__c;
                        system.debug('SFMessage---'+SFMessage);
                        //add response to aws 20220221 sushanhu end
                        continue;
                    }
                    geDataDetailsMap.put(gdds.LicenseType, gdds);
@@ -847,6 +919,7 @@
                    licenseInfo.IsFromSPO__c = true;
                    licenseInfo.Scope__c = gdds.BusinessScope;
                    licenseInfo.Scope3__c = gdds.BusinessScope1;
                    licenseInfoUpsertList.add(licenseInfo);
                    //对"经销商"主数据中的"营业许可证书状况"的相关字段进行赋值
@@ -960,11 +1033,20 @@
            logstr += '\nend';
            rowData.retry_cnt__c = 0;
            //add response to AWS 20220221 start
            SFStatus =true;
            SFMessage+=logstr;
            system.debug('SFMessage---'+SFMessage);
             //add response to AWS 20220221 end
        } catch (Exception ex) {
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'NFM203_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM203_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            //add response to AWS 20220221 start
            SFMessage+=ex.getMessage();
            system.debug('SFMessage---'+SFMessage);
            //add response to AWS 20220221 end
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
@@ -1069,9 +1151,9 @@
            
        for (Consignee_Info consigneeInfo : gda.Consignee_Info) {
            // 联系人名称
            if (String.isBlank(consigneeInfo.ContactName)) {
                continue;
            }
            // if (String.isBlank(consigneeInfo.ContactName)) {
            //     continue;
            // }
            // 联系人地址
            if (String.isBlank(consigneeInfo.ContactAddress)) {
                continue;
@@ -1148,10 +1230,14 @@
                if (complianceDataMap.containsKey(accountCode)) {
                    // 获取客户Id
                    for(Consignee_Info info:complianceDataMap.get(accountCode)){
                        String accountId = tempAccountMap.get(accountCode).Id;
                        // add  sushanhu for pi '000000'为Consignee_Info源数据为空
                        if (!'000000'.equals(info.ContactDataId)) {
                            String accountId = tempAccountMap.get(accountCode).Id;
                        String contactPhone = info.ContactPhone;
                        String contactName = info.ContactName;
                        String contactAllName = contactAllName(contactName);
                        // String contactAllName = contactAllName(contactName);
                        //update to aws contactName 20220221 sushanhu
                        String contactAllName=info.contactName;
                        // 联系人字段赋值
                        Contact contact  = new Contact();
@@ -1160,27 +1246,47 @@
                        contact.Phone = contactPhone;
                        contact.MobilePhone = contactPhone;
                        contact.LastName = contactAllName.split('_')[0];
                        contact.FirstName = contactAllName.split('_')[1];
                        // contact.FirstName = contactAllName.split('_')[1];//delete for pi 20220304
                        contact.IsFromSPO__c = true;
                        // SFDC电话重复人员创建信息确认  pk 2022-2-28 start
                        contact.RecordTypeId = '01210000000QfWi';
                        // SFDC电话重复人员创建信息确认  pk 2022-2-28 end
                        contact.Ignore_Same_Name__c = true;//不是重复的客户名 跳过 客户人员名字已存在 验证
                        // add pi 密文 start 20220221 sushanhu
                        contact.Phone_Encrypted__c = info.ContactPhoneEncrypted;
                        contact.MobilePhone_Encrypted__c = info.ContactPhoneEncrypted;
                        contact.LastName_Encrypted__c =info.ContactNameEncrypted;
                        contact.AWS_Data_Id__c = info.ContactDataId;
                        // add pi 密文 end 20220221 sushanhu
                        insertContactList.add(contact);
                        // 收货地址字段赋值
                        if (!'000000'.equals(info.AddressDataId)) {
                            // 收货地址字段赋值
                        String contactCity = info.ContactCity;
                        Address__c addressData = new Address__c();
                        addressData.Customer__c = accountId;//客户
                        addressData.IsFromSPO__c = true;//是否SPO来源
                        addressData.Telephone__c = contactPhone;//电话
                        addressData.Telephone_Encrypted__c =info.ContactPhoneEncrypted;//电话密文 add sushanhu for pi 20220304
                        addressData.Address_Classification__c = '经销商';//地址分类
                        addressData.Detailed_Address__c = info.ContactAddress;//详细地址
                        // 因接口不传输省,导致保存时报错。因此将其注掉,城市字段不赋值 XHL 20210823
                        // if (cityMap.containsKey(contactCity)) {
                        //     addressData.City__c = cityMap.get(contactCity).Id;//市
                        // }
                        // add pi 密文 start sushanhu 20220221
                        addressData.AWS_Data_Id__c =info.AddressDataId;
                        addressData.Detailed_Address_Encrypted__c =info.ContactAddressEncrypted;
                        // add pi 密文 end sushanhu 20220221
                        String mapkey = accountCode + contactName;
                        insertAddressMap.put(mapkey,addressData);
                        accountCodeList.add(accountCode);
                       }
                        }
                    }
                    
                }