高章伟
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
global with sharing  class LostByCompany {
    public List<retLine> results {get; private set;}
    public String lineNo {get;set;}
    public String SearchName {get;set;}   
    public String queryCopy {get;set;}   
    public Boolean getIsOverLimit() {
        if (results != null && results.size() > 50) {
            return true;
        }
        return false;
    }
    public LostByCompany() {
        this.results = new List<retLine>();
    }
 
    // 获取列表值
    /*
    public void search() {
        String sname = 'PCLLostBrand__c';
        String fname = 'Lost_By_Company__c';
        List<String> retlist = new List<String>();
        Map<String, Schema.SObjectField> sof;
        List<SObject> so = Database.query('Select Id from ' + sname +  ' limit 1');
        if (so.size() > 0) {
            sof = so[0].getSObjectType().getDescribe().fields.getMap();
        } else {
            sof = Schema.getGlobalDescribe().get(sname).newSObject().getSObjectType().getDescribe().fields.getMap();
        }
        if (!sof.containsKey(fname)) this.results =  null;
        Schema.DescribeFieldResult dfr = sof.get(fname).getDescribe();
        String ftype = dfr.getType().name();
        if (ftype != 'PICKLIST' && ftype != 'MULTIPICKLIST') this.results =  null;
 
        for (Schema.PicklistEntry pe : dfr.getPicklistValues()) {
            String val = '';
            val = pe.getLabel(); 
            if (pe.isDefaultValue()) val += '(デフォルト)';
     
            if (!pe.isActive()) val += '(無効)';
            retlist.add(val);
        }
        this.results =  retlist;
    }
    */
 
    // SWAG-CJNAJG 获取列表的值 上面那个太复杂了 换一种写法
    public void search() {
        
        // 增加一个输入参数的筛选
        String query = System.currentPageReference().getParameters().get('q');
        String line_no = System.currentPageReference().getParameters().get('l');
        query = query.trim();
        SearchName=query;
        queryCopy=query;
        this.lineNo = String.isNotBlank(line_no) ? line_no : '';
        this.results =  searchLogic(query);
    }
    public PageReference searchOne() {
        system.debug('SearchName++'+SearchName);
        this.results =  searchLogic(SearchName);
        return null;
    }
    public List<retLine> searchLogic(String query){
        // 初始化参数
        List<retLine> retlist = new List<retLine>();
        system.debug('query++'+query);
        // 获取对象结构
        Map<String, Schema.SObjectField> sof = Schema.getGlobalDescribe().get('PCLLostBrand__c').getDescribe().fields.getMap();
        // 获取选项列表
        List<Schema.PicklistEntry> brandPLEntries = sof.get('Lost_By_Company__c').getDescribe().getPicklistValues();
        // 循环列表
        Integer i = 0;
        for (Schema.PicklistEntry entry : brandPLEntries) {
            system.debug('111++'+(String.isNotBlank(query) && !entry.getValue().contains(query)));
            system.debug('222++'+!entry.isActive());
            // 跳过被禁用的选项,跳过和输入值不匹配的选项
            if (!entry.isActive() || (String.isNotBlank(query) && !entry.getValue().contains(query))) {
                continue;
            }
            retLine ret = new retLine();
            ret.idx=i;
            ret.retvalue=entry.getValue();
            system.debug('ret++'+ret);
            retlist.add(ret);
            i++;
        }
        system.debug('retlist++'+retlist);
        return retlist;
    }
    public class retLine {
        public Integer idx {get; set;}
        public String retvalue {get; set;}
    }
 
}