沙世明
2022-09-13 bfca7a84bec815da594f1d12558535ed06d2490b
force-app/main/default/classes/DealerPersonnelController.cls
@@ -1,4 +1,4 @@
public with sharing class DealerPersonnelController {
public without sharing class DealerPersonnelController {
    public DealerPersonnelController() {
    }
@@ -6,6 +6,7 @@
    public static String processData(String fileData,String sobjectName,List<String> fields) {
        system.debug('fileData===============>'+fileData);
        String errorMage = '';
        Savepoint sp = Database.setSavepoint();
        try {
            if(fileData != null){
                String[] fileLines = new String[]{};
@@ -13,77 +14,147 @@
                system.debug('fileLines===================>'+fileLines);
                // 经销商管理编码
                List<String> managementCodeList = new List<String>();  
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                // 客户名
                List<String> CustomerNameList = new List<String>();
                //人员管理编码
                List<String> CManageCodeList = new List<String>();
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                // 导入的数据
                List<List<String>> inputList = new List<List<String>>();
                for (Integer i = 1,j = fileLines.size(); i < j; i++) {
                    List<String> inputValues = new List<String>();
                    inputValues = fileLines[i].split(',');
                    System.debug('inputValues=============>'+inputValues);
                    if(inputValues != null){
                        if(inputValues[0] == '' || inputValues[0]== null){
                            errorMage += 'errorMage: 第' + i + '行,姓名不能为空!';
                            errorMage += '=';
                        }
                        if(inputValues[1] == '' || inputValues[1] ==null){
                            errorMage += 'errorMage: 第' + i + '行,手机不能为空!';
                            errorMage += '=';
                    if(fileLines[i].replace(',','').trim()!=''){
                        inputValues = fileLines[i].split(',',-1);
                        System.debug('inputValues=============>'+inputValues);
                        System.debug('fileLines[i]=============>'+fileLines[i]);
                        if(inputValues != null){
                            if(inputValues[0] == '' || inputValues[0]== null){
                                errorMage += 'errorMage: 第' + i + '行,姓名不能为空!';
                                errorMage += '=';
                            }
                            // if(inputValues[1] == '' || inputValues[1] ==null){
                            //     errorMage += 'errorMage: 第' + i + '行,手机不能为空!';
                            //     errorMage += '=';
                            // }
                            // boolean mobileNumberFlag = mobileNumberVerification(inputValues[1]);
                            // if(mobileNumberFlag == false){
                            //     errorMage += 'errorMage: 你的手机号码不太正确,请检查您的手机是否正确!';
                            //     errorMage += '=';
                            // }
                            // if(inputValues[2] == '' || inputValues[2] == null){
                            //     errorMage += 'errorMage: 第' + i + '行,邮箱不能为空!';
                            //     errorMage += '=';
                            // }
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                            if(inputValues[2] != '' && inputValues[2] != null){
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                                boolean mailboxFlag = mailboxVerification(inputValues[2]);
                                if(mailboxFlag == false){
                                    errorMage += 'errorMage: 你的邮箱不太正确,请检查您的邮箱格式!';
                                    errorMage += '=';
                                }
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                            }
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                            if(inputValues[3] == '' || inputValues[3] == null){
                                errorMage += 'errorMage: 第' + i + '行,经销商管理编码不能为空!';
                                errorMage += '=';
                            }
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                            if(inputValues[4] == '' || inputValues[4] == null){
                                errorMage += 'errorMage: 第' + i + '行,客户名不能为空!';
                                errorMage += '=';
                            }
                            if(inputValues[5] == '' || inputValues[5] == null){
                                errorMage += 'errorMage: 第' + i + '行,经销商用户不能为空!';
                                errorMage += '=';
                            }
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                            inputValues[3]=inputValues[3].leftpad(10,'0');
                            System.debug('测试去零1 '+inputValues[3].replace(' ','').length());
                            managementCodeList.add(inputValues[3]);
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                            CustomerNameList.add(inputValues[4]);
                            CManageCodeList.add(inputValues[6]);
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                            inputList.add(inputValues);
                            //导入的数据
                            System.debug('inputValues[0]===============>'+ inputValues[0]);
                            System.debug('inputValues[1]===============>'+ inputValues[1]);
                            System.debug('inputValues[2]===============>'+ inputValues[2]);
                            System.debug('inputValues[3]===============>'+ inputValues[3]);
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                            System.debug('inputValues[4]===============>'+ inputValues[4]);
                            System.debug('inputValues[4]===============>'+ inputValues[5]);
                            System.debug('inputValues[6]===============>'+ inputValues[6]);
                            System.debug('inputValues[7]===============>'+ inputValues[7]);
                            System.debug('inputValues[8]===============>'+ inputValues[8]);
                            System.debug('inputValues[9]===============>'+ inputValues[9]);
                            System.debug('inputValues[10]===============>'+ inputValues[10]);
                            System.debug('inputValues[11]===============>'+ inputValues[11]);
                            System.debug('inputValues[12]===============>'+ inputValues[12]);
                            System.debug('inputValues[13]===============>'+ inputValues[13]);
                            System.debug('inputValues[14]===============>'+ inputValues[14]);
                            System.debug('inputValues[15]===============>'+ inputValues[15]);
                            System.debug('inputValues[16]===============>'+ inputValues[16]);
                            //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                        }
                        boolean mobileNumberFlag = mobileNumberVerification(inputValues[1]);
                        if(mobileNumberFlag == false){
                            errorMage += 'errorMage: 你的手机号码不太正确,请检查您的手机是否正确!';
                            errorMage += '=';
                        }
                        if(inputValues[2] == '' || inputValues[2] == null){
                            errorMage += 'errorMage: 第' + i + '行,邮箱不能为空!';
                            errorMage += '=';
                        }
                        boolean mailboxFlag = mailboxVerification(inputValues[2]);
                        if(mailboxFlag == false){
                            errorMage += 'errorMage: 你的邮箱不太正确,请检查您的邮箱格式!';
                            errorMage += '=';
                        }
                        if(inputValues[3] == '' || inputValues[3] == null){
                            errorMage += 'errorMage: 第' + i + '行,经销商管理编码不能为空!';
                            errorMage += '=';
                        }
                        managementCodeList.add(inputValues[3]);
                        inputList.add(inputValues);
                        //导入的数据
                        System.debug('inputValues[0]===============>'+ inputValues[0]);
                        System.debug('inputValues[1]===============>'+ inputValues[1]);
                        System.debug('inputValues[2]===============>'+ inputValues[2]);
                        System.debug('inputValues[3]===============>'+ inputValues[3]);
                    }
                }
                Map<String,Account> accountIdMap = new Map<String,Account>();
                List<Account> accountList = [select id, Name,Management_Code__c from Account where Management_Code__c in: managementCodeList];
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                Map<String,Contact> contactIdMap = new Map<String,Contact>();
                Map<Id,String> ManagementCodeMap = new Map<Id,String>();
                List<Id> AccountIdList =new  List<Id>();
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                List<Account> accountList = [select id, Name,Management_Code__c from Account where Management_Code__c in: managementCodeList and Name in:CustomerNameList ];
                for(Account ac : accountList){ 
                    accountIdMap.put(ac.Management_Code__c,ac);
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                    AccountIdList.add(ac.Id);
                    ManagementCodeMap.put(ac.Id,ac.Management_Code__c);
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                }
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                //查询客户人员是否有数据
                List<Contact> contactList = [select id, CManageCode__c,AccountId from Contact where AccountId in: AccountIdList and CManageCode__c in:CManageCodeList ];
                for(Contact cc : contactList){
                    String contactKey=ManagementCodeMap.get(cc.AccountId)+'_'+cc.CManageCode__c;
                    contactIdMap.put(contactKey,cc);
                }
                List<Contact> cnupdateList = new List<Contact>();
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                List<Contact> cnList = new List<Contact>();
                Integer lineNumber = 1;
                for(List<String> lineList :inputList){
                    Contact con = new Contact();
                    //暂时导入的是经销商用户,所以记录类型只有经销商,暂时写死;后期如果有变动再更改
                    con.RecordTypeId = '01210000000QfWiAAK';
                    if(lineList[0] != null && lineList[0] != ''){
                        con.LastName  = lineList[0];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,姓氏'+lineList[0]+'不存在';
                        errorMage += '=';
                    }
                    if(lineList[1] != null && lineList[1] != ''){
                        if(mobileNumberVerification(lineList[1])){
                            con.MobilePhone = lineList[1];
                    if(!contactIdMap.containsKey(lineList[3]+'_'+lineList[6])){
                        if(lineList[0] != null && lineList[0] != ''){
                            con.LastName  = lineList[0];
                        }else{
                            errorMage += 'error: 第'+ lineNumber +'行数据,手机'+lineList[1]+'不正确!';
                            errorMage += 'error: 第'+ lineNumber +'行数据,姓氏'+lineList[0]+'不存在';
                            errorMage += '=';
                        }
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,手机'+lineList[1]+'不存在';
                        errorMage += '=';
                        }
                    }
                    if(lineList[1] != null && lineList[1] != ''){
                        // if(mobileNumberVerification(lineList[1])){
                            con.MobilePhone = lineList[1];
                        // }else{
                        //     errorMage += 'error: 第'+ lineNumber +'行数据,手机'+lineList[1]+'不正确!';
                        //     errorMage += '=';
                        // }
                    }
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                    // else{
                    //     errorMage += 'error: 第'+ lineNumber +'行数据,手机'+lineList[1]+'不存在';
                    //     errorMage += '=';
                    // }
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                    if(lineList[2] != null && lineList[2] != ''){
                        if(mailboxVerification(lineList[2])){
                            con.Email = lineList[2];
@@ -91,39 +162,163 @@
                            errorMage += 'error: 第'+ lineNumber +'行数据,电子邮件'+lineList[2]+'不正确!';
                            errorMage += '=';
                        }
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,电子邮件'+lineList[2]+'不存在';
                        errorMage += '=';
                    }
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                    // else{
                    //     errorMage += 'error: 第'+ lineNumber +'行数据,电子邮件'+lineList[2]+'不存在';
                    //     errorMage += '=';
                    // }
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                    if(accountIdMap.containsKey(lineList[3])){
                        con.AccountId = accountIdMap.get(lineList[3]).id;
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,经销商编码'+lineList[3]+'不存在';
                        errorMage += 'error: 第'+ lineNumber +'行数据,经销商编码'+lineList[3]+'或者客户名不存在';
                        errorMage += '=';
                    }
                    //暂定创建直接为 true
                    con.Agency_User__c =  true;
                    // con.Agency_User__c =  true;
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                    if(lineList[5] != null && lineList[5] != ''){
                        if(lineList[5]=='true'||lineList[5]=='TRUE'){
                            con.Agency_User__c=true;
                        }else{
                            con.Agency_User__c=false;
                        }
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,经销商用户'+lineList[0]+'不存在';
                        errorMage += '=';
                    }
                     //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                    //经销商专员类型
                    if(VerificationOLYAssistantType(lineList[7])){
                        con.OLY_Assistant_Type__c  = lineList[7];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,经销商专员类型'+lineList[7]+'不正确!';
                        errorMage += '=';
                    }
                    //入职年份
                    if(VerificationDateType(lineList[8])){
                        con.InductionYear__c  = ConversionDateType(lineList[8]);
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,入职年份'+lineList[8]+'格式不正确!';
                        errorMage += '=';
                    }
                    //GIR认证级别
                    if(VerifyCertificationLevel(lineList[9])){
                        con.GIR_CertificationLevel__c  = lineList[9];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,GIR认证级别'+lineList[9]+'不正确!';
                        errorMage += '=';
                    }
                    //GIR认证时间
                    if(VerificationDateType(lineList[10])){
                        con.GIR_CertificationTime__c  = ConversionDateType(lineList[10]);
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,GIR认证时间'+lineList[10]+'格式不正确!';
                        errorMage += '=';
                    }
                    //SP认证级别
                    if(VerifyCertificationLevel(lineList[11])){
                        con.SP_CertificationLevel__c  = lineList[11];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,SP认证级别'+lineList[11]+'不正确!';
                        errorMage += '=';
                    }
                    //SP认证时间
                    if(VerificationDateType(lineList[12])){
                        con.SP_CertificationTime__c  = ConversionDateType(lineList[12]);
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,SP认证时间'+lineList[12]+'格式不正确!';
                        errorMage += '=';
                    }
                    //ET认证级别
                    if(VerifyCertificationLevel(lineList[13])){
                        con.ET_CertificationLevel__c  = lineList[13];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,ET认证级别'+lineList[13]+'不正确!';
                        errorMage += '=';
                    }
                    //ET认证时间
                    if(VerificationDateType(lineList[14])){
                        con.ET_CertificationTime__c  = ConversionDateType(lineList[14]);
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,ET认证时间'+lineList[14]+'格式不正确!';
                        errorMage += '=';
                    }
                    //ENG认证级别
                    if(VerifyCertificationLevel(lineList[15])){
                        con.ENG_CertificationLevel__c  = lineList[15];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,ENG认证级别'+lineList[15]+'不正确!';
                        errorMage += '=';
                    }
                    //ENG认证时间
                    if(VerificationDateType(lineList[16])){
                        con.ENG_CertificationTime__c  = ConversionDateType(lineList[16]);
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,ENG认证时间'+lineList[16]+'格式不正确!';
                        errorMage += '=';
                    }
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                    lineNumber++;
                    cnList.add(con);
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                    System.debug('001+'+lineList[3]+'_'+lineList[6]);
                    System.debug('002+'+contactIdMap.get(lineList[3]+'_'+lineList[6]));
                    if(contactIdMap.containsKey(lineList[3]+'_'+lineList[6])){
                        con.Id=contactIdMap.get(lineList[3]+'_'+lineList[6]).Id;
                        cnupdateList.add(con);
                    }else {
                        cnList.add(con);
                    }
                    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
                    // cnList.add(con);
                }
                if(errorMage != ''){
                    system.debug('errorMage==========>'+errorMage);
                    return errorMage;
                }
                // 新增周报明细
                if(cnList.size() > 0 ){
                    System.debug('003+'+cnList);
                    insertContact(cnList);
                }
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
                // 更新周报明细
                if(cnupdateList.size() > 0 ){
                    System.debug('004+'+cnupdateList);
                    updateContact(cnupdateList);
                }
                //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
            }
            return 'success';
        } catch (Exception e) {
            System.debug('exception'+e);
            return 'exception'+e;
            Database.rollback(sp);
            System.debug('exception++++++++++++++++++'+e.getLineNumber()+e);
            String errer=String.valueOf(e);
            String errerContactiD='';
            String errerContactName='';
            if(errer.contains('UniqueNumber__c 与记录值重复')){
                List<String> errerList = errer.split('UniqueNumber__c 与记录值重复');
                if(errerList.size()>1){
                    errerContactiD=errerList[1].substring(errerList[1].indexOf('ID 为:')+5,errerList[1].indexOf(': []')).replace(' ','');
                    List<Contact> repeatContact=[select Name from Contact where Id = :errerContactiD];
                    if(repeatContact.size()>0){
                        errerContactName=repeatContact[0].Name;
                        return 'error: 数据中存在与系统现有用户 '+errerContactName+'(Id为:'+errerContactiD+')重复的电话号码';
                    }else{
                        return 'error: 数据中存在重复的电话号码';
                    }
                }
            }
            return e.getLineNumber()+'exception'+e;
        }
    }
      // 电子邮件的验证
    public static boolean mailboxVerification(String mailbox){
        String  check =  '^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$';
        String  check =  '^([a-z0-9A-Z]+[_-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$';
        Pattern regex = Pattern.compile(check);  
        Matcher matcher = regex.matcher(mailbox);  
        if (matcher.matches()){
@@ -132,18 +327,58 @@
        return false;
    }
      // 手机号的验证
    public static boolean mobileNumberVerification(String phoneNumber){
        String check = '^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$';
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(phoneNumber);
        if(matcher.matches()){
            return true;
        }
        return false;
    }
    // public static boolean mobileNumberVerification(String phoneNumber){
    //     String check = '^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$';
    //     Pattern regex = Pattern.compile(check);
    //     Matcher matcher = regex.matcher(phoneNumber);
    //     if(matcher.matches()){
    //         return true;
    //     }
    //     return false;
    // }
    //新增客户人员;
    public static void insertContact(List<Contact> data) {
        insert data;
    }
   // SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy start
    //校验认证级别;
    public static boolean VerifyCertificationLevel(String data) {
        if(data=='A'||data=='B'||data==''){
            return true;
        }
        return false;
    }
    //校验日期类型
    public static Boolean VerificationDateType(String data) {
        List<String> date1str;
        List<String> date2str;
        if(data==''){
            return true;
        }
        date1str=data.split('/');
        date2str=data.split('-');
        if(date1str.size()>1||date2str.size()>1){
            return true;
        }
        return false;
    }
    //转换日期类型;
    public static Date ConversionDateType(String data) {
        if(data==''){
            return null;
        }
        return Date.valueOf(data.replace('/','-'));
    }
    //校验经销商专员类型;
    public static Boolean VerificationOLYAssistantType(String data) {
        if(data=='GR专员'||data=='SP专员'||data=='GRSP专员'||data=='ET专员'||data=='TB专员'||data=='奥辉基层专员'||data=='商务助理'||data=='*****'||data==''){
            return true;
        }
        return false;
    }
    //更新客户人员;
    public static void updateContact(List<Contact> data){
        update data;
    }
    //SWAG-CEW85A 【委托】消耗品&DAMS系统经销商人员管理——信息完善;fy end
}