buli
2023-06-05 3962c2bb0435484b60a3e408e4738d792e249a53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
    }
}