高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
public with sharing class SpecialDiscountController {
    public String accountId { get; set; }
    //public List<product2Info> productInfoList = new List<product2Info>();
    public List<product2Info> productInfoListview { get; set; }
    public Account acc { get; set; }
    public Dealer_Product__c dpc { get; set; }
    public Boolean Pro2DealerFlag {get ; set;}
    public Integer productRecoedsCount { get; set; }
    //错误信息
    public String alertMessage {set;get;}
    //
    public Boolean productSearch;
 
    //page
    Public Integer pagesize{get; set;} 
    Public Integer totalcount{get; set;} 
    Public Integer pagecount{get; set;} 
    Public Integer currentpage{get; set;} 
    public Boolean hasPrevious{get; set;}
    public Boolean hasNext{get; set;}
 
    Map<Id,Dealer_Product__c> ExistProdSDMap = new Map<Id,Dealer_Product__c> ();
 
    //1-5分类选项
    public String keyword { get; set; }
    public String category1 { get; set; }
    public String category2 { get; set; }
    public String category3 { get; set; }
    public String category4 { get; set; }
    public String category5 { get; set; }
    public String SpecialDiscount{ get; set; }
    public List<SelectOption> category1OptionList{get;set;}
    public List<SelectOption> category2OptionList{get;set;}
    public List<SelectOption> category3OptionList{get;set;}
    public List<SelectOption> category4OptionList{get;set;}
    public List<SelectOption> category5OptionList{get;set;}
 
    public SpecialDiscountController() {
        productSearch = false;
        accountId = ApexPages.currentPage().getParameters().get('userid');
        //page
        pagesize = Integer.valueof(system.label.orderdetLimitsize);
        currentpage = 0;
    }
 
    public void init(){
        geCategory();
        acc = [SELECT Id,Name,Parent.Name,Parent.Management_Code__c FROM Account WHERE Id = :accountId];
        ExistProdSDMap = new Map<Id,Dealer_Product__c>();
        List<product2Info> productInfoList = new List<product2Info>();
        productInfoListview = new List<product2Info>();
        dpc = new Dealer_Product__c();
 
        Pro2DealerFlag = false;
 
        //所有的产品
        List<Product2__c> product2Selected  = [select Id, Name,Name__c,Asset_Model_No__c,Category1__c,Category2__c,Category3__c,Category4__c,Category5__c,SFDA_Status__c FROM Product2__c where Estimation_Entry_Possibility__c = '○' order by Name__c ];
        for (Integer i = 0; i < product2Selected.size(); i++) {
            productInfoList.add(new product2Info(product2Selected[i]));
        }
 
        //已存在的特殊折扣
        List<Dealer_Product__c> dealerProductList = [select Id, Special_Discount__c,Dealer_Product2__c
                                    FROM Dealer_Product__c
                                    WHERE Dealer_Contact__c = :accountId
                                    AND Special_Discount__c != null];
        for(Dealer_Product__c ahlc : dealerProductList){
            ExistProdSDMap.put(ahlc.Dealer_Product2__c, ahlc);
        }
 
        for(product2Info prd : productInfoList){
            if(ExistProdSDMap.containsKey(prd.pro.Id)){
                //prd.check = true;
                prd.dealerPro = ExistProdSDMap.get(prd.pro.Id);
                prd.specialDiscount = ExistProdSDMap.get(prd.pro.Id).Special_Discount__c;
            }
        }
        productInfoList.sort();
 
        //总件数
        totalcount = productInfoList.size();
        //页数
        pagecount=(totalcount  +  pagesize  - 1) / pagesize;
        //显示第一页
        moveToFirst();
        makeCurrentPageRecords(productInfoList);
    }
 
 
    public void geCategory(){
        AggregateResult[] categoryList = [select Count(id), Category1_text__c c1c from Product2__c group by Category1_text__c];
        category1OptionList = new List<SelectOption>();
        category1OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
            String deliverycnt = String.valueOf(category3Search.get('c1c'));
            if(String.isNotBlank(deliverycnt)){
                category1OptionList.add(new SelectOption(deliverycnt,deliverycnt));
            }
        }
        category2OptionList = new List<SelectOption>();
        category2OptionList.add(new SelectOption('', '--无--'));
        category3OptionList = new List<SelectOption>();
        category3OptionList.add(new SelectOption('', '--无--'));
        category4OptionList = new List<SelectOption>();
        category4OptionList.add(new SelectOption('', '--无--'));
        category5OptionList = new List<SelectOption>();
        category5OptionList.add(new SelectOption('', '--无--'));
    }
 
    public void showcategory2() {
        AggregateResult[] categoryList = [select Count(id), Category2_text__c c2c from Product2__c WHERE Category1_text__c = :category1  group by Category2_text__c];
        category2OptionList = new List<SelectOption>();
        category2OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
            String deliverycnt = String.valueOf(category3Search.get('c2c'));
            if(String.isNotBlank(deliverycnt)){
                category2OptionList.add(new SelectOption(deliverycnt,deliverycnt));
            }
        }
 
    }
 
    public void showcategory3() {
        AggregateResult[] categoryList = [select Count(id), Category3_text__c c3c from Product2__c WHERE Category1_text__c = :category1 AND Category2_text__c = :category2  group by Category3_text__c];
        category3OptionList = new List<SelectOption>();
        category3OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
            String deliverycnt = String.valueOf(category3Search.get('c3c'));
            if(String.isNotBlank(deliverycnt)){
                category3OptionList.add(new SelectOption(deliverycnt,deliverycnt));
            }
        }
 
    }
    public void showcategory4() {
        AggregateResult[] categoryList = [select Count(id), Category4_text__c c4c from Product2__c WHERE Category1_text__c = :category1 AND Category2_text__c = :category2 AND Category3_text__c = :category3  group by Category4_text__c];
        category4OptionList = new List<SelectOption>();
        category4OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
            String deliverycnt = String.valueOf(category3Search.get('c4c'));
            if(String.isNotBlank(deliverycnt)){
                category4OptionList.add(new SelectOption(deliverycnt,deliverycnt));
            }
        }
 
    }
    public void showcategory5() {
        AggregateResult[] categoryList = [select Count(id), Category5_text__c c5c from Product2__c WHERE Category1_text__c = :category1 AND Category2_text__c = :category2 AND Category3_text__c = :category3 AND Category4_text__c = :category4  group by Category5_text__c];
        category5OptionList = new List<SelectOption>();
        category5OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
            String deliverycnt = String.valueOf(category3Search.get('c5c'));
            if(String.isNotBlank(deliverycnt)){
                category5OptionList.add(new SelectOption(deliverycnt,deliverycnt));
            }
        }
 
    }
 
    public PageReference save() {
        List<Dealer_Product__c> Ins = new List<Dealer_Product__c>();
        List<Dealer_Product__c> Del = new List<Dealer_Product__c>();
        alertMessage = '';
        Savepoint sp = Database.setSavepoint();
        try{
            for(product2Info proinfo:productInfoListview){
                if(proinfo.check == true){
                    if(proinfo.dealerPro.Special_discount__c != null  && 
                        (proinfo.dealerPro.Special_discount__c <=0 || proinfo.dealerPro.Special_discount__c >=100)){
                        proinfo.dealerPro.Special_discount__c.addError('百分比无效,请输入0~100的数字。');
                        return null;
                    }
                    Dealer_Product__c dpc = new Dealer_Product__c();
                    dpc.Id = proinfo.dealerPro.Id;
                    dpc.Dealer_Contact__c = accountId;
                    dpc.Dealer_Product2__c = proinfo.pro.Id;
 
                    if(proinfo.dealerPro.Special_Discount__c != null && proinfo.dealerPro.Special_Discount__c > 0){
                        dpc.Special_Discount__c = proinfo.dealerPro.Special_Discount__c;
                        Ins.add(dpc);
                    }
                    else{
                        if(proinfo.dealerPro.Id != null){
                            Del.add(dpc);
                        }
 
 
 
                        /*List<Dealer_Product__c> results = [select Id, Special_Discount__c,Dealer_Product2__c
                                    FROM Dealer_Product__c
                                    WHERE id = :proinfo.dealerPro.Id];
                        if (results !=null && results.size()>0) Del.add(dpc);*/
                    }
                }
                else{
                    if(proinfo.dealerPro.Special_Discount__c == null || proinfo.dealerPro.Special_Discount__c==0){
                        if(proinfo.dealerPro.id != null){
                            Dealer_Product__c dpc = new Dealer_Product__c();
                            dpc.Id = proinfo.dealerPro.Id;
                            dpc.Dealer_Contact__c = accountId;
                            dpc.Dealer_Product2__c = proinfo.pro.Id;
                            Del.add(dpc);
                        }
 
 
                        //101 sql 对应注释
                        /*List<Dealer_Product__c> results = [select Id, Special_Discount__c,Dealer_Product2__c
                                    FROM Dealer_Product__c
                                    WHERE id = :proinfo.dealerPro.Id];
                        if (results !=null && results.size()>0){
                             Dealer_Product__c dpc = new Dealer_Product__c();
                             dpc.Id = proinfo.dealerPro.Id;
                             dpc.Dealer_Contact__c = accountId;
                            dpc.Dealer_Product2__c = proinfo.pro.Id;
                            Del.add(dpc);
                        }*/
                    }
                }
            }
            /*if(Del.size()>0){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, 'Del' + Del));
                return null;
                delete Del;
            }*/
 
            if(Ins.size()>0){
                upsert Ins;
            }
 
            if(Del.size()>0){
                delete Del;
            }
            //alertMessage = '保存成功。';
        }catch(Exception e){
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return  null;
        }
        return setpage();
        //return null;
 
    }
 
    public PageReference setpage(){
        PageReference ref = new Pagereference('/' + acc.Id);
        ref.setRedirect(true);
        return ref;
    }
 
    // 检索
    public void searchProduct() {
        productSearch = true;
        //所有的产品
        List<product2Info> productInfoList = new List<product2Info>();
        //检索产品
        String soql = this.makeSoql(category1,category2,category3,category4,category5,keyword,Pro2DealerFlag);
        List<Product2__c> product2Selected = Database.query(soql);
        for (Integer i = 0; i < product2Selected.size(); i++) {
            productInfoList.add(new product2Info(product2Selected[i]));
        }
 
        //已存在的特殊折扣
        ExistProdSDMap.clear();
        List<Dealer_Product__c> DealerProductList = [select Id, Special_Discount__c,Dealer_Product2__c
                                    FROM Dealer_Product__c
                                    WHERE Dealer_Contact__c = :accountId
                                    AND Special_Discount__c != null];
        for(Dealer_Product__c ahlc : dealerProductList){
            ExistProdSDMap.put(ahlc.Dealer_Product2__c, ahlc);
        }
 
        for(product2Info prd : productInfoList){
            if(ExistProdSDMap.containsKey(prd.pro.Id)){
                //prd.check = true;
                prd.dealerPro = ExistProdSDMap.get(prd.pro.Id);
                prd.specialDiscount = ExistProdSDMap.get(prd.pro.Id).Special_Discount__c;
            }
        }
        productInfoList.sort();
 
        //总件数
        totalcount = productInfoList.size();
        //页数
        pagecount=(totalcount  +  pagesize  - 1) / pagesize; 
        //显示第一页
        moveToFirst();
        makeCurrentPageRecords(productInfoList);
    }
 
    //产品检索
    public String makeSoql(String category1,String category2,String category3,String category4,String category5,String keyword,Boolean Pro2DealerFlag){
        String soql = 'select Id, Name,Name__c,Asset_Model_No__c,Category1__c,Category2__c,Category3__c,Category4__c,';
        soql += ' Category5__c,SFDA_Status__c FROM Product2__c WHERE Estimation_Entry_Possibility__c = \'○\'';
        if(String.isNotBlank(category1)){
            soql += ' AND Category1__c = \'' +  category1 + '\'';
        }
        if(String.isNotBlank(category2)){
            soql += ' AND Category2__c = \'' +  category2 + '\'';
        }
        if(String.isNotBlank(category3)){
            soql += ' AND Category3__c = \'' +  category3 + '\'';
        }
        if(String.isNotBlank(category4)){
            soql += ' AND Category4__c = \'' +  category4 + '\'';
        }
        if(String.isNotBlank(category5)){
            soql += ' AND Category5__c = \'' +  category5 + '\'';
        }
        if(String.isNotBlank(keyword)){
            soql += ' AND Name__c like \'%' + String.escapeSingleQuotes(keyword.replaceAll('%', '\\%')) + '%\'';
        }
        if(Pro2DealerFlag){
            soql += ' AND Pro2_Dealer_Object__c = true';
        }
        soql += ' order by Name__c ';
 
        return soql;
    }
 
    //首页
    public void moveToFirst(){
        if(currentpage == 1) return;
        currentpage = 1;
        canMove();
    }
 
    //尾页
    public void moveToLast(){
        if(currentpage >= pagecount) return;
        currentpage = pagecount;
        canMove();
    }
 
    //上一页
    public void moveToPrevious(){
        if(currentpage == 1) return;
        currentpage = currentpage -1;
        canMove();
    }
 
    //下一页
    public void moveToNext(){
        if(currentpage >= pagecount) return;
 
        currentpage = currentpage + 1;
        canMove();
    }
 
    //判断是否可翻页
    public void canMove(){
        hasPrevious = false;
        hasNext = false;
        if(pagecount > 1 && currentpage > 1) hasPrevious = true;
        if(pagecount > 1 && currentpage < pagecount)  hasNext = true;
    }
 
    //获取所有产品
    public List<product2Info> searchAllProduct(){
        //所有的产品
        List<product2Info> allProductList = new List<product2Info>();
        List<Product2__c> accountInfo = [select Id, Name,
                                                Name__c,
                                                Asset_Model_No__c,
                                                Category1__c,
                                                Category2__c,
                                                Category3__c,
                                                Category4__c,
                                                Category5__c,
                                                SFDA_Status__c
                                            FROM Product2__c
                                            where Estimation_Entry_Possibility__c = '○'
                                            order by Name__c];
        for (Integer i = 0; i < accountInfo.size(); i++) {
            allProductList.add(new product2Info(accountInfo[i]));
        }
        return allProductList;
    }
 
    //刷新当前页
    public PageReference refreshPageSize() {
        //所有的产品
        List<product2Info> productInfoList = new List<product2Info>();
        //if(productSearch){
            //检索产品
            String soql = this.makeSoql(category1,category2,category3,category4,category5,keyword,Pro2DealerFlag);
            List<Product2__c> product2Selected = Database.query(soql);
            for (Integer i = 0; i < product2Selected.size(); i++) {
                productInfoList.add(new product2Info(product2Selected[i]));
            }
 
            //已存在的特殊折扣
            ExistProdSDMap.clear();
            List<Dealer_Product__c> DealerProductList = [select Id, Special_Discount__c,Dealer_Product2__c
                                        FROM Dealer_Product__c
                                        WHERE Dealer_Contact__c = :accountId
                                        AND Special_Discount__c != null];
            for(Dealer_Product__c ahlc : dealerProductList){
                ExistProdSDMap.put(ahlc.Dealer_Product2__c, ahlc);
            }
 
            for(product2Info prd : productInfoList){
                if(ExistProdSDMap.containsKey(prd.pro.Id)){
                    prd.dealerPro = ExistProdSDMap.get(prd.pro.Id);
                    prd.specialDiscount = ExistProdSDMap.get(prd.pro.Id).Special_Discount__c;
                }
            }
        /*}else{
            productInfoList = searchAllProduct();
            //已存在的产品促销信息
                if(nowContactMap.size() > 0){
                    List<Dealer_Product__c> dealerProductList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                                                Special_Campaign_Price__c,Campaign_StartDate__c,
                                                                Campaign_EndDate__c,OrderGoods_Limit__c
                                                            FROM Dealer_Product__c
                                                            WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                                            AND Promotion_No__c = :recordId];
                    for(Dealer_Product__c ahlc : dealerProductList){
                        existRecordsMap.put('' + ahlc.Dealer_Contact__c + ahlc.Dealer_Product2__c,ahlc);
                        agencyedHospitalMap.put(ahlc.Dealer_Product2__c, ahlc);
                    }
                    for(product2Info prd : productInfoList){
                        if(agencyedHospitalMap.containsKey(prd.pro.Id)){
                            //prd.check = true;
                            prd.dealerPro = agencyedHospitalMap.get(prd.pro.Id);
                        }
                    }
                }
        }*/
        productInfoList.sort();
        //总件数
        totalcount = productInfoList.size();
        //页数
        pagecount=(totalcount  +  pagesize  - 1) / pagesize;
        makeCurrentPageRecords(productInfoList);
        return null;
    }
 
    //编辑当前页内容
    public void makeCurrentPageRecords(List<product2Info> productInfoList){
        Integer startIdx;
        Integer endIdx;
        productInfoListview = new List<product2Info>();
/*
        List<product2Info> reSet = new List<product2Info>();
        Map<String, String> selectedIdMap = new Map<String, String>();
        for(product2Info prd : productInfoListview){
            if(prd.check == true){
                selectedIdMap.put(prd.pro.Id,prd.pro.Id);
                reSet.add(prd);
            }
        }
 
        if(reSet.size() > 0){
            productInfoListview = reSet;
        }
*/
        startIdx = (currentpage-1) * pagesize;
        endIdx = (currentpage-1) * pagesize+ pagesize;
        if (endIdx > productInfoList.size()){
            endIdx = productInfoList.size();
        } 
 
        for(Integer i=startIdx; i < endIdx ; i++){
            //if(selectedIdMap.containsKey(productInfoList.get(i).pro.Id)){
            //    continue;
            //}
            //else{
                productInfoListview.add(productInfoList.get(i));
            //}
        }
 
        productRecoedsCount = productInfoListview.size();
 
   }
 
 
    class product2Info implements Comparable {
        public Boolean check { get; set; }
        public Decimal specialDiscount { get; set; }
        public Product2__c pro { get; set; }
        public Dealer_Product__c dealerPro { get; set; }
 
        public product2Info(Product2__c e) {
            check = false;
            pro = e;
            dealerPro = new Dealer_Product__c();
            specialDiscount = null;
        }
 
        public Integer compareTo(Object compareTo) {
            product2Info compareToesd =(product2Info)compareTo;
            Integer returnValue = 0;
            if(check == true){
                returnValue = -1;
            }else{
                if (specialDiscount == null){
                    returnValue = 1;
                }else{
                    if (specialDiscount > compareToesd.specialDiscount) {
                        returnValue = -1;
                    } else if (specialDiscount < compareToesd.specialDiscount) {
                        returnValue = 1;
                    }
                }
            }
            return returnValue;
        }
    }
}