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
    }
}