public without sharing class ConsumableOrderManageController {
    /****************画面表示同****************/
    public Consumable_order__c coc { get; set; }
    /*****************検索用******************/
    //经销商产品分类
    public String agencyProType {get;set;}

    public String category1 { get; set; }
    /*****************画面表示Bean******************/
    public String baseUrl {get;private set;}
    private String[] columus = new String[]{ 'Product2__c.Name'};
    /*****************ソート時再検索条件（画面からの入力条件を無視するため）******************/
    public List<SelectOption> provinceOpts  { get; set; }
    private String accountid = null;
    // 订单 字段标签
    public List<String> title { get; private set; }
    // 订单 字段名
    public List<String> column;
    public List<List<String>> columns { get; private set; }
    // 画面显示数据
    public List<Consumable_order__c> raesList { get; private set; }
    public ConsumableOrderManageController() {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
    }

    // 登录者工作地
    private String userWorkLocation;

    //add by rentx 2021-3-10 start
    //经销商下没有维护特价医院时 隐藏医院特价按钮
    public Boolean hasHop {get;set;}
    //经销商没有维护促销价格 隐藏促销订货按钮
    public Boolean hasSpecial {get;set;}
    //add by rentx 2021-3-10 end

    //========20160311======ADD_Start================================
    // 将订单明细以字段SET 方式显示
    //========20160311======ADD_Start================================
    public void init1() {
        
        String userId = UserInfo.getUserId();
     
        List<user> Useracc = New List<user>();
        Useracc = [SELECT accountid, Work_Location__c,UserPro_Type__c
                    FROM user
                    WHERE id =:userId];
        accountid = Useracc[0].accountid;
        agencyProType = Useracc[0].UserPro_Type__c;
        if(String.isBlank(Useracc[0].UserPro_Type__c)){
            agencyProType = 'ET';
        }
        userWorkLocation = Useracc[0].Work_Location__c;
        coc = new Consumable_order__c();
        // 获得订单一览
        Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get('Consumable_order__c').getDescribe().fieldSets.getMap();
        Schema.FieldSet fs = fsMap.get('order_view');
        // 获得订单中的所有项目
        List<FieldSetMember> fsmList = fs.getFields();
        // 获得字段标签和字段名
        title = new List<String>();
        column = new List<String>();
        columns = new List<List<String>>();
        for (FieldSetMember fsm : fsmList) {
            //update by rentx 2020-12-22 start
            if (fsm.getLabel() == '医院' && agencyProType != null && agencyProType == 'ET') {
            }else{                
                title.add(fsm.getLabel());
            }
            if (fsm.getFieldPath() == 'Order_ForHospital__c' && agencyProType != null && agencyProType == 'ET') {
            }else{
                column.add(fsm.getFieldPath());
                columns.add(fsm.getFieldPath().split('\\.'));
            }
            // title.add(fsm.getLabel());
            // column.add(fsm.getFieldPath());
            // columns.add(fsm.getFieldPath().split('\\.'));
            //update by rentx 2020-12-22 end
        }

        provinceOpts = new List<SelectOption>();
        provinceOpts.add(new SelectOption('', '-无-'));
        provinceOpts.add(new SelectOption('草案中', '草案中'));
        provinceOpts.add(new SelectOption('已提交', '已提交'));
        provinceOpts.add(new SelectOption('批准', '批准'));
        provinceOpts.add(new SelectOption('驳回', '驳回'));
        // 获得显示数据
        raesList = new List<Consumable_order__c>();

            String soql = 'select Id';

            for (String s : column) {
                soql += ',' + s;
            }

            soql += ' from Consumable_order__c where Order_type__c = \''+'订单'+'\'';
            soql += ' and RecordtypeId = \'' + System.Label.RT_ConOrder_Order + '\'';
            soql += ' and Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\'';
            soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
            soql += ' and Dealer_Info__c =\'' + accountid +'\' order by Order_status__c ';
            System.debug('====soql====' + soql);
            raesList = Database.query(soql);


        //add by rentx 2021-3-10 start
        //为 hasHop 赋值 判断当前经销商下是否有特价医院
        List<hospitalprice__c> hopList = [select id,hospital__c from hospitalprice__c where account__c = :accountid];
        if (hopList == null || hopList.size() == 0) {
            hasHop = false;
        }else{
            hasHop = true;
        }

        //为 hasSpecial 赋值 判断当前经销商下是否有促销商品
        //查询当前经销商下的有效合同
        List<Account> contractList = [select Id,Name,RecordType.DeveloperName
                                        from Account
                                        where RecordType.DeveloperName ='AgencyContract'
                                        and Contact_Type__c like :agencyProType
                                        and Agent_Ref__c =:accountid];
        List<Id> dealIds = new List<Id>();
        if (contractList != null && contractList.size() > 0) {
            for (Account acc : contractList) {
                dealIds.add(acc.Id);
            }
            List<Dealer_Product__c> deList = [select id from Dealer_Product__c where Dealer_Contact__c in :dealIds ];
            if (deList == null || deList.size() == 0) {
                hasSpecial = false;
            }else{
                hasSpecial = true;
            }
        }else {
            hasSpecial = false;
        }
        //add by rentx 2021-3-10 end
    }
    //========20160311======ADD_End================================

    // 检索
    public void searchConsumableorderdetails() {
        Date cate2 = coc.Order_date__c;

        String cate3  = coc.Order_status__c;
        
        // 获得订单一览
        Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get('Consumable_order__c').getDescribe().fieldSets.getMap();
        Schema.FieldSet fs = fsMap.get('order_view');
        // 获得订单中的所有项目
        List<FieldSetMember> fsmList = fs.getFields();
        // 获得字段标签和字段名
        title = new List<String>();
        column = new List<String>();
        columns = new List<List<String>>();
        for (FieldSetMember fsm : fsmList) {
            //update by rentx 2020-12-22 start
            if (fsm.getLabel() == '医院' && agencyProType != null && agencyProType == 'ET') {
            }else{
                title.add(fsm.getLabel());
            }
            if (fsm.getFieldPath() == 'Order_ForHospital__c' && agencyProType != null && agencyProType == 'ET') {
            }else{
                column.add(fsm.getFieldPath());
                columns.add(fsm.getFieldPath().split('\\.'));
            }
            // title.add(fsm.getLabel());    
            // column.add(fsm.getFieldPath());
            // columns.add(fsm.getFieldPath().split('\\.'));
            //update by rentx 2020-12-22 end
        }
        // 获得显示数据
        raesList = new List<Consumable_order__c>();
        String soql = 'select Id';
        for (String s : column) {
            soql += ',' + s;
        }
        soql += ' from Consumable_order__c where Order_type__c = \''+'订单'+'\'and RecordtypeId = \'' + System.Label.RT_ConOrder_Order + '\' and Dealer_Info__c =\'' + accountid +'\' ';
        soql += ' and Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        if (!String.isBlank(category1)) {
            soql += ' and Name like \'%' + String.escapeSingleQuotes(category1.replaceAll('%', '\\%')) + '%\' ';
        }
        if(cate2 != null){
            soql += ' and Order_date__c = :cate2';
        }
        if(cate3 != null){
            soql += ' and Order_status__c = :cate3 ';
        }
        soql += ' order by Order_status__c ';
      
        system.debug('====soql:' + soql);
        raesList = Database.query(soql);
        system.debug('====raesList:' + raesList);
        if(raesList.size()>0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有' + raesList.size() + '个订单'));
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没有搜索到相关订单。'));
        }
     }

    //新建订单
    public PageReference neworder() {
        // 返回备品set
        PageReference ref = new Pagereference('/apex/Consumable');
        ref.setRedirect(true);
        return ref;
    }

    //add by rentx 2020-11-20 start
    //协议订货
    public PageReference agreementorder() {
        // 返回备品set
        // PageReference ref = new Pagereference('/apex/Consumable?type=agreementorder');
        PageReference ref = new Pagereference('/apex/Consumable?type=agreementorder');
        ref.setRedirect(true);
        return ref;
    }

    //医院特价
    public PageReference hospitalorder() {
        // 返回备品set
        PageReference ref = new Pagereference('/apex/Consumable?type=hospitalorder');
        ref.setRedirect(true);
        return ref;
    }

    //促销订货
    public PageReference promotionorder() {
        // 返回备品set
        PageReference ref = new Pagereference('/apex/Consumable?type=promotionorder');
        ref.setRedirect(true);
        return ref;
    }

    //add by rentx 2020-11-20 start
}