buli
2022-05-14 ead4df22dca33a867279471821ca675f91dec760
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
public without sharing class TydelikeACController {
    public TydelikeAccount__c tya {get;set;}
    public TydelikeContact__c tyc {get;set;}
    public Account acc {get;set;}
    public String settyAcId {get;set;}
    public String settyCoId {get;set;}
    public Boolean settyof {get;set;}
    public Integer lineNum {get;set;}
    //报错及显示信息
    public String errorMessage{get;set;}
    public String accId{get;set;}
    public String accountId{get;set;}
    public String accname {get;set;}
    //url
    public String baseUrl { get; set; }
    public String rtUrl { get; set; }
    //检索
    public String tycontactname { get; set; } //联系人姓名
    public String tycountname { get; set; }  //客户姓名
    public String tycountcity { get; set; }  //客户的城市
    // Bean
    public List<DataLineBean> dataLines { get; set; }
    // 行数
    public Integer getDatalineSize() {
        return dataLines == null ? 0 : dataLines.size();
    }
 
    public TydelikeACController() {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
        if (rtUrl == null || rtUrl == 'null') {
            rtUrl = '';
        }
    }
    //页面初始化
    public PageReference init() {
        dataLines = new List<DataLineBean>();
        Map<Id,TydelikeAccount__c> tyacMap = new Map<Id,TydelikeAccount__c>();
        Map<Id,List<TydelikeContact__c>> tycoMap = new Map<Id,List<TydelikeContact__c>>();
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        List<TydelikeContact__c> tycoList = new List<TydelikeContact__c>();
        // 明细Bean
        tyacList = [select id,name,OfSkrap__c,TyCity__c,TyAddress__c,TyPostCode__c,Account1__c from TydelikeAccount__c];
        tycoList = [select id,name,OfSkrap__c,TydelikeAccount__c,TyFaxD__c,TyAddress__c,TyPhoneD__c,TyPostcode__c from TydelikeContact__c];
        if (tyacList.size() == 0) {
            return null;
        }
        if (tycoList.size() == 0) {
            return null;
        }
        for (TydelikeAccount__c tyac: tyacList) {
            tyacMap.put(tyac.Id, tyac);
        }
        for (TydelikeContact__c tyco: tycoList) {
            if (tycoMap.containsKey(tyco.TydelikeAccount__c)) {
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }else {
                tycoMap.put(tyco.TydelikeAccount__c, new List<TydelikeContact__c>());
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }  
        }
        Integer cnt = 0;
        for (TydelikeAccount__c tyac1: tyacList) {
            if (tycoMap.containsKey(tyac1.Id)) {
                for(Integer i = 0; i< tycoMap.get(tyac1.Id).size(); i++){
                DataLineBean dataLine = new DataLineBean();
                dataLine.lineNum = cnt ;
                cnt ++;
                dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                dataLine.tycoName = tycoMap.get(tyac1.Id)[i].Name;
                dataLine.tycoId = tycoMap.get(tyac1.Id)[i].Id;
                dataLine.tycoof = tycoMap.get(tyac1.Id)[i].OfSkrap__c;
                dataLine.tyaccount = tycoMap.get(tyac1.Id)[i].TydelikeAccount__c;
                dataLine.tycofax = tycoMap.get(tyac1.Id)[i].TyFaxD__c;
                dataLine.tycoadd = tycoMap.get(tyac1.Id)[i].TyAddress__c;
                dataLine.tycophone = tycoMap.get(tyac1.Id)[i].TyPhoneD__c;
                dataLine.tycopost = tycoMap.get(tyac1.Id)[i].TyPostcode__c;
                dataLines.add(dataLine);
                System.debug('dataLine.lineNum='+dataLine.lineNum);
                }
            }else {
                DataLineBean dataLine = new DataLineBean();
                dataLine.lineNum = cnt;
                cnt ++;
                dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                dataLine.tycoName = null;
                dataLine.tycoId = null;
                dataLine.tyaccount = null;
                dataLine.tycofax = null;
                dataLine.tycoadd = null;
                dataLine.tycophone = null;
                dataLine.tycopost = null;
                dataLines.add(dataLine);
                System.debug('dataLine.lineNum='+dataLine.lineNum);
            }
        }
        System.debug('dataLines='+dataLines);
        return null;
    }
    //检索
    public PageReference searchTy() {
        dataLines = new List<DataLineBean>();
        Map<Id,TydelikeAccount__c> tyacMap = new Map<Id,TydelikeAccount__c>();
        Map<Id,List<TydelikeContact__c>> tycoMap = new Map<Id,List<TydelikeContact__c>>();
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        List<TydelikeContact__c> tycoList = new List<TydelikeContact__c>();
        // 明细Bean
        String acsoql = 'select id,name,OfSkrap__c,TyCity__c,TyAddress__c,TyPostCode__c,Account1__c from TydelikeAccount__c WHERE Id != null';
 
        if (String.isNotBlank(tycountname)) {
            acsoql += ' and  Name like \'%' + String.escapeSingleQuotes(tycountname.trim().replaceAll('%', '\\%')) + '%\'';
        }
        if (String.isNotBlank(tycountcity)) {
            acsoql += ' and  TyCity__c like \'%' + String.escapeSingleQuotes(tycountcity.trim().replaceAll('%', '\\%')) + '%\'';
        }
 
        String cosoql = 'select id,name,OfSkrap__c,TydelikeAccount__c,TyFaxD__c,TyAddress__c,TyPhoneD__c,TyPostcode__c from TydelikeContact__c WHERE Id != null';
       
        if (String.isNotBlank(tycontactname)) {
            cosoql += ' and  Name like \'%' + String.escapeSingleQuotes(tycontactname.trim().replaceAll('%', '\\%')) + '%\'';
        }
 
        tyacList = Database.query(acsoql);
        tycoList = Database.query(cosoql);
        if (tyacList.size() == 0) {
            return null;
        }
        if (tycoList.size() == 0) {
            return null;
        }
        for (TydelikeAccount__c tyac: tyacList) {
            tyacMap.put(tyac.Id, tyac);
        }
        for (TydelikeContact__c tyco: tycoList) {
            if (tycoMap.containsKey(tyco.TydelikeAccount__c)) {
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }else {
                tycoMap.put(tyco.TydelikeAccount__c, new List<TydelikeContact__c>());
                tycoMap.get(tyco.TydelikeAccount__c).add(tyco);
            }
        }
        Integer cnt = 0;
        for (TydelikeAccount__c tyac1: tyacList) {
            if (tycoMap.containsKey(tyac1.Id)) {
                for(Integer i = 0; i< tycoMap.get(tyac1.Id).size(); i++){
                DataLineBean dataLine = new DataLineBean();
                dataLine.lineNum = cnt ;
                cnt ++;
                dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                dataLine.tycoName = tycoMap.get(tyac1.Id)[i].Name;
                dataLine.tycoId = tycoMap.get(tyac1.Id)[i].Id;
                dataLine.tycoof = tycoMap.get(tyac1.Id)[i].OfSkrap__c;
                dataLine.tyaccount = tycoMap.get(tyac1.Id)[i].TydelikeAccount__c;
                dataLine.tycofax = tycoMap.get(tyac1.Id)[i].TyFaxD__c;
                dataLine.tycoadd = tycoMap.get(tyac1.Id)[i].TyAddress__c;
                dataLine.tycophone = tycoMap.get(tyac1.Id)[i].TyPhoneD__c;
                dataLine.tycopost = tycoMap.get(tyac1.Id)[i].TyPostcode__c;
                dataLines.add(dataLine);
                System.debug('dataLine.lineNum='+dataLine.lineNum);
                }
            }else {
                if (String.isBlank(tycontactname)) {
                    DataLineBean dataLine = new DataLineBean();
                    dataLine.lineNum = cnt;
                    cnt ++;
                    dataLine.tyacId = tyacMap.get(tyac1.Id).Id;
                    dataLine.tyacName = tyacMap.get(tyac1.Id).Name;
                    dataLine.tyacof = tyacMap.get(tyac1.Id).OfSkrap__c;
                    dataLine.tyaccity = tyacMap.get(tyac1.Id).TyCity__c;
                    dataLine.tyacadd = tyacMap.get(tyac1.Id).TyAddress__c;
                    dataLine.tyacpost = tyacMap.get(tyac1.Id).TyPostCode__c;
                    dataLine.tycoName = null;
                    dataLine.tycoId = null;
                    dataLine.tyaccount = null;
                    dataLine.tycofax = null;
                    dataLine.tycoadd = null;
                    dataLine.tycophone = null;
                    dataLine.tycopost = null;
                    dataLines.add(dataLine);
                    System.debug('dataLine.lineNum='+dataLine.lineNum);
                }
            }
        }
        System.debug('dataLines='+dataLines);
        return null;
    }
    //删除和批量删除
    public PageReference setOfSkrapBtn() {
        List<Id> tycoofIdList = new List<Id>();
        List<Id> tyacofIdList = new List<Id>();
        for (DataLineBean d : dataLines) {
            if(d.bondel == true){
                if (d.tyaccount != null) {
                    tycoofIdList.add(d.tycoId);
                }else {
                    tyacofIdList.add(d.tyacId);
                }
            }
        }
        System.debug('tyacofIdList='+tyacofIdList);
 
        
        Map<Id,TydelikeAccount__c> uptyacMap = new Map<Id,TydelikeAccount__c>();
        List<TydelikeAccount__c> tyacofList = new List<TydelikeAccount__c>();
        tyacofList = [select id,OfSkrap__c from TydelikeAccount__c];
        if (tyacofIdList.size() > 0) {
            for (TydelikeAccount__c uptyac : tyacofList) {
                TydelikeAccount__c uptyacof = new TydelikeAccount__c();
                if (tyacofIdList.contains(uptyac.Id)) {
                    uptyacof.Id = uptyac.Id;
                    uptyacof.OfSkrap__c = true;
                    //uptyacList.add(uptyacof);
                    uptyacMap.put(uptyacof.Id,uptyacof);
                }
            }
        }
        if (uptyacMap.size() > 0) {
            update uptyacMap.values();
        }
        
        Map<Id,TydelikeContact__c> uptycoMap = new Map<Id,TydelikeContact__c>();
        List<TydelikeContact__c> tycoofList = new List<TydelikeContact__c>();
        tycoofList = [select id,OfSkrap__c from TydelikeContact__c];
        if (tycoofIdList.size() > 0) {
            for (TydelikeContact__c uptyco : tycoofList) {
                TydelikeContact__c uptycoof = new TydelikeContact__c();
                if (tycoofIdList.contains(uptyco.Id)) {
                    uptycoof.Id = uptyco.Id;
                    uptycoof.OfSkrap__c = true;
                    //uptyacList.add(uptyacof);
                    uptycoMap.put(uptycoof.Id,uptycoof);
                }
            }
        }
        if (uptycoMap.size() > 0) {
            update uptycoMap.values();
        }
        
        
        return null;
    }
 
    public class DataLineBean {
        //批量
        public Boolean bondel {get;set;}
        //行号
        public Integer lineNum {get;set;}
        //临时客户Id
        public String tyacId {get;set;}
        //临时客户名
        public String tyacName {get;set;}
        //临时客户是否删除
        public Boolean tyacof {get;set;}
        //临时客户城市
        public String tyaccity {get;set;}
        //临时客户地址
        public String tyacadd {get;set;}
        //临时客户邮编
        public String tyacpost {get;set;}
        //临时联系人Id
        public String tycoId {get;set;}
        //临时联系人名
        public String tycoName {get;set;}
        //临时联系人是否删除
        public Boolean tycoof {get;set;}
        //临时联系人的查找临时客户 同时也是临时客户
        public String tyaccount {get;set;} 
        //临时联系人传真
        public String tycofax {get;set;}
        //临时联系人地址
        public String tycoadd {get;set;}
        //临时联系人电话 
        public String tycophone {get;set;}
        //临时联系人邮政编码
        public String tycopost {get;set;}
    }
}