public with sharing class LexConInvoiceList {
    public static Consumable_order__c coc { get; set; }
    public static String agencyProType { get; set; }
    public static String category1 { get; set; }
    public static String category2 { get; set; }
    //发票开始、结束日期
    private static Date cate1 { get; set; }
    private static Date cate2 { get; set; }
    // 登录用户 ID
    private static String userinfoId { get; set; }
    private static String invoiceStatus { get; set; }
    //经销商id
    private static String accountid { get; set; }
    // 画面显示数据
    public static List<Consumable_order__c> raesList { get; private set; }
    //排序使用
    private static String[] orderby = new List<String>{ 'Invoice_Date__c', 'Name', 'ShipmentAccount__c' };
    public static String sortKey { get; set; }
    public static String preSortKey { get; private set; }
    public static Boolean sortOrderAsc { get; private set; }
    public static String[] sortOrder { get; private set; }
    // 登录者工作地
    private static String userWorkLocation { get; set; }
    public static Map<String, String> statusMap { get; set; }

    @AuraEnabled
    public static ResponseBodyLWC init() {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;

        statusMap = new Map<String, String>();
        raesList = new List<Consumable_order__c>();
        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 = makeSoql(category1, category2, invoiceStatus);
        raesList = Database.query(soql);

        statusMap = getPicklistValues('Consumable_order__c', 'Invoice_status__c');

        data.put('userinfoId', userinfoId);
        data.put('accountid', accountid);
        data.put('userWorkLocation', userWorkLocation);
        data.put('agencyProType', agencyProType);
        data.put('raesList', raesList);
        data.put('statusMap', statusMap);
        res.status = 'Success';
        res.code = 200;
        System.debug('res = ' + res);
        return res;
    }

    // 发票单检索
    @AuraEnabled
    public static ResponseBodyLWC invoiceCodeSearch(
        Date orderDateLwc,
        Date deliverDateLwc,
        String invoiceStatusLwc,
        String category1Lwc,
        String category2Lwc,
        String accountidLwc,
        String userWorkLocationLwc,
        String agencyProTypeLwc
    ) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;

        cate1 = orderDateLwc;
        cate2 = deliverDateLwc;
        invoiceStatus = invoiceStatusLwc;
        category1 = category1Lwc;
        category2 = category2Lwc;
        accountid = accountidLwc;
        userWorkLocation = userWorkLocationLwc;
        agencyProType = agencyProTypeLwc;
        // 获得发票一览
        raesList = new List<Consumable_order__c>();
        String soql = 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, '没有搜索到相关发票。'));
        // }
        data.put('raesList', raesList);
        data.put('raesListSize', raesList.size());
        res.status = 'Success';
        res.code = 200;
        System.debug('res = ' + res);
        return res;
    }

    // 做成检索SQL文
    private static 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;
    }

    /*
    通过sObjectName和指定字段获取PickList的value值
    */
    public static Map<String, String> getPicklistValues(String sObjectName, String sFieldName) {
        Map<String, String> picValues = new Map<String, String>();
        picValues.put('-无-', '');
        Map<String, Schema.SObjectType> sObjectDescribeMap = Schema.getGlobalDescribe();
        if (sObjectDescribeMap.containsKey(sObjectName)) {
            Map<String, Schema.SobjectField> sObjectFieldsMap = sObjectDescribeMap.get(sObjectName).getDescribe().fields.getMap();
            if (sObjectFieldsMap.containsKey(sFieldName)) {
                Schema.DescribeFieldResult sObjectFieldDescribe = sObjectFieldsMap.get(sFieldName).getDescribe();
                List<Schema.PicklistEntry> lPickEntryList = sObjectFieldDescribe.getPicklistValues();
                //tPicklistEntry.isDefaultValue()
                for (Schema.PicklistEntry tPicklistEntry : lPickEntryList) {
                    picValues.put(tPicklistEntry.getLabel(), tPicklistEntry.getValue());
                }
            } else {
                //TODO 不存在此字段的执行
            }
        } else {
            // TODO 不存在此sObject的执行
        }
        return picValues;
    }
}