public without sharing class lexSearchAgencyHospitalController {
    //经销商用户产品分类（ET、ENG）
    public static String agencyProType { get; set; }
    public static String searchName { get; set; }
    private static String accountid = null;
    @AuraEnabled
    public static List<Account> at { get; set; }

    @AuraEnabled
    public static ResponseBodyLWC init(String ctype) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        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);
        data.put('at', at);
        res.status = 'Success';
        res.code = 200;
        System.debug('res = ' + res);
        return res;
    }

    @AuraEnabled
    public static ResponseBodyLWC serContact(String searchName, String ctype) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        User useracc = [SELECT accountid, UserPro_Type__c FROM user WHERE id = :UserInfo.getUserId()];
        accountid = Useracc.accountid;
        at = Database.query(makeSoql(searchName, accountid, ctype));
        data.put('at', at);
        res.status = 'Success';
        res.code = 200;
        System.debug('res = ' + res);
        return res;
    }

    private static 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;
    }
}