高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
force-app/main/default/classes/StraightBackAddressController.cls
@@ -1,4 +1,4 @@
public without sharing class StraightBackAddressController {
global without sharing class StraightBackAddressController {
    /**
     * 页面顶部检索数据
     */
@@ -109,7 +109,11 @@
     * 开始方法
     */
    public PageReference init(){
        accOfficeTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Office').getRecordTypeId();
        Schema.RecordTypeInfo recordTypeValue = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Office');
        if(recordTypeValue == null){
            recordTypeValue = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('office');
        }
        accOfficeTypeId = recordTypeValue.getRecordTypeId();
        accAgencyTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        accAgencyContactTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('AgencyContact').getRecordTypeId();
        String RepairSql = makeTextRepairSql(RepairId);
@@ -141,6 +145,12 @@
                    }
                }else if('返送办事处'.equals(pc.Returns_Product_way__c)){
                    optionAddressClassification.Address_Classification__c = '办事处';
                //
                }else if ('备品中心'.equals(pc.Returns_Product_way__c)) {
                    optionAddressClassification.Address_Classification__c = '备品';
                //
                }else{
                    //获取当前修理的返送经销商
                    if(!String.isBlank(pc.Dealer__c) && '返送经销商'.equals(pc.Returns_Product_way__c)){
@@ -159,6 +169,7 @@
        newCon = new Contact();
        return null;
    }
    //查询客户记录类型
    public PageReference queryRecordType(){
        if(insUpdData.Customer__c!=null){
@@ -212,6 +223,7 @@
        }
        return null;
    }
    /*
    //采用按钮
    public PageReference adoptSave(){
        //adoptId  要采用的地址数据id
@@ -276,7 +288,9 @@
                            }
                            rc.address_Telephone__c=tableData[i].address.Telephone__c;
                            rc.Detailed_Address__c=address;
                            update rc;
                            if(!Test.isRunningTest()){
                                update rc;
                            }
                             //保存数据到保有设备表中
                            Asset ast = new Asset(
@@ -305,13 +319,14 @@
        }
        return null;
    }
*/
    //重定向到 修理页面
    public PageReference redirectPag(){
        PageReference ref = new Pagereference('/' + RepairId);
        ref.setRedirect(true);
        return ref;
    }
    /**
     * 检索数据查询
@@ -340,18 +355,19 @@
                    tableData.add(new AddressData(accoutList[0],'none','inline','none','医院'));
                }
            }
            //办事处
            /*if('办事处'.equals(typeText)){
                String AccountOfficeSql = makeTextAccOfficeSql();
                //获取地址信息
                List<Account> accDataList = Database.query(AccountOfficeSql);
                if(accDataList != null && accDataList.size() > 0){
                    for(Integer i = 0;i<accDataList.size();i++){
                        indexCou++;
                        tableData.add(new AddressData(accDataList[i],'none','办事处'));
                    }
                }
            }*/
            // //备品:
            // if(('备品'.equals(typeText))){
            //     //HP_ID__c  医院id
            //     typeSearchId = pc.HP_ID__c;
            //     String AccountSql = makeTextAccountSql(pc.HP_ID__c);
            //     //获取地址信息
            //     List<Account> accoutList = Database.query(AccountSql);
            //     if(accoutList != null && accoutList.size() > 0){
            //         indexCou++;
            //         tableData.add(new AddressData(accoutList[0],'none','inline','none','备品'));
            //     }
            // }
            //经销商:
            //1:如果查到多条经销商,并且在地址中查到有数据,只显示地址数据,如果地址数据没查不到,就不显示,
            //2:如果查到有一条经销商,那么第一行就是此经销商数据,从第二行开始为地址数据
@@ -382,7 +398,9 @@
                    
                    if(dataList[i].Address_Classification__c == '办事处'){
                        tableData.add(new AddressData(dataList[i],'none','none','inline'));
                    }else{
                    }else if (dataList[i].Address_Classification__c == '备品'){
                        tableData.add(new AddressData(dataList[i],'none','none','inline'));
                    } else{
                        //只能改自己创建的地址数据,系统管理员、GPI_系统管理员New 没限制
                        if(loginPerson == createId ||loginPerson == '00e10000000dzzG' ||loginPerson == '00e10000000Y3o5'){
                            tableData.add(new AddressData(dataList[i],'inline','inline','inline'));
@@ -416,10 +434,12 @@
     */
    public PageReference save(){
        system.debug('Address value:'+JSON.serialize(insUpdData));       
        if(String.isNotBlank(contactIdValue)&&String.isNotEmpty(contactIdValue)){
            system.debug('Contact Value:'+contactIdValue);
            insUpdData.Contacts__c = contactIdValue;
        }
        // WLIG-CCX4BB 【委托】修理直返收货地址功能改造 thh 20220513 start
        // if(String.isNotBlank(contactIdValue)&&String.isNotEmpty(contactIdValue)){
        //     system.debug('Contact Value:'+contactIdValue);
        //     insUpdData.Contacts__c = contactIdValue;
        // }
        // WLIG-CCX4BB 【委托】修理直返收货地址功能改造 thh 20220513 end
        system.debug('Address value:'+JSON.serialize(insUpdData));
        savepoint sp = Database.setsavepoint();
        if(insUpdData != null){
@@ -450,6 +470,7 @@
                this.insUpdData.Telephone__c.addError('不能为空!');
            }
            //详细地址不能为空
            System.debug('insUpdData.Detailed_Address__c1:' + insUpdData.Detailed_Address__c);
            if(String.isBlank(insUpdData.Detailed_Address__c)){
                flag = false;
                this.insUpdData.Detailed_Address__c.addError('详细地址不能为空!');
@@ -495,65 +516,70 @@
                    }
                }
            }
            //判断联系人是否都为空
            if(insUpdData.Contacts__c == null || String.isBlank(insUpdData.Contacts__c)){
            //WLIG-CCX4BB 【委托】修理直返收货地址功能改造 thh 20220513 start
            //判断联系人是否为空
            System.debug('insUpdData.ContactName__c1:' + insUpdData.ContactName__c);
            if(String.isBlank(insUpdData.ContactName__c)){
                this.insUpdData.ContactName__c.addError('【联系人】不能为空!');
                //判断新建联系人是否为空
                if(insUpdData.Create_Contacts__c == null || String.isBlank(insUpdData.Create_Contacts__c)){
                    flag = false;
                    this.insUpdData.Create_Contacts__c.addError('【联系人】和【联系人(新建)】不能同时为空!');
                }else{//如果联系人为空,但是新建联系人不为空,那么新增一条联系人数据
                    if(flag){
                        String typeL = '';//类型
                        if(insUpdData.Address_Classification__c == '医院'){
                            typeL = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();//医師
                        }else if(insUpdData.Address_Classification__c == '经销商'){
                            typeL = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();//販売店
                        }else if(insUpdData.Address_Classification__c == '办事处'){
                            typeL = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Internal_staff').getRecordTypeId();//社内员工
                        }
                        String surname = String.valueOf(insUpdData.Create_Contacts__c);//姓
                        String monicker = '';//名
                        if(surname.length() >= 2){
                            monicker = surname.substring(1,surname.length());
                            surname = surname.substring(0,1);
                        }
                        System.debug('newCon = '+newCon);
                        addContact = new Contact(LastName = surname,
                                                FirstName = monicker,
                                                AccountId = insUpdData.Customer__c,
                                                RecordTypeId = typeL,
                                                AWS_Data_Id__c = newCon.AWS_Data_Id__c,
                                                LastName_Encrypted__c = newCon.LastName_Encrypted__c);
                        try{
                            //新增一条联系人数据
                            insert addContact;
                            sfRecordId = addContact.id;
                            insUpdData.Create_Contacts__c = '';
                        }catch(Exception e){
                            //ApexPages.addMessages(e);
                            Database.rollback(sp);
                            //失败提示
                            System.debug('myException caught when save or submit: ' + e.getMessage() + e.getStackTraceString());
                            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
                            isSearchBtn = false;
                            return null;
                        }
                        insUpdData.Contacts__c = addContact.id;
                        //name赋值
                        /*if(insUpdData.Contacts__c == null){
                            insUpdData.name =  insUpdData.Customer__r.Name+'_'+insUpdData.Contacts__r.Name;
                        }else{
                            insUpdData.name =  insUpdData.Customer__r.Name+'_'+insUpdData.Create_Contacts__c;
                        }*/
                    }
                }
            }else{//联系人和新建联系人不能都有值
                //判断新建联系人是否为空
                if(!String.isBlank(insUpdData.Create_Contacts__c)){
                    flag = false;
                    this.insUpdData.Create_Contacts__c.addError('【联系人】和【联系人(新建)】不能同时都有值!');
                }
                // if(insUpdData.Create_Contacts__c == null || String.isBlank(insUpdData.Create_Contacts__c)){
                //     flag = false;
                //     this.insUpdData.Create_Contacts__c.addError('【联系人】和【联系人(新建)】不能同时为空!');
                // }else{//如果联系人为空,但是新建联系人不为空,那么新增一条联系人数据
                //     if(flag){
                //         String typeL = '';//类型
                //         if(insUpdData.Address_Classification__c == '医院'){
                //             typeL = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();//医師
                //         }else if(insUpdData.Address_Classification__c == '经销商'){
                //             typeL = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();//販売店
                //         }else if(insUpdData.Address_Classification__c == '办事处'){
                //             typeL = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Internal_staff').getRecordTypeId();//社内员工
                //         }
                //         String surname = String.valueOf(insUpdData.Create_Contacts__c);//姓
                //         String monicker = '';//名
                //         if(surname.length() >= 2){
                //             monicker = surname.substring(1,surname.length());
                //             surname = surname.substring(0,1);
                //         }
                //         System.debug('newCon = '+newCon);
                //         addContact = new Contact(LastName = surname,
                //                                 FirstName = monicker,
                //                                 AccountId = insUpdData.Customer__c,
                //                                 RecordTypeId = typeL,
                //                                 AWS_Data_Id__c = newCon.AWS_Data_Id__c,
                //                                 LastName_Encrypted__c = newCon.LastName_Encrypted__c);
                //         try{
                //             //新增一条联系人数据
                //             insert addContact;
                //             sfRecordId = addContact.id;
                //             insUpdData.Create_Contacts__c = '';
                //         }catch(Exception e){
                //             //ApexPages.addMessages(e);
                //             Database.rollback(sp);
                //             //失败提示
                //             System.debug('myException caught when save or submit: ' + e.getMessage() + e.getStackTraceString());
                //             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
                //             isSearchBtn = false;
                //             return null;
                //         }
                //         insUpdData.Contacts__c = addContact.id;
                //         //name赋值
                //         /*if(insUpdData.Contacts__c == null){
                //             insUpdData.name =  insUpdData.Customer__r.Name+'_'+insUpdData.Contacts__r.Name;
                //         }else{
                //             insUpdData.name =  insUpdData.Customer__r.Name+'_'+insUpdData.Create_Contacts__c;
                //         }*/
                //     }
                // }
            }
            // else{//联系人和新建联系人不能都有值
            //     //判断新建联系人是否为空
            //     if(!String.isBlank(insUpdData.Create_Contacts__c)){
            //         flag = false;
            //         this.insUpdData.Create_Contacts__c.addError('【联系人】和【联系人(新建)】不能同时都有值!');
            //     }
            // }
            //WLIG-CCX4BB 【委托】修理直返收货地址功能改造 thh 20220513 end
            if(flag){
                try{
                    //新增或修改数据
@@ -584,14 +610,42 @@
    //查询修理表数据
    private String makeTextRepairSql(String uuid){
        String RepairSql = 'SELECT ID,NAME,HP_ID__c,HP_Name__c,Delivered_Product__c,FSE_ApplyForRepair_time__c,SAP_Transfer_time__c,Dealer__c,Dealer__r.Name,Returns_Product_way__c,Address_Type_Index__c,AWS_Data_Id__c FROM Repair__c where id = \''+RepairId+'\'';
        String RepairSql = 'SELECT ID,NAME,HP_ID__c,HP_Name__c,Delivered_Product__c,FSE_ApplyForRepair_time__c,SAP_Transfer_time__c,Dealer__c,Dealer__r.Name,Returns_Product_way__c,Address_Type_Index__c,AWS_Data_Id__c,Incharge_Staff__r.UserRole.name,Incharge_Staff__r.Branch__c,Incharge_Staff__r.Profile.name FROM Repair__c where id = \''+RepairId+'\'';
        return RepairSql;
    }
    //查询地址表数据
    private String makeTextAddressSql(String typeSearchId) {
        String AddressSql = 'SELECT AWS_Data_Id__c,ID,Address_Classification__c,Customer__c,Customer__r.Name,Customer__r.Type,Customer__r.RecordTypeId,Contacts__c,Contacts__r.Name,Contacts__r.AWS_Data_Id__c,Telephone__c'
                        +',Province__c,Province__r.Name,City__c,City__r.name,Detailed_Address__c,Create_Contacts__c,ZipCode__c,CreatedByid,Detailed_Address_Encrypted__c,Telephone_Encrypted__c,ZipCode_Encrypted__c,Contacts__r.LastName_Encrypted__c'
        // DB202212270703 备品修理直返地址项目导入 start
        String beipCenter = '';
        if (pc.Returns_Product_way__c == '备品中心') {
            if (pc.Incharge_Staff__r.Profile.name.startsWith('2B2')) {
                if (pc.Incharge_Staff__r.Branch__c == '北京') {
                    beipCenter = '北京备品中心';
                }else if (pc.Incharge_Staff__r.Branch__c == '上海') {
                    beipCenter = '华东备品中心';
                }else if (pc.Incharge_Staff__r.Branch__c == '广州') {
                    beipCenter = '广州备品中心';
                }
            }else if (pc.Incharge_Staff__r.UserRole.name == 'CTEC教育本部') {
                if (pc.Incharge_Staff__r.Branch__c == '北京') {
                    beipCenter = '北京C-TEC';
                }else if (pc.Incharge_Staff__r.Branch__c == '上海') {
                    beipCenter = '上海C-TEC';
                }else if (pc.Incharge_Staff__r.Branch__c == '广州') {
                    beipCenter = '广州C-TEC';
                }
            }
        }
        // DB202212270703 备品修理直返地址项目导入 end
        // WLIG-CCX4BB 【委托】修理直返收货地址功能改造 thh 20220513 start
        // String AddressSql = 'SELECT AWS_Data_Id__c,ID,Address_Classification__c,Customer__c,Customer__r.Name,Customer__r.Type,Customer__r.RecordTypeId,Contacts__c,Contacts__r.Name,Contacts__r.AWS_Data_Id__c,ContactName__c,Telephone__c,ContactName_Encrypted__c' //zhj 新方案改造 去除Encrypted__c 2022-12-05
        //                 +',Province__c,Province__r.Name,City__c,City__r.name,Detailed_Address__c,Create_Contacts__c,ZipCode__c,CreatedByid,Detailed_Address_Encrypted__c,Telephone_Encrypted__c,ZipCode_Encrypted__c,Contacts__r.LastName_Encrypted__c'
        //                 + ' FROM Address__c where id != null';
        String AddressSql = 'SELECT AWS_Data_Id__c,ID,Address_Classification__c,Customer__c,Customer__r.Name,Customer__r.Type,Customer__r.RecordTypeId,Contacts__c,Contacts__r.Name,Contacts__r.AWS_Data_Id__c,ContactName__c,Telephone__c' //zhj 新方案改造 去除Encrypted__c 2022-12-05
                        +',Province__c,Province__r.Name,City__c,City__r.name,Detailed_Address__c,Create_Contacts__c,ZipCode__c,CreatedByid'
                        + ' FROM Address__c where id != null';
        // WLIG-CCX4BB 【委托】修理直返收货地址功能改造 thh 20220513 end
        //类型筛选
        if(!String.isBlank(typeText)){
            if('医院'.equals(typeText)){
@@ -604,6 +658,12 @@
            }
            if('办事处'.equals(typeText)){
                AddressSql += ' and Address_Classification__c LIKE \'%' + typeText.trim() + '%\'' ;
            }
            if('备品'.equals(typeText)){
                AddressSql += ' and Address_Classification__c LIKE \'%' + typeText.trim() + '%\'' ;
                if (String.isNotBlank(beipCenter)) {
                    AddressSql += ' and Beipin_Center__c = \'' + String.escapeSingleQuotes(beipCenter) +'\'' ;
                }
            }
            if('经销商'.equals(typeText)){
                if(String.isBlank(typeSearchId)){
@@ -673,6 +733,51 @@
                        + ' RecordType.name,Parent.Parent.Id FROM Account WHERE ID = \'' + accId + '\'';
        return AccountSql;
    }
    @RemoteAction
    global static string SaveEx(string repaire_json,string asset_json, string address_id){
        return SaveExCore(repaire_json,asset_json,address_id);
    }
    public static string SaveExCore(string repaire_json,string asset_json, string address_id){
        string message = null;
        string savemessage = null;
        savepoint sp = Database.setsavepoint();
        try {
            Repair__c r = (Repair__c)JSON.deserialize(repaire_json,Repair__c.class);
            Asset ast = (Asset)JSON.deserialize(asset_json,Asset.class);
            Address__c add = new Address__c(Id=address_id,Using_Datetime__c = Datetime.now());
            update r;
            update ast;
            update add;
        }catch(DmlException e) {
            Integer index = 0;
            System.debug(e.getNumDml());
            System.debug(e.getDmlFields(index));
            System.debug(e.getDmlId(index));
            System.debug(e.getDmlIndex(index));
            System.debug(e.getDmlMessage(index));
            System.debug(e.getDmlStatusCode(index));
            System.debug(e.getDmlType(index));
            system.debug(e.getMessage());
            system.debug(e.getStackTraceString());
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            message ='保存失败,原因:'+ e.getDmlMessage(index);
            savemessage = e.getMessage()+e.getStackTraceString();
        } catch (Exception e) {
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            savemessage = e.getMessage()+e.getStackTraceString();
            message = '保存失败';
        } finally {
            PIHelper.saveTransLog('StraightBackAddressController',address_id,address_id,address_id, repaire_json ,'success',message);
        }
        return message;
    }
    /**
     * 为了方便前段table获取值