高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public with sharing class DealerPersonnelController {
    public DealerPersonnelController() {
 
    }
    @AuraEnabled
    public static String processData(String fileData,String sobjectName,List<String> fields) {
        system.debug('fileData===============>'+fileData);
        String errorMage = '';
        String str = '';
        try {
            if(fileData != null){
                String[] fileLines = new String[]{};
                fileLines = fileData.split('\n');
                system.debug('fileLines===================>'+fileLines);
                // 记录类型
                List<String> recordTypeList = new List<String>();  
                // 客户名
                List<String> accountNameList = new List<String>();  
                // 导入的数据
                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(inputValues[2] == '' || inputValues[2] == null){
                            errorMage += 'errorMage: 第' + i + '行,名字不能为空!';
                            errorMage += '=';
                        } 
                        if(inputValues[3] == '' || inputValues[3] ==null){
                            errorMage += 'errorMage: 第' + i + '行,手机不能为空!';
                            errorMage += '=';
                        }
                        boolean mobileNumberFlag = mobileNumberVerification(inputValues[3]);
                    System.debug('mobileNumberFlag=============>'+mobileNumberFlag);
 
                        if(mobileNumberFlag == false){
                            errorMage += 'errorMage: 你的手机号码不太正确,请检查您的手机是否正确!';
                            errorMage += '=';
                        }
                        if(inputValues[4] == '' || inputValues[4] == null){
                            errorMage += 'errorMage: 第' + i + '行,邮箱不能为空!';
                            errorMage += '=';
                        }
                        boolean mailboxFlag = mailboxVerification(inputValues[4]);
                    System.debug('mailboxFlag=============>'+mailboxFlag);
 
                        if(mailboxFlag == false){
                            errorMage += 'errorMage: 你的邮箱不太正确,请检查您的邮箱格式!';
                            errorMage += '=';
                        }
                        if(inputValues[5] == '' || inputValues[5] == null){
                            errorMage += 'errorMage: 第' + i + '行,客户名不能为空!';
                            errorMage += '=';
                        }
                       
                        str = inputValues[5];
                        recordTypeList.add(inputValues[0]);
                        accountNameList.add(str);
                        //导入的数据
                        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]);
                        System.debug('inputValues[4]===============>'+ inputValues[4]);
                        System.debug('inputValues[5]===============>'+ inputValues[5]);
                        System.debug('inputValues[6]===============>'+ inputValues[6]);
                    }
                }
                
                Map<String,Account> accountMap = new Map<String,Account>();
                List<String> accountIdList = new List<String>();
                Map<String,RecordType> recordTypeMap = new Map<String,RecordType>();
                List<String> recordTypeIdList = new List<String>();
                List<Account> accountList = [select id, Name from Account where Name in: accountNameList];
                List<RecordType> rectCo = [select Id,Name from RecordType where IsActive = true and SobjectType = 'Contact' and Name in: recordTypeList];
                for(Account ac : accountList){ 
                    accountMap.put(ac.Name, ac);
                    accountIdList.add(ac.Id);
                }
                system.debug('accountList===============>'+accountList);
                system.debug('accountNameList===============>'+accountNameList);
                system.debug('accountMap===============>'+accountMap);
                for(RecordType re : rectCo){ 
                    recordTypeMap.put(re.Name, re);
                    recordTypeIdList.add(re.Id);
                }
                List<Contact> cnList = new List<Contact>();
                Integer lineNumber = 1;
                for(List<String> lineList :inputList){
                    Contact con = new Contact();
                    if(recordTypeMap.containsKey(lineList[0])){ 
                        con.RecordTypeId = recordTypeMap.get(lineList[0]).id;
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,记录类型'+lineList[0]+'不存在';
                        errorMage += '=';
                    }
                    if(lineList[1] != null && lineList[1] != ''){
                        con.LastName  = lineList[1];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,姓氏'+lineList[1]+'不存在';
                        errorMage += '=';
                    }   
                    if(lineList[2] != null && lineList[2] != ''){
                        con.FirstName  = lineList[2];
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,名字'+lineList[2]+'不存在';
                        errorMage += '=';
                    }  
                    if(lineList[3] != null && lineList[3] != ''){
                        if(mobileNumberVerification(lineList[3])){
                            con.MobilePhone = lineList[3];
                        }else{
                            errorMage += 'error: 第'+ lineNumber +'行数据,手机'+lineList[3]+'不正确!';
                            errorMage += '=';
                        }
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,手机'+lineList[3]+'不存在';
                        errorMage += '=';
                    }
                    if(lineList[4] != null && lineList[4] != ''){
                        if(mailboxVerification(lineList[4])){
                            con.Email = lineList[4];
                        }else{
                            errorMage += 'error: 第'+ lineNumber +'行数据,电子邮件'+lineList[4]+'不正确!';
                            errorMage += '=';
                        }
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,电子邮件'+lineList[4]+'不存在';
                        errorMage += '=';
                    }
                    if(accountMap.containsKey(lineList[5])){ 
                        con.AccountId = accountMap.get(lineList[5]).id;
                    }else{
                        errorMage += 'error: 第'+ lineNumber +'行数据,客户名'+lineList[5]+'不存在';
                        errorMage += '=';
                    }
                    if(lineList[6] == 'TRUE'){
                        con.Agency_User__c =  true; 
                    }else{
                        con.Agency_User__c =  false; 
                    }
                    lineNumber++;
                    cnList.add(con);
                }
                if(errorMage != ''){
                    system.debug('errorMage==========>'+errorMage);
                    return errorMage;
                }
                // 新增周报明细
                if(cnList.size() > 0 ){
                    insertContact(cnList);
                }
            }
            return 'success';
        } catch (Exception e) {
            System.debug('exception'+e);
            return '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,}$';  
        Pattern regex = Pattern.compile(check);  
        Matcher matcher = regex.matcher(mailbox);  
        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) {
        // System.debug('data==============>'+data);
        // List<String> lastNameList = new List<String>();
        // List<String> firstNameList = new List<String>();
        // for(Contact con:data){
        //     lastNameList.add(con.LastName);
        //     firstNameList.add(con.FirstName);
        // }
        // List<Contact> contactList = [select id, Name,Agency_User__c from contact where LastName in: lastNameList AND FirstName in: firstNameList];
        // if(contactList.size() >= 0){
        //     return '';
        // }
        // System.debug('strNameList==============>'+strNameList);
        insert data;
    }
 
}