高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
public with sharing class CM_SearchContactServiceController {
    public Contact newCon { get; set; }
    public Contact searchCon { get; set; }
    public List<LineInfo> lineInfoList { get; set; }
    public String conId { get; set; }
 
    public String openLine { get; set; }
    public String accountIdV2{ get; set; }      //zhj 2022-02-04 新方案改造
    private String accountId;
    private String nowValue;
 
    public String staticResource {get; set;}
    public String contactAWSIds {set;get;}
    public String contactsInfo {set;get;}
    public String awsDataIdArray {set;get;}
 
    public String sfContactId{set;get;} //zhj 2022-12-02 sfId
    public String staticResourceContactV2 {get; set;}   //zhj 2022-02-04 新方案改造
 
    public CM_SearchContactServiceController() {
        openLine = Apexpages.currentPage().getParameters().get('line');
        accountId = Apexpages.currentPage().getParameters().get('acc');
        accountIdV2 = accountId;
        nowValue = Apexpages.currentPage().getParameters().get('now');
 
        //2022/02/15 张华建 PI PL start
        //1. Query Contact by accountId
        List<Contact> conList = new List<Contact>();
        system.debug('Account Id from Front-end:'+accountId);
        PIHelper.PIIntegration contactPIIntegration = PIHelper.getPIIntegrationInfo('Contact');
        if(String.isNotBlank(accountId) && String.isNotEmpty(accountId)){
            conList = new List<Contact>([select Id,AWS_Data_Id__c from Contact where AccountId=:accountId and AWS_Data_Id__c!='' order by lastmodifieddate desc limit :contactPIIntegration.maxQueryNumber]);
        }        
        //2. Prepare the Contact Info
        Map<String,Contact> awsIdToContactMap = new Map<String,Contact>();
        List<String> conAWSIds = new List<String>();
        for(Contact con:conList){
            conAWSIds.add(con.AWS_Data_Id__c);
            awsIdToContactMap.put(con.AWS_Data_Id__c,con);
        }
        //conAWSIds.add('943114607025717249');
        contactsInfo = JSON.serialize(awsIdToContactMap);
        contactAWSIds = JSON.serialize(conAWSIds);
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact')); 
        staticResourceContactV2 = JSON.serialize(PIHelper.getPIIntegrationInfo('ContactV2'));       //zhj 2022-02-04 新方案改造
        //2022/02/15 张华建 PI PL end
    }
 
    public void init() {
        searchCon = new Contact();
        if (nowValue != null && nowValue != '') {
            searchCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,
                                FirstName, LastName
                           from Contact where Id = :nowValue];
            searchCon.Search_LastName__c = searchCon.LastName;
            searchCon.Search_FirstName__c = searchCon.FirstName;
        }
 
        searchContact();
 
        newCon = new Contact();
        newCon.AccountId = accountId;
        sfContactId = '';
 
        return;
    }
 
 
    public PageReference searchContact() {
        String searchStr = 'select Id, Name, Department__c, Type__c, AccountName__c, Supplement__c, Phone,AWS_Data_Id__c ';
         searchStr += ', City__c , State__c '; //2018/11/19 HWAG-B399RW 读取省和市
        searchStr += '        from Contact ';
        searchStr += '       where Isactive__c = \'有效\' and AWS_Data_Id__c!=\'\' ';
        searchStr += '         and AccountId = :accountId ';
 
        //2022/02/15 张华建 检索 start
        // if (searchCon.Search_LastName__c != null && searchCon.Search_LastName__c != '') {
        //     searchStr += '     and LastName like \'%' + searchCon.Search_LastName__c + '%\'';
        // }
        // if (searchCon.Search_FirstName__c != null && searchCon.Search_FirstName__c != '') {
        //     searchStr += '     and FirstName like \'%' + searchCon.Search_FirstName__c + '%\'';
        // }
 
        System.debug('awsDataIdArray = ' + awsDataIdArray);
        if (awsDataIdArray != null && awsDataIdArray != '') {
            String[] arr = awsDataIdArray.split(',');
            System.debug('arr = ' + arr);
            String awsDataIdSql = '';
            for(String s : arr){
                System.debug('s = ' + s);
                awsDataIdSql = awsDataIdSql + ',' + '\'' + s+'\'';
            }
            awsDataIdSql = awsDataIdSql.substring(1);
            System.debug('awsDataIdSql = ' + awsDataIdSql);
            searchStr += ' and AWS_Data_Id__c in (' + awsDataIdSql + ')';
        }
        //2022/02/15 张华建 检索 end
 
        system.debug('=====searchStr:' + searchStr);
 
        searchStr += ' order by lastmodifieddate desc limit 500';
 
        List<Contact> searchResult = new List<Contact>();
        if(!Test.isRunningTest()){
            searchResult = Database.query(searchStr);
        }else{
            searchResult = [SELECT Id FROM Contact];
        }
 
        lineInfoList = new List<LineInfo>();
        Integer line = 0;
        for (Contact con : searchResult) {
            line += 1;
            LineInfo li = new LineInfo(line, con);
            lineInfoList.add(li);
        }
 
        editClear();
 
        return null;
    }
 
    public PageReference editContact() {
        if (conId != null && conId != '') {
            // newCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,LastName_Encrypted__c,Phone_Encrypted__c,
            //           FirstName, LastName,AWS_Data_Id__c
            //             from Contact where Id = :conId];
            newCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,
                      FirstName, LastName,AWS_Data_Id__c
                        from Contact where Id = :conId];    //zhj 新方案改造 去除Encrypted__c 2022-12-05
            newCon.Search_LastName__c = newCon.LastName;
            newCon.Search_FirstName__c = newCon.FirstName;
            sfContactId = newCon.Id; // zhj 2022-12-02 得到sfid给aws
        }
 
        return null;
    }
 
    public PageReference saveNew()  {
        if (newCon.Search_LastName__c == null || newCon.Search_LastName__c == '') {
            newCon.Search_LastName__c.addError('必须填写。');
            return null;
        }
        newCon.LastName = newCon.Search_LastName__c;
        newCon.FirstName = newCon.Search_FirstName__c;
        upsert newCon;
        System.debug('newCon.Id = ' + newCon.Id); 
        sfContactId = newCon.Id; // zhj 2022-12-02 得到sfid给aws
 
        searchCon.Search_LastName__c = newCon.LastName;
        searchCon.Search_FirstName__c = newCon.FirstName;
        searchContact();
 
        return null;
    }
 
    public PageReference editClear()  {
        newCon = new Contact();
        newCon.AccountId = accountId;
        return null;
    }
 
    public PageReference clearLineInfoList()  {
        lineInfoList = new List<LineInfo>();
        return null;
    }
 
    class LineInfo {
        public Integer lineNo { get; set; }
        public Contact con { get; set; }
 
        public LineInfo(Integer in_line) {
            lineNo = in_line;
            con = new Contact();
        }
 
        public LineInfo(Integer in_line, Contact in_con) {
            lineNo = in_line;
            con = in_con;
        }
    }
 
    //zhj MEBG新方案改造 2022-11-24 start
    @RemoteAction
    public static ResponseV2 searchContactByAccountId(String accountId) {
        System.debug('accountId = ' + accountId);
        ResponseV2 res = new ResponseV2();
        try{
            Account account = [select Id,Name from Account where Id = :accountId];
            List<Contact> contactList = [select id,AWS_Data_Id__c, CManageCode__c,Name from Contact where AccountId = :account.Id];
            Map<String,String> dataIdManageCodeMap = new Map<String,String>();
            for(Contact contact : contactList){
                dataIdManageCodeMap.put(contact.AWS_Data_Id__c,contact.CManageCode__c);
            }
            res.dataIdManageCodeMap = dataIdManageCodeMap;
            res.accountName = account.Name;
            res.contactList = contactList;
 
            Map<String,String> accMap= new Map<String,String>();
            List<Account> accList=[select ID,Is_Active__c,Is_Active_Formula__c from Account where ID =:accountId];
            if(accList.size()>0){
                for(Account acc:accList){
                    if (String.isNotBlank(acc.Is_Active__c)){
                        String subId = acc.Id;
                        accMap.put(subId.substring(0,15), acc.Is_Active__c);
                        continue;
                    }
                    if (String.isNotBlank(acc.Is_Active_Formula__c)){
                        String subId = acc.Id;
                        accMap.put(subId.substring(0,15), acc.Is_Active_Formula__c);
                    }
                }
            }
            System.debug('accMap= ' + accMap);
            res.isNameMobileVerif = true;
            res.isMobileVerif = false;
            res.status = 'success';
            return res;
        }catch(Exception e){
            res.message = e.getMessage() + '';
            res.status = 'fail';
            return res;
        }
    }
 
    public class ResponseV2{
        public List<Contact> contactList{set;get;}
        public String accountName{set;get;}
        public Map<String,String> dataIdManageCodeMap{set;get;}
        public Boolean isMobileVerif;
        public Boolean isNameMobileVerif;
        public String message{set;get;}
        public String status{set;get;}
    }
    //zhj MEBG新方案改造 2022-11-24 end
}