public without sharing class ConSearchAgencyPlace {
	public List<Account> results {get; private set;}
	public Boolean getIsOverLimit() {
		if (results != null && results.size() > 50) {
			return true;
		}
		return false;
	}

	public ConSearchAgencyPlace () {
		this.results = new List<Account>();
	}
	public void search() {
		String query = System.currentPageReference().getParameters().get('q');
		String agency = System.currentPageReference().getParameters().get('r');
        System.debug('query = ' + query);
        System.debug('agency = ' + agency);
		this.search(query, agency);
	}
	public void search(String query, String reporterState) {

        //List<String> qwordstmp = query.split(' ');
        // nameCondition 用于拼接用户输入内容,做模糊查询
        String nameCondition = '';
        nameCondition    += '%' + String.escapeSingleQuotes(query.replaceAll('%', '\\%')) + '%';
        /*for (String qword : qwordstmp) {
            if (String.isBlank(qword) == false) {
                qwords.add('%' + String.escapeSingleQuotes(qword.replaceAll('%', '')) + '%');
                nameCondition += ' Name like \'' + qwords[qwords.size() - 1] + '\' and';
            }
        }*/
        
        /*if(qwords.size() < 2 && query.length() < 3) {
            return;
        }*/
        // system.debug('query.length=' + query + '===' +  query.length());
        // if (String.isBlank(reporterState)) {
        //     return;
        // }
        // if( query.length() < 2) {
            // return;
        // }
        
        
        List<Account> accs;
        // 查询符合条件的特约经销商
        // 直接使用SOQL的方式进行模糊查询时不需要对引号进行转义
        // 如果使用DataBase类的query方法则传入的字符串中如果有引号需要对其进行转义
        System.debug('nameCondition = ' + nameCondition);
        accs = [SELECT id,Parentid
                FROM Account
                WHERE  Parent.Name like :nameCondition
                AND Contract_Decide_Start_Date__c <= :Date.Today()
                AND Contract_Decide_End_Date__c >= :Date.Today()];
                
        // 用于保存特约经销商匹配到的其父类的id
        Set<String> pids = new Set<String>();     
        for (Account acc : accs) {
            pids.add(acc.Parentid);
        }         


        // 検索
        // String queryString = 'select Id, Name, Department_Class__c, Department_Class__r.Name, Hospital__c, Hospital__r.Name from Account where id in' + qwords + ' and RecordType.DeveloperName = \'Agency\' and Is_Active__c <> \'無効\' order by Name limit 51';
        // system.debug('queryString=' + queryString);
        // Map<Id, Account> accMap = new Map<Id, Account>((List<Account>) Database.query(queryString));
        // if (accMap.size() <= 50) {
        //     // さらに省単位のデータを検索
        //     nameCondition += ' and RecordType.DeveloperName = \'Agency\' and ';
        //     Map<Id, Account> stateDepartmentMap = ControllerUtil.selectDepartByHospitalState(nameCondition, reporterState, 51 - accMap.size());
        //     if (stateDepartmentMap.size() > 0) {
        //         for (Id accId : stateDepartmentMap.keySet()) {
        //             if (!accMap.containsKey(accId)) {
        //                 accMap.put(accId, stateDepartmentMap.get(accId));
        //             }
        //         }
        //     }
        // }

        // 検索,查询到最终匹配的经销商名单
        System.debug('nameCondition = ' + nameCondition);
        System.debug('pids = ' + pids);
        accs = [
                SELECT Id, Name, 
                Department_Class__c, Department_Class__r.Name, 
                Hospital__c, Hospital__r.Name 
                FROM Account
                WHERE (Id IN :pids
                    OR (Name = '奥林巴斯（北京）销售服务有限公司' AND Name like :  nameCondition))
                AND RecordType.DeveloperName = 'Agency'
                AND Is_Active__c <> '無効' order by Name limit 49
        ];
        
        
        Map<Id, Account> accMap = new Map<Id, Account>(accs);
        
        this.results = accMap.values();                         // values()の場合、順序わからないです
        this.results.sort();                                    // order by Name
	}

}