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
public without sharing class TS_SearchAccountController {
    public List<Account> results {get; private set;}
    public Boolean getIsOverLimit() {
        if (results != null && results.size() > 50) {
            return true;
        }
        return false;
    }
 
    public TS_SearchAccountController () {
        this.results = new List<Account>();
    }
 
    public void search() {
        // 検索条件を用意
        String query = System.currentPageReference().getParameters().get('q');
        system.debug('query--->'+query);
        String reporterState = System.currentPageReference().getParameters().get('r');
        reporterState = reporterState == 'LS' ? 'BS':reporterState;
 
        if ('NDT/ANI'.equals(reporterState)) {
            reporterState = '';
            reporterState +=  ' ( ProductSegmentF__c = \'' +'NDT' +'\' OR ';
            reporterState +=  ' ProductSegmentF__c = \'' +'ANI' +'\') AND ';
        } else if ('System'.equals(reporterState)) {
            reporterState = '';
            reporterState +=  ' ( ProductSegmentF__c = \'' +'NDT' +'\' OR ';
            reporterState +=  ' ProductSegmentF__c = \'' +'BS' +'\' OR ';
            reporterState +=  ' ProductSegmentF__c = \'' +'IE' +'\' OR ';
            reporterState +=  ' ProductSegmentF__c = \'' +'RVI' +'\' OR ';
            reporterState +=  ' ProductSegmentF__c = \'' +'ANI' +'\') AND ';
        } else {
            reporterState =  ' ProductSegmentF__c = \'' +reporterState +'\' AND ';
        }
        if (query == null || query == '') {
            return;
        }
 
        this.search(query, reporterState);
    }
 
 
    public void search(String query, String reporterState) {
        List<String> qwords = new List<String>();
        List<String> qwordstmp = query.split(' ');
        String nameCondition = '';
        for (String qword : qwordstmp) {
            if (String.isBlank(qword) == false) {
                qwords.add('%' + String.escapeSingleQuotes(qword.replaceAll('%', '')) + '%');
                nameCondition += ' Name like \'' + qwords[qwords.size() - 1] + '\' AND ';
            }
        }
        
        // 検索
        // 客户 记录类型
        String accountRecordTypeId =  TSRepairUtil.AccountBaseRecordTypeIdSQL() + ' AND ';
        // 客户 产品分类
        // String accountRecordTypeDelear = ' ProductSegmentF__c = \'' +reporterState +'\' AND ';
        // String queryString = 'select Id, ProductSegmentF__c,Name from Account where '+ nameCondition +accountRecordTypeId +accountRecordTypeDelear+' Id != null order by Name limit 51';
         String queryString = 'select Id, ProductSegmentF__c,Name from Account where '+ nameCondition +accountRecordTypeId +reporterState+' Id != null order by Name limit 51';
        system.debug('queryString=' + queryString);
        Map<Id, Account> accMap = new Map<Id, Account>((List<Account>) Database.query(queryString));
        // system.debug('accMap--->'+accMap.values());
        this.results = accMap.values();                         // values()の場合、順序わからないです
        this.results.sort();                                    // order by Name
    }
}