buli
2023-06-05 e9e1806b0f4b5bae1fc36204ccc5d6fdad52d66f
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
public with sharing class LexDealerInquiryModifyStateController {
    public static List<OpportunityInfo> oppRecords { get; set; }
    // ページレイアウトの情報を取得
    private static Map<String, Map<String, String>> editLayoutItemRWMap = New Map<String, Map<String, String>>();
    // private Map<String, Map<String, String>> editLayoutItemRWMapRt = null;
    public static List<OpportunityInfo> OPPORTList { get; set; }
    /*****************検索用******************/
 
    /*******************检索属性值*******************/
 
    public static String accSearch { get; set; }//经销商医院
    public static String aooSearch { get; set; }//担当人
    public static String numtext1 { get; set; } //数据字段01
    public static String numtext { get; set; }//数据字段03
    public static String numtext2 { get; set; } //数据字段02
    public static String timetext1 { get; set; } //日期01
    public static String timetext2 { get; set; } //日期02
    public static Agency_Opportunity__c tmpAO { get; set; } //检索日期用
    public static Agency_Opportunity__c tmpBO { get; set; } //检索日期用
    public static String limits { get; set; }//日期03
 
    public static List<SelectOption> dateOpts { get; private set; }
    public static List<CusOption> textCusOpts;
    public static List<SelectOption> textOpts { get; private set; }
 
    public static List<CusOption> timeCusOpts;
    public static List<SelectOption> timeOpts { get; private set; }
 
    public static List<CusOption>  equalCusOpts;
    public static List<SelectOption> equalOpts { get; private set; }
    public static List<SelectOption> timeequalOpts { get; private set; }
    public static List<CusOption> limitCusOpts;
    public static List<SelectOption> limitOpts { get; private set; }
 
    /*****************画面表示Bean******************/
    public static Integer oppCount { get; set; }
    public static String saveType { get; set; }
 
    public static String sortKey { get; set; }
    public static String preSortKey { get; set; }
    public static Boolean sortOrderAsc { get; set; }
    public static String[] sortOrder { get; set; }
    public static String[] columus = new String[] {'Id'};
    public static String[] selColumus = null;
    public static Set<String> columusSet = new Set<String> {'Id'};
 
    // 项目set 字段标签
    public static List<String> titleLeft { get; private set; }
    public static List<String> titleRight { get; private set; }
    // 项目set 字段名
    public static List<List<String>> columnsLeftApi { get; private set; }         // 参照項目用
    public static List<List<String>> columnsRightApi { get; private set; }        // 参照項目用
    public static List<String> columnLeftCss { get; private set; }               // css 用
    public static List<String> columnRightCss { get; private set; }              // css 用
    public static Map<String, String> columnLeftRW { get; private set; }              // r,w,wm用
    public static Map<String, String> columnRightRW { get; private set; }             // r,w,wm用
    private static String strColumus;
    private static String strRtColumus;
 
    @TestVisible private static String accTypeForSort = null;
    private static Integer oppLimit = 500;
 
     // page
     public static Integer pagesize { get; set; }
     public static Integer pageToken { get; set; }
     public static String sortField { get; set; }
    //  public static String sortOrder { get; set; }
   
     public static Integer totalcount { get; set; }
 
    public LexDealerInquiryModifyStateController() {
    }
 
    @AuraEnabled
    public static Results init(Integer pageSizeLWC,Integer pageTokenLWC) {
        pageSize = pageSizeLWC;
        pageToken = pageTokenLWC;
        Results results = new Results();
        oppRecords = new List<OpportunityInfo>();
 
        //dateOpts = new List<SelectOption>();
        //dateOpts.add(new SelectOption('', '--无--'));
        //dateOpts.add(new SelectOption('Registration_Day__c', '登录日'));
        //dateOpts.add(new SelectOption('Ban_On_Use_Date__c', '禁用日期'));
 
        //数据字段下拉列表
        // textOpts = new List<SelectOption>();
        // textOpts.add(new SelectOption('', '--无--'));
        // textOpts.add(new SelectOption('Agency_Opportunity_No__c', '经销商询价编码'));
        // textOpts.add(new SelectOption('Agency__r.Name', '经销商'));
        // textOpts.add(new SelectOption('Agency_Hospital__r.Name', '经销商医院'));
        // textOpts.add(new SelectOption('Department_Cateogy__c', '科室分类'));
        // textOpts.add(new SelectOption('Change_To_Opportunity_T__c', '询价名称'));
        // // textOpts.add(new SelectOption('Name', '询价名称'));
        // textOpts.add(new SelectOption('StageName__c', '询价阶段'));
        // textOpts.add(new SelectOption('Oly_Inquiry_Status1__c', '状态1'));
        // //精琢科技   zxk    2021-08-25   start
        // // textOpts.add(new SelectOption('Oly_Inquiry_Status2__c', '状态2'));
        // textOpts.add(new SelectOption('Oly_Inquiry_Stage__c', 'Oly询价阶段'));
        // //精琢科技   zxk    2021-08-25   end
        // textOpts.add(new SelectOption('Hospital_City_Master__c', '市'));
 
        //custom option
        textCusOpts = new List<CusOption>();
        textCusOpts.add(new CusOption('', '--无--'));
        textCusOpts.add(new CusOption('Agency_Opportunity_No__c', '经销商询价编码'));
        textCusOpts.add(new CusOption('Agency__r.Name', '经销商'));
        textCusOpts.add(new CusOption('Agency_Hospital__r.Name', '经销商医院'));
        textCusOpts.add(new CusOption('Department_Cateogy__c', '科室分类'));
        textCusOpts.add(new CusOption('Change_To_Opportunity_T__c', '询价名称'));
        textCusOpts.add(new CusOption('StageName__c', '询价阶段'));
        textCusOpts.add(new CusOption('Oly_Inquiry_Status1__c', '状态1'));
        textCusOpts.add(new CusOption('Oly_Inquiry_Stage__c', 'Oly询价阶段'));
        textCusOpts.add(new CusOption('Hospital_City_Master__c', '市'));
 
       
        // textOpts.add(new SelectOption('Agency_Hospital__r.Hospital__r.Salesdepartment_HP__c', '销售本部'));
        // textOpts.add(new SelectOption('Agency_Hospital__r.Hospital__r.State_Master__r.Name', '省'));
        // textOpts.add(new SelectOption('Agency_Hospital__r.Hospital__r.City_Master__r.Name', '市'));
        //日期字段下拉列表
        // timeOpts = new List<SelectOption>();
        // timeOpts.add(new SelectOption('', '--无--'));
        // timeOpts.add(new SelectOption('Created_Day__c', '创建日'));
        // timeOpts.add(new SelectOption('Bid_Planned_Date__c', '预测招标日'));
        // timeOpts.add(new SelectOption('Forecasted_Bid_Date__c', '预测中标日'));
        // timeOpts.add(new SelectOption('Close_Forecasted_Date__c', '预测与OLY签约日'));
        // timeOpts.add(new SelectOption('Lost_Opportunity_Date__c', '失单日期'));
        // timeOpts.add(new SelectOption('Deleted_Reason_Date__c', '取消日期'));
 
        timeCusOpts = new List<CusOption>();
        timeCusOpts.add(new CusOption('', '--无--'));
        timeCusOpts.add(new CusOption('Created_Day__c', '创建日'));
        timeCusOpts.add(new CusOption('Bid_Planned_Date__c', '预测招标日'));
        timeCusOpts.add(new CusOption('Forecasted_Bid_Date__c', '预测中标日'));
        timeCusOpts.add(new CusOption('Close_Forecasted_Date__c', '预测与OLY签约日'));
        timeCusOpts.add(new CusOption('Lost_Opportunity_Date__c', '失单日期'));
        timeCusOpts.add(new CusOption('Deleted_Reason_Date__c', '取消日期'));
 
 
 
        //数据字段中间连接符号
        // equalOpts = new List<SelectOption>();
        // equalOpts.add(new SelectOption('=', '等于'));
        // equalOpts.add(new SelectOption('<>', '不等于'));
        // equalOpts.add(new SelectOption('<', '<'));
        // equalOpts.add(new SelectOption('>', '>'));
        // equalOpts.add(new SelectOption('<=', '<='));
        // equalOpts.add(new SelectOption('>=', '>='));
        // equalOpts.add(new SelectOption('contains', '包含'));
        // equalOpts.add(new SelectOption('notcontains', '不包含'));
        // equalOpts.add(new SelectOption('starts with', '起始字符'));
 
        equalCusOpts = new List<CusOption>();
        equalCusOpts.add(new CusOption('=', '等于'));
        equalCusOpts.add(new CusOption('<>', '不等于'));
        equalCusOpts.add(new CusOption('<', '<'));
        equalCusOpts.add(new CusOption('>', '>'));
        equalCusOpts.add(new CusOption('<=', '<='));
        equalCusOpts.add(new CusOption('>=', '>='));
        equalCusOpts.add(new CusOption('contains', '包含'));
        equalCusOpts.add(new CusOption('notcontains', '不包含'));
        equalCusOpts.add(new CusOption('starts with', '起始字符'));
 
        //日期字段连接符号
        //timeequalOpts = new List<SelectOption>();
        //timeequalOpts.add(new SelectOption('=', '等于'));
        //timeequalOpts.add(new SelectOption('<>', '不等于'));
        //timeequalOpts.add(new SelectOption('<', '<'));
        //timeequalOpts.add(new SelectOption('>', '>'));
        //timeequalOpts.add(new SelectOption('<=', '<='));
        //timeequalOpts.add(new SelectOption('>=', '>='));
        //数据显示条数
        // limitOpts = new List<SelectOption>();
        // limitOpts.add(new SelectOption('10', '10'));
        // limitOpts.add(new SelectOption('20', '20'));
        // limitOpts.add(new SelectOption('50', '50'));
        // limitOpts.add(new SelectOption('100', '100'));
        // limitOpts.add(new SelectOption('200', '200'));
        // limitOpts.add(new SelectOption('1000', '全部'));
        //数据显示默认条数
        limits = '20';
 
        limitCusOpts = new List<CusOption>();
        limitCusOpts.add(new CusOption('10', '10'));
        limitCusOpts.add(new CusOption('20', '20'));
        limitCusOpts.add(new CusOption('50', '50'));
        limitCusOpts.add(new CusOption('100', '100'));
        limitCusOpts.add(new CusOption('200', '200'));
        limitCusOpts.add(new CusOption('1000', '全部'));
        try{
            PartnerSoapSforceCom.Soap soap = new PartnerSoapSforceCom.Soap();
            soap.SessionHeader = new PartnerSoapSforceCom.SessionHeader_element();
            soap.SessionHeader.sessionId = UserInfo.getSessionId();
            soap.endpoint_x = URL.getSalesforceBaseUrl().toExternalForm() + '/partner/services/Soap/u/33.0';
            // soap.endpoint_x = URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/33.0';
    
            PartnerSoapSforceCom.DescribeLayoutResult dlr = new PartnerSoapSforceCom.DescribeLayoutResult();
            if (System.Test.isRunningTest()) {
                // UnitTest 用
                // PartnerSoapSforceCom.DescribeLayoutResult dlr = new PartnerSoapSforceCom.DescribeLayoutResult();
                PartnerSoapSforceCom.RecordTypeMapping rtMap = new PartnerSoapSforceCom.RecordTypeMapping();
                PartnerSoapSforceCom.DescribeLayout editLayout = new PartnerSoapSforceCom.DescribeLayout();
                PartnerSoapSforceCom.DescribeLayoutSection editLayoutSection = new PartnerSoapSforceCom.DescribeLayoutSection();
                PartnerSoapSforceCom.DescribeLayoutRow layoutRow = new PartnerSoapSforceCom.DescribeLayoutRow();
                PartnerSoapSforceCom.DescribeLayoutItem layoutItem = new PartnerSoapSforceCom.DescribeLayoutItem();
                PartnerSoapSforceCom.DescribeLayoutComponent layoutComponent = new PartnerSoapSforceCom.DescribeLayoutComponent();
                dlr.recordTypeMappings = new List<PartnerSoapSforceCom.RecordTypeMapping>();
                dlr.recordTypeMappings.add(rtMap);
                rtMap.recordTypeId = 'recordTypeId';
                dlr.layouts = new List<PartnerSoapSforceCom.DescribeLayout>();
                dlr.layouts.add(editLayout);
                editLayout.editLayoutSections = new List<PartnerSoapSforceCom.DescribeLayoutSection>();
                editLayout.editLayoutSections.add(editLayoutSection);
                editLayoutSection.layoutRows = new List<PartnerSoapSforceCom.DescribeLayoutRow>();
                editLayoutSection.layoutRows.add(layoutRow);
                layoutRow.layoutItems = new List<PartnerSoapSforceCom.DescribeLayoutItem>();
                layoutRow.layoutItems.add(layoutItem);
                layoutItem.layoutComponents = new List<PartnerSoapSforceCom.DescribeLayoutComponent>();
                layoutItem.layoutComponents.add(layoutComponent);
                // return dlr;
            } else {
                dlr  = soap.describeLayout('Agency_Opportunity__c', null, null);
                // dlr  = soap.describeLayout('asdas', null, null);
            }
    
            // PartnerSoapSforceCom.DescribeLayoutResult dlr  = soap.describeLayout('Agency_Opportunity__c', null, null);
            Map<String, PartnerSoapSforceCom.DescribeLayout> layputMap = new Map<String, PartnerSoapSforceCom.DescribeLayout>();
            for (PartnerSoapSforceCom.DescribeLayout layout : dlr.layouts) {
                layputMap.put(layout.id, layout);
            }
            for (Integer lidx = 0; lidx < dlr.recordTypeMappings.size(); lidx++) {
                String recordTypeId = dlr.recordTypeMappings[lidx].recordTypeId;
                Map<String, String> rtnInner = new Map<String, String>();
                editLayoutItemRWMap.put(recordTypeId, rtnInner);
                System.debug('recordTypeId=' + recordTypeId);
                PartnerSoapSforceCom.DescribeLayout layout = layputMap.get(dlr.recordTypeMappings[lidx].layoutId);
                for (PartnerSoapSforceCom.DescribeLayoutSection section : layout.editLayoutSections) {
                    for (PartnerSoapSforceCom.DescribeLayoutRow row : section.layoutRows) {
                        for (PartnerSoapSforceCom.DescribeLayoutItem item : row.layoutItems) {
                            if (item.layoutComponents != null && item.layoutComponents.size() > 0
                                    && String.isBlank(item.layoutComponents[0].value) == false) {
                                rtnInner.put(item.layoutComponents[0].value, 'r');
                                if (item.editableForUpdate) {
                                    rtnInner.put(item.layoutComponents[0].value, 'w');
                                }
                                if (item.required) {
                                    rtnInner.put(item.layoutComponents[0].value, 'wm');
                                }
                            }
                        }
                    }
                }
            }
            // editLayoutItemRWMap = SoapApi.getEditRWByRecordType('Agency_Opportunity__c', null);
            tmpAO = new Agency_Opportunity__c();
            tmpBO = new Agency_Opportunity__c();
            setLayoutRWInfo();
            searchOppInner(strColumus,strRtColumus,accSearch,aooSearch,numtext,numtext1,numtext2,sortKey);
            System.debug('limitCusOpts==>'+limitCusOpts);
            System.debug('searchOppInner==>'+ searchOppInner(strColumus,strRtColumus,accSearch,aooSearch,numtext,numtext1,numtext2,sortKey));
            LexDealerInquiryModifyStateController.Results returnList = searchOppInner(strColumus,strRtColumus,accSearch,aooSearch,numtext,numtext1,numtext2,sortKey);
            System.debug('returnList==>'+ returnList);
                 //分页
                 PaginatedAccounts paginatedAccounts = new PaginatedAccounts();
                 totalCount = oppRecords.size();
                 paginatedAccounts.nextPageToken = (pageToken + pageSize < totalCount) ? pageToken + pageSize : null;
                 paginatedAccounts.recordStart = pageToken + 1;
                 paginatedAccounts.pageNumber = pageToken / pageSize + 1;
                 Integer recordEnd = pageSize * paginatedAccounts.pageNumber;
                 paginatedAccounts.recordEnd = totalCount >= recordEnd ? recordEnd : totalCount;
                 paginatedAccounts.totalRecords = totalCount;
     
                 Integer startIdx;
                 Integer endIdx;
                 List<OpportunityInfo> pageoppRecords = new List<OpportunityInfo>();
                 startIdx = pageToken;
                 endIdx = startIdx + pageSize;
                 if (endIdx > oppRecords.size()) {
                     endIdx = oppRecords.size();
                 }
                 for (Integer i = startIdx; i < endIdx; i++) {
                    pageoppRecords.add(oppRecords.get(i));
                 }
                 //end
                  
            //回传参数
            results.result = 'Success';
            results.oppRecords = oppRecords;
            results.timeCusOpts = timeCusOpts;
            results.equalCusOpts = equalCusOpts;
            results.limitCusOpts = limitCusOpts;
            results.textCusOpts = textCusOpts;
            results.limits = limits;
            results.strColumus = strColumus;
            results.strRtColumus = strRtColumus;
            results.paginatedAccounts = paginatedAccounts;
            results.pageoppRecords = pageoppRecords;
            results.Msg = returnList.Msg;
 
 
            System.debug('results==>'+results);
 
        }catch (Exception e) {
            results.result = 'Fail';
            results.Msg = e.getLineNumber()+'---'+e.getMessage();
        }
      
        // return null;
        return results;
    }
    //用于获取经销商询价字段集和询价字段集以及相应的读写权限
    @TestVisible private static void setLayoutRWInfo() {
        // if (this.sortOrder == null) {
            if (sortOrder == null) {
            selColumus = new String[] {};
            strColumus = '';
            ID accRecordTypeId = accTypeForSort;
 
            Map<String, String> DESC_RW = editLayoutItemRWMap.get('012100000006KW7AAM');
            Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get('Agency_Opportunity__c').getDescribe().fieldSets.getMap();
            Schema.FieldSet fs = fsMap.get('DealerInquiryModifyState');
            List<FieldSetMember> fsmList = fs.getFields();
            titleLeft = new List<String>();
            List<String> columnLeft = new List<String>();
            columnLeftCss = new List<String>();
            columnsLeftApi = new List<List<String>>();
            columnLeftRW = new Map<String, String>();
            for (FieldSetMember fsm : fsmList) {
                titleLeft.add(fsm.getLabel());
                columnLeft.add(fsm.getFieldPath());
                List<String> splitFieldPath = fsm.getFieldPath().split('\\.');
                columnsLeftApi.add(splitFieldPath);
                if (DESC_RW == null) {
                    columnLeftRW.put(fsm.getFieldPath(), 'r');
                } else if (splitFieldPath.size() == 1) {
                    String rw = DESC_RW.get(fsm.getFieldPath());
                    if (rw != null) {
                        columnLeftRW.put(fsm.getFieldPath(), rw);
                    } else {
                        columnLeftRW.put(fsm.getFieldPath(), 'r');
                    }
                } else {
                    columnLeftRW.put(fsm.getFieldPath(), 'r');
                }
            }
            for (String str : columnLeftRW.keySet()) {
                System.debug(str + '=+=+=+=+=' + columnLeftRW.get(str));
            }
            for (String s : columnLeft) {
                if (columusSet.contains(s) == false) {
                    columus.add(s);
                    columusSet.add(s);
                }
                if (selColumus.contains(s) == false) {
                    selColumus.add(s);
                }
                columnLeftCss.add(s.replace('.', '_'));
            }
            strRtColumus = '';
            Schema.FieldSet fsRt = fsMap.get('DealerOpportunity');
            List<FieldSetMember> fsmListRt = fsRt.getFields();
            titleRight = new List<String>();
            List<String> columnRight = new List<String>();
            columnRightCss = new List<String>();
            columnsRightApi = new List<List<String>>();
            columnRightRW = new Map<String, String>();
 
            for (FieldSetMember fsm : fsmListRt) {
                titleRight.add(fsm.getLabel());
                columnRight.add(fsm.getFieldPath());
                List<String> splitFieldPath = fsm.getFieldPath().split('\\.');
                columnsRightApi.add(splitFieldPath);
                if (DESC_RW == null) {
                    columnRightRW.put(fsm.getFieldPath(), 'r');
                } else if (splitFieldPath.size() == 1) {
                    String rw = DESC_RW.get(fsm.getFieldPath());
                    if (rw != null) {
                        columnRightRW.put(fsm.getFieldPath(), rw);
                    } else {
                        columnRightRW.put(fsm.getFieldPath(), 'r');
                    }
                } else {
                    columnRightRW.put(fsm.getFieldPath(), 'r');
                }
            }
            for (String s : columnRight) {
                selColumus.add(s);
                if (strRtColumus == '') {
                    strRtColumus = s;
                } else {
                    strRtColumus = strRtColumus + ',' + s;
                }
                columnRightCss.add(s.replace('.', '_'));
 
            }
            strColumus = String.join(columus, ',');
            System.debug('======-======-======strColumus' + strColumus);
            System.debug('======-======-======strRtColumus' + strRtColumus);
            sortOrderAsc = true;
            sortOrder = new String[selColumus.size()];
            for (Integer i = 0; i < selColumus.size(); i++) sortOrder[i] = ' ';
        }
    }
    //用于拼接SOQL语句 根据不同检索条件拼接不同SOQL语句。
    @AuraEnabled
    public static Results searchOppInner(String strColumus,String strRtColumus,String accSearch,String aooSearch,String numtext,String numtext1,String numtext2,String sortKeyLWC) {
        Results results = new Results();
        sortKey = sortKeyLWC;
 
        //SOQL:拼接检索条件以及经销商询价的SOQL语句
        // List<User> userlist2 = [select id,name from user where Contactid in (select id from Contact)];
        // String querySoql = '';
        // Integer queryint = 0;
        // for(User user : userlist2){
        //     if(queryint == 0){
        //         querySoql += '\''+user.Id+'\'';
        //         queryint++;
        //     }else{
        //         querySoql += ',\''+user.Id+'\'';
        //     }
 
        // }
        // System.debug('pppqqq222'+userlist2);
        // System.debug('pppqqq333'+querySoql);
        // AND Ownerid in ( '+querySoql+')'
 
        String soql = 'SELECT Change_To_Opportunity__r.Id , Agency_Hospital__r.Name , Product_Category1__r.Name , Product_Category2__r.Name , Product_Category3__r.Name , Agency_Person__r.Name , Agency__r.Name , Change_To_Opportunity__r.Name ,' +
                      strColumus + ',' + strRtColumus +
                      ' FROM Agency_Opportunity__c WHERE recordType.DeveloperName = \'Opportunity\' ';
        //数据字段:经销商医院  +SOQL
        if (!String.isBlank(accSearch)) {
            accSearch = accSearch.trim();
            // soql += 'AND Agency__r.Name  '   + '  LIKE  \'%' + accSearch + '%\' ';
            soql += 'AND Agency_Hospital__r.Name  '   + '  LIKE  \'%' + accSearch + '%\' ';
        }
        //数据字段:担当人  +SOQL
        if (!String.isBlank(aooSearch)) {
            String newAooSearch = aooSearch.trim();
            // soql += 'AND Change_To_Opportunity__r.Owner.Alias = ' + '\'' + newAooSearch + '\'';//Name
            soql += 'AND Agency_PersonName__c ' + '  LIKE  \'%' + newAooSearch + '%\' ';
        }
        //数据字段:03 手动填写项 准备调用makeTextSql;
        if (!String.isBlank(numtext)) {
            String newNumtext = numtext.trim();
            String str = makeTextSql(numtext1,  numtext2,  newNumtext);
            soql += str;
            System.debug('+++++++++++++=============++++++++++++++++++' + soql);
        }
        //点击排序
        // System.debug('==-=-=-=-=-=this.sortKey'+Integer.valueOf(this.sortKey)+'===titleLeft.size'+titleLeft.size()+7);
        //start注释 by WangXueqin
        // if (String.isBlank(this.sortKey)) {
        //     soql += ' order by LastModifiedDate desc limit ' + Integer.valueOf(limits);
        // } else {
        //     if (Integer.valueOf(this.sortKey) <= titleLeft.size() + 7) {
        //         soql += ' order by ' + this.selColumus[Integer.valueOf(this.sortKey)] + ' ' + (this.sortOrderAsc == true ? 'asc nulls first' : 'desc nulls last') + ' limit ' + Integer.valueOf(limits);
        //     } else {
        //         soql += ' order by Change_To_Opportunity__r.' + this.selColumus[Integer.valueOf(this.sortKey)] + ' ' + (this.sortOrderAsc == true ? 'asc nulls first' : 'desc nulls last') + ' limit ' + Integer.valueOf(limits);
        //     }
        // }
        // system.debug(soql);
        //end
 
 
 
        // List<User> userlist1 = [select id,name from user ];
        // System.debug('pppqqq111'+userlist1);
 
        List<Agency_Opportunity__c> InfoList = Database.query(soql);
        System.debug('+++++++-------------------'+InfoList);
 
        // List<Agency_Opportunity__c> InfoList = ControllerUtil.DatabaseQuery(soql);
        // System.debug('InfoList[0].Change_To_Opportunity_T__c'+InfoList[0].Change_To_Opportunity_T__c);
        for (Agency_Opportunity__c apt : InfoList) {
            System.debug('pppqqq' + apt.Agency_PersonName__c + '====' + apt.Name);
        }
 
        List<OpportunityInfo> oppinfoList = new List<OpportunityInfo>();
        if (InfoList != null && InfoList.size() > 0) {
            String str = '';
            for (Agency_Opportunity__c agc : InfoList) {
                if (str == '' && agc.Change_To_Opportunity__r.Id != null) {
                    str = '\'' + agc.Change_To_Opportunity__r.Id + '\'';
                } else if (agc.Change_To_Opportunity__r.Id != null) {
                    str += ',\'' + agc.Change_To_Opportunity__r.Id + '\'';
 
                }
            }
            for (Agency_Opportunity__c info : InfoList) {
                oppinfoList.add(new OpportunityInfo(info, info));
                oppinfoList[oppinfoList.size() - 1].lineNo = oppinfoList.size() - 1;
            }
        }
        oppRecords = oppinfoList.clone();
        oppCount = oppRecords.size();
        //显示提示操作信息
        if (String.isBlank(saveType) && String.isBlank(sortKey)) {
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '取得最近的 ' + oppCount + ' 条数据'));
            results.result = 'Success';
            results.Msg = '取得最近的 ' + oppCount + ' 条数据';
            return results;
        } else if (!String.isBlank(sortKey)) {
            if (oppCount > oppLimit) {
                // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '数据超过' + oppLimit + '件,只显示前' + oppLimit + '件'));
                results.result = 'Success';
                results.Msg = '数据超过' + oppLimit + '件,只显示前' + oppLimit + '件';
               
            } else {
                results.result = 'Success';
                results.Msg = '共有 ' + oppCount + ' 条数据';
               
                // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有 ' + oppCount + ' 条数据'));
            }
        } else {
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有 ' + oppCount + ' 条数据'));
            results.result = 'Success';
            results.Msg = '共有 ' + oppCount + ' 条数据';
        }
        //   //分页
        //   PaginatedAccounts paginatedAccounts = new PaginatedAccounts();
        //   totalCount = oppRecords.size();
        //   paginatedAccounts.nextPageToken = (pageToken + pageSize < totalCount) ? pageToken + pageSize : null;
        //   paginatedAccounts.recordStart = pageToken + 1;
        //   paginatedAccounts.pageNumber = pageToken / pageSize + 1;
        //   Integer recordEnd = pageSize * paginatedAccounts.pageNumber;
        //   paginatedAccounts.recordEnd = totalCount >= recordEnd ? recordEnd : totalCount;
        //   paginatedAccounts.totalRecords = totalCount;
 
        //   Integer startIdx;
        //   Integer endIdx;
        //   List<OpportunityInfo> pageoppRecords = new List<OpportunityInfo>();
        //   startIdx = pageToken;
        //   endIdx = startIdx + pageSize;
        //   if (endIdx > oppRecords.size()) {
        //       endIdx = oppRecords.size();
        //   }
        //   for (Integer i = startIdx; i < endIdx; i++) {
        //      pageoppRecords.add(oppRecords.get(i));
        //   }
        //   //end
 
 
        //回传参数
        results.result = 'Success';
        results.oppRecords = oppRecords;
        return results;
 
    }
    //检索条件:数据字段1,数据字段2,数据字段3均满足进入此方法 判断多种情况
    private static String makeTextSql(String textOpts, String equalOpts, String numtext) {
        String soql = '';
        if (!String.isBlank(textOpts)) {
            //寻找检索条件包含‘ ’ 空格的多重检索条件
            //system.debug('314-textOpts:'+textOpts);
            if ((equalOpts == 'contains' || equalOpts == 'notcontains') && numtext.contains(',')) {
                //system.debug('316-equalOpts:'+equalOpts);
                //system.debug('316-numtext:'+numtext);
                String[] vals = numtext.split(',');
                String cSql = '';
                soql += ' AND (';
                for (Integer icount = 0; icount < vals.size(); icount++) {
 
                    //精琢科技   zxk    2021-08-25   start
                    String val = vals[icount];
                    if (equalOpts == 'contains') {
                        cSql = makeTextSqlStr(textOpts, equalOpts, val);
                        soql += cSql;
 
                        if (icount < vals.size() - 1) {
                            soql += ' OR ';
                        }
                    } else if (equalOpts == 'notcontains') {
                        cSql = makeTextSqlStr(textOpts, equalOpts, val);
                        soql += cSql;
                        if (icount < vals.size() - 1) {
                            soql += ' AND ';
                        }
                    }
 
                }
                //精琢科技   zxk    2021-08-25   end
 
 
                //     String val = vals[icount];
                //     cSql = this.makeTextSqlStr(textOpts, equalOpts, val);
                //     system.debug(cSql);
                //     soql += cSql;
                //     if (icount < vals.size() - 1) {
                //         soql += ' OR ';
                //     }
                // }
                soql += ')';
            }
            //寻找检索条件包含‘,’ 逗号的多重检索条件
            else if ((equalOpts == '=' || equalOpts == '<>') && numtext.contains(',')) {
                String[] vals = numtext.split(',');
                soql += ' AND ( ';
                for (Integer icount = 0; icount < vals.size(); icount++) {
                    String val = vals[icount];
                    if (equalOpts == '=') {
                        soql += textOpts + ' = \'' + val + '\'';
                        if (icount < vals.size() - 1) {
                            soql += ' OR ';
                        }
                    } else if (equalOpts == '<>') {
                        soql += textOpts + ' <> \'' + val + '\'';
                        if (icount < vals.size() - 1) {
                            soql += ' AND ';
                        }
                    }
                }
                soql += ')';
            }
            //检索连接符为包含以及不包含
            else if (equalOpts.equals('contains') || equalOpts.equals('notcontains')) {
                if (equalOpts.equals('contains')) {
                    soql += ' AND ' + textOpts + ' LIKE \'%' + numtext + '%\'';
                } else if (equalOpts.equals('notcontains')) {
                    soql += ' AND ( NOT ' + textOpts + ' LIKE \'%' + numtext + '%\' ) ';
                }
            }
            //检索连接符为等于以及不等于
            else if (equalOpts == '=' || equalOpts == '<>') {
                soql += 'AND ' + textOpts + equalOpts + '\'' + numtext + '\'';
            }
 
            //精琢科技   zxk    2021-08-25   start
            //起始字符
            else if (equalOpts == 'starts with' && numtext.contains(' ')) {
                String[] vals = numtext.split(' ');
                soql += ' AND ( ';
                for (Integer icount = 0; icount < vals.size(); icount++) {
                    String val = vals[icount];
                    if (equalOpts == 'starts with') {
                        soql += ' ' + textOpts + '  LIKE  \'%' + val + '%\'' ;
                        if (icount < vals.size() - 1) {
                            soql += ' OR ';
 
                        }
                    }
                }
                soql += ')';
 
            }
            //精琢科技   zxk    2021-08-25   end
 
            // //起始字符
            // else if (equalOpts == 'starts with'){
            //     soql += 'AND ' + textOpts + ' LIKE \'' + numtext + '%\'';
            // }
            //检索连接符的其他情况
            else {
                String cSql = makeTextSqlStr(textOpts, equalOpts, numtext);
                if (equalOpts != '<>') {
                    soql += cSql;
                } else {
                    soql += ' and (NOT ' + cSql + ') ';
                }
            }
        }
        return soql;
    }
    //检索条件:数据字段1,数据字段2,数据字段3均满足,并且进入makeTextSql()之后,内含数据字段包含多种时进入此方法。
    @TestVisible private static String makeTextSqlStr(String textOpts, String equalOpts, String val) {
        String soql = '';
        if (!String.isBlank(textOpts)) {
            String tmpVal = val;
            if (!String.isBlank(tmpVal)) {
                if (equalOpts == 'contains' || equalOpts == 'notcontains') {
                    if (equalOpts == 'contains') {
                        soql += ' ' + textOpts + '  LIKE  \'%' + val + '%\'' ;
                    } else if (equalOpts == 'notcontains') {
                        soql += ' ( NOT ' + textOpts + '  LIKE  \'%' + val + '%\' ) ' ;
                    }
                } else if (equalOpts == '=' || equalOpts == '<>') {
                    if (equalOpts == '=') {
                        soql += ' AND ' + textOpts + ' = ' + val ;
                    } else if (equalOpts == '<>') {
                        soql += ' AND ' + textOpts + ' <> ' + val ;
                    }
                }
            } else {
                soql = ' AND ' + textOpts;
                if (equalOpts == '=') {
                    soql += ' = ' + tmpVal;
                } else if (equalOpts == '<>') {
                    soql += ' <> ' + tmpVal;
                } else if (equalOpts == 'contains') {
                    soql += ' like \'%' + String.escapeSingleQuotes(tmpVal.replaceAll('%', '\\%')) + '%\'';
                } else if (equalOpts == 'notcontains') {
                    soql += ' like \'%' + String.escapeSingleQuotes(tmpVal.replaceAll('%', '\\%')) + '%\'';
                } else if (equalOpts == 'starts with') {
                    soql += ' like \'%' + String.escapeSingleQuotes(tmpVal.replaceAll('%', '\\%')) + '%\'';
                } else {
                    soql += ' ' + equalOpts + ' ' + tmpVal;
                }
            }
        }
        return soql;
    }
 
    //检索按钮:点击检索按钮触发此方法;
    // public static PageReference chick() {
    //     setLayoutRWInfo();
    //     searchOppInner(strColumus,strRtColumus,accSearch,aooSearch,numtext,numtext1,numtext2,sortKey,pageSizeLWC,pageTokenLWC);
    //     return null;
    // }
    //页面内容有修改时,页面判断后传值给changeFlg以及changeFlgRt属性,点击保存时调用此方法,进行保存。
    @AuraEnabled
    public static Results save(List<OpportunityInfo> oppRecordsLWC) {
        //system.debug('oppRecords[1].opp:' + oppRecords[1].opp +' oppRecords[1].AgcOpp:' + oppRecords[1].AgcOpp );
        Results results = new Results();
        oppRecords = oppRecordsLWC;
        try {
            List<Agency_Opportunity__c> updTarget = new List<Agency_Opportunity__c>();
            // List<Opportunity> updOpps = new List<Opportunity>();
            for (OpportunityInfo oi : oppRecords) {
                if (oi.changeFlg == '1') {
                    oi.changeFlg = '0';
                    updTarget.add(oi.AgcOpp);
 
                }
                // if (oi.changeFlgRt == '1' && oi.opp.id != null) {
                //     oi.changeFlgRt = '0';
                //     updOpps.add(oi.opp);
                // }
            }
            if (updTarget.size() > 0) {
                system.debug('updTarget.size:' + updTarget.size() );
                update updTarget;
            }
            // if (updOpps.size() > 0) {
            //     update updOpps;
            // }
            // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '保存完了'));
            results.result = 'Success';
            results.Msg = '保存完了';
            return results;
        } catch (Exception e) {
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请确定科室分类和产品区分的关系'));
        }
        if(saveType == '1') {
            searchOppInner(strColumus,strRtColumus,accSearch,aooSearch,numtext,numtext1,numtext2,sortKey);
            saveType = '';
        } else if (saveType == '2') {
            sortTable();
            saveType = '';
        } else {
        }
        return null;
    }
    //全部展开调用此方法
    public static PageReference sortTable() {
        oppRecords = new List<OpportunityInfo>();
        if (sortKey == preSortKey) {
            if (String.isBlank(sortKey) == false) {
                // 方向が変わるのみ
                sortOrderAsc = !sortOrderAsc;
                sortOrder[Integer.valueOf(sortKey)] = (sortOrderAsc == true ? '↑' : '↓');
            }
        } else {
            sortOrderAsc = true;
            if (String.isBlank(preSortKey) == false) {
                sortOrder[Integer.valueOf(preSortKey)] = ' ';
            }
            sortOrder[Integer.valueOf(sortKey)] = (sortOrderAsc == true ? '↑' : '↓');
        }
        preSortKey = sortKey;
 
        setLayoutRWInfo();
        searchOppInner(strColumus,strRtColumus,accSearch,aooSearch,numtext,numtext1,numtext2,sortKey);
        return null;
    }
      //分页Bean
      public class PaginatedAccounts {
        @AuraEnabled
        public Integer nextPageToken;
        @AuraEnabled
        public Integer pageNumber { get; set; }
        @AuraEnabled
        public Integer totalRecords { get; set; }
        @AuraEnabled
        public Integer recordStart { get; set; }
        @AuraEnabled
        public Integer recordEnd { get; set; }
    }
 
    //自定义类OpportunityInfo,用于创建虚拟字段,合并输出。
    class OpportunityInfo {
        // public Opportunity opp { get; set; }
        @AuraEnabled
        public Agency_Opportunity__c opp { get; set; }
        @AuraEnabled
        public Boolean canEdit { get; private set; }
        @AuraEnabled
        public Boolean hasError { get; private set; }
        @AuraEnabled
        public Boolean hasFieldError { get; private set; }
        @AuraEnabled
        public Integer lineNo { get; private set; }
        @AuraEnabled
        public String changeFlg { get; set; }
        @AuraEnabled
        public String changeFlgRt { get; set; }
        @AuraEnabled
        public Boolean ifLock { get; set; }
        //public String accType { get; private set; }
        @AuraEnabled
        public Agency_Opportunity__c AgcOpp { get; set; }
        public OpportunityInfo(Agency_Opportunity__c record, Agency_Opportunity__c oppo) {
            opp = oppo;
            canEdit = true;
            hasError = false;
            hasFieldError = false;
            lineNo = 0;
            changeFlg = '0';
            changeFlgRt = '0';
            ifLock = Approval.isLocked(record);
            AgcOpp = record;
        }
    }
    public class Results {
        @AuraEnabled
        public String result;
        @AuraEnabled
        public String Msg;
        @AuraEnabled
        public List<OpportunityInfo> oppRecords;
        @AuraEnabled
        public Integer oppCount ;
        @AuraEnabled
        public List<CusOption> timeCusOpts;
        @AuraEnabled
        public List<CusOption> textCusOpts;
        @AuraEnabled
        public List<CusOption> equalCusOpts;
        @AuraEnabled
        public List<CusOption> limitCusOpts;
        @AuraEnabled
        public String limits;
        @AuraEnabled
        public String strColumus;
        @AuraEnabled
        public String strRtColumus;
        @AuraEnabled
        public List<OpportunityInfo> pageoppRecords;
        @AuraEnabled
        public PaginatedAccounts paginatedAccounts;
        @AuraEnabled
        public Boolean canEdit;
        @AuraEnabled
        public String changeFlg;
    }
    public class CusOption {
        CusOption(String value,String label) {
            this.label = label;
            this.value = value;
        }
 
        @AuraEnabled
        public String label;
        @AuraEnabled
        public String value;
    }
  
}