public with sharing class ConInvoiceListController {
    public Consumable_order__c coc { get; set; }
    /*****************検索用******************/
    //经销商产品分类
    public String agencyProType {get;set;}
    //发票号码
    public String category1 { get; set; }
    //客户名
    public String category2 { get; set; }
    //发票开始、结束日期
    private Date cate1 = null;
    private Date cate2 = null;
    // 登录用户 ID
    private String userinfoId = '';
    private String invoiceStatus = '';
    //经销商id
    private String accountid = null;
    // 画面显示数据
    public List<Consumable_order__c> raesList { get; private set; }

    //排序使用
    private String[] orderby = new String[]{ 'Invoice_Date__c','Name','ShipmentAccount__c'};
    public String sortKey { get; set; }
    public String preSortKey { get; private set; }
    public Boolean sortOrderAsc { get; private set; }
    public String[] sortOrder { get; private set; }
    // 登录者工作地
    private String userWorkLocation;

    public ConInvoiceListController() {
        raesList = new List<Consumable_order__c>();
    }

    public void init() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};

        coc = new Consumable_order__c();
        cate1 = coc.Order_date__c;
        cate2 = coc.Deliver_date__c;
        userinfoId = UserInfo.getUserId();
        List<user> Useracc = New List<user>();
        Useracc = [SELECT
                        accountid, Work_Location__c,UserPro_Type__c
                    FROM user
        WHERE id =:userinfoId];
        accountid = Useracc[0].accountid;
        userWorkLocation = Useracc[0].Work_Location__c;
        agencyProType = Useracc[0].UserPro_Type__c;
        if(String.isBlank(Useracc[0].UserPro_Type__c)){
            agencyProType = 'ET';
        }
        invoiceStatus = coc.Invoice_status__c;
        String soql = this.makeSoql(category1,category2,invoiceStatus);
        raesList = Database.query(soql);
    }

    // 发票单检索
    public void invoiceCodeSearch() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};

        cate1 = coc.Order_date__c;
        cate2 = coc.Deliver_date__c;
        invoiceStatus = coc.Invoice_status__c;
        // 获得发票一览
        raesList = new List<Consumable_order__c>();
        String soql = this.makeSoql(category1,category2,invoiceStatus);
        raesList = Database.query(soql);
        if(raesList.size()>0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共检索到' + raesList.size() + '个发票'));
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没有搜索到相关发票。'));
        }
     }
    // 发票单清空
    public void clearUp() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};

        cate1 = coc.Order_date__c = null;
        cate2 = coc.Deliver_date__c = null;
        this.category1 = '';
        this.category2 = '';
        invoiceStatus = coc.Invoice_status__c = null;
        // 获得发票一览
        raesList = new List<Consumable_order__c>();
        String soql = this.makeSoql(category1,category2,invoiceStatus);
        raesList = Database.query(soql);
     }

    //排序
    public void SortLimited(){
        List<Id> orderIdList = new List<Id>();
        for(Integer i = 0 ; i< raesList.size();i++){
            orderIdList.add(raesList[i].Id);
        }
        if (this.sortKey == this.preSortKey) {
            // 方向が変わるのみ
            this.sortOrderAsc = !this.sortOrderAsc;
            this.sortOrder[Integer.valueOf(this.sortKey)] = (this.sortOrderAsc == true ? '↑' : '↓');
        } else {
            this.sortOrderAsc = true;
            this.sortOrder[Integer.valueOf(this.preSortKey)] = ' ';
            this.sortOrder[Integer.valueOf(this.sortKey)] = (this.sortOrderAsc == true ? '↑' : '↓');
        }

        this.preSortKey = this.sortKey;
        // 画面记录取得
        String soql = 'SELECT Id, Name,Invoice_Date__c,ShipmentAccount__c,Shipment_total_amount__c,RrturnPro_total_amount__c,Invoice_status__c, ';
        soql += ' InvoicedPro_total_amount__c,InvoiceNotPro_total_amount__c,Invoice_total_amount__c,Invoice_attachment__c,Invoicedet_attachment__c FROM Consumable_order__c ';
        soql += ' WHERE Id in :orderIdList';
        //soql += ' order by Name';
        soql += ' order by ' + this.orderby[Integer.valueOf(this.sortKey)];
        soql += ' ' + (this.sortOrderAsc == true ? 'asc nulls first' : 'desc nulls last');
        System.debug('soql+++++' + soql);
        List<Consumable_order__c> queryList = Database.query(soql);
        raesList = queryList;
    }

    // 做成检索SQL文
    private String makeSoql(String invoiceCode,String accountName,String invoiceStatus){
        String soql = 'SELECT Id, Name,Invoice_Date__c,ShipmentAccount__c,Shipment_total_amount__c,RrturnPro_total_amount__c,Invoice_status__c, ';
        soql += ' InvoicedPro_total_amount__c,InvoiceNotPro_total_amount__c,Invoice_total_amount__c,Invoice_attachment__c,Invoicedet_attachment__c FROM Consumable_order__c ';
        soql += ' WHERE Dealer_Info__c = \'' +  accountid + '\'';
        soql += ' AND Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        soql += ' AND recordtypeid = \''+ System.Label.RT_ConOrder_Invoice + '\'';
        if(cate1 != null){
            soql += ' AND Invoice_Date__c >= :cate1 ' ;
        }
        if(cate2 != null){
            soql += ' AND Invoice_Date__c <= :cate2 ' ;
        }
         if(!String.isBlank(invoiceCode)){
            soql += ' AND Name like \'%' + String.escapeSingleQuotes(invoiceCode.replaceAll('%', '\\%')) + '%\' ';
        }
         if(!String.isBlank(accountName)){
            soql += ' AND ShipmentAccount__c like \'%' + String.escapeSingleQuotes(accountName.replaceAll('%', '\\%')) + '%\' ';
        }
        if(!String.isBlank(invoiceStatus)){
            soql += ' AND Invoice_status__c =\'' + invoiceStatus +'\' ';
        }
        System.debug('soql+++++' + soql);
        return soql;
    }
}