public without sharing class searchAgencyHospitalController {
    //经销商用户产品分类（ET、ENG）
    public String agencyProType {get;set;}
    public String searchName {get;set;}               // 検索文字列
    private String accountid = null;
    public List<Account> at {get;set;}
    private String ctype;
    public searchAgencyHospitalController() {
        ctype = ApexPages.currentPage().getParameters().get('Ctype');
    }

    public void init(){
        //userId = '0050k000000Krop';
        User useracc = [SELECT accountid,UserPro_Type__c FROM user WHERE id = :UserInfo.getUserId() ];
        accountid = Useracc.accountid;
        agencyProType = Useracc.UserPro_Type__c;
        if(String.isBlank(Useracc.UserPro_Type__c)){
            agencyProType = 'ET';
        }
        String soql = 'SELECT id,Name,State_Master__c,State_Master__r.Name FROM Account';
        soql += ' where id in (SELECT Hospital__c FROM Agency_Hospital_Link__c WHERE Agency__c = \'' + accountid + '\' AND Hosptial_Type__c like \'%' + String.escapeSingleQuotes(ctype.replaceAll('%', '\\%')) + '%\')' ;
        soql += ' order by Name desc limit 100';
        at = Database.query(soql);
    }

    public PageReference serContact(){
        String aaaa = this.makeSoql(searchName,accountid,ctype);
        at = Database.query(this.makeSoql(searchName,accountid,ctype));
        return null;
    }

    private String makeSoql(String searchName,String accountid,String ctype){

        String soql = 'SELECT id,Name,State_Master__c,State_Master__r.Name FROM Account';
        soql += ' where id in (SELECT Hospital__c FROM Agency_Hospital_Link__c WHERE Agency__c = \'' + accountid + '\' AND Hosptial_Type__c like \'%' + String.escapeSingleQuotes(ctype.replaceAll('%', '\\%')) + '%\')' ;
        if(String.isNotBlank(searchName)){
            soql += ' AND Name like \'%' + String.escapeSingleQuotes(searchName.replaceAll('%', '\\%')) + '%\'';
        }
        soql += ' order by Name desc limit 100';
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'soql' + soql));
        return soql;

    }
    
}