buli
2023-07-11 80a3f59e2d3df07805bc67e329300b8de90a5b3a
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
public without sharing class LexConsumableAccountController {
    //经销商用户产品分类(ET、ENG)
    public static String agencyProType { get; set; }
    public static String agencyProTypestr { get; set; }
    //选项卡名称
    public static String filterName { get; set; }
    //选项卡类型
    public static String hosptialType { get; set; }
    public static ApexPages.StandardSetController setCon { get; set; }
    //画面显示数量
    public static Integer size { get; set; }
    public static Integer noOfRecords { get; set; }
    //显示集合
    public static List<Account> pageRecords { get; set; }
    //经销商信息
    private static String accountName { get; set; }
    private static String accountId { get; set; }
    //排序使用
    // 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[] orderby = new String[]{ 'Name','State_Master__c','Salesdepartment_HP__c','CreatedDate','Is_Active__c','RecordType.Name','Grade__c','OCM_Category__c','City_Master__c','Town__c','Phone'};
    //上周开始、结束日期
    private static Datetime lastweekstart { get; set; }
    private static Datetime lastweekend { get; set; }
    // page
    public static Integer pagesize { get; set; }
    public static Integer pageToken { get; set; }
    public static String sortField { get; set; }
    public static String sortOrder { get; set; }
    public static Integer totalcount { get; set; }
 
    //初始化
    @AuraEnabled
    public static ResponseBodyLWC init(Integer pageSizeLWC, Integer pageTokenLWC, String fiscalYear) {
        try {
            ResponseBodyLWC res = new ResponseBodyLWC();
            Map<String, object> data = new Map<String, object>();
            res.entity = data;
 
            pageSize = pageSizeLWC;
            pageToken = pageTokenLWC > 2000 ? 2000 : pageTokenLWC;
 
            User Useracc = [
                SELECT accountid, UserPro_Type__c
                FROM user
                WHERE id = :UserInfo.getUserId()
            ];
            accountId = [SELECT id, Name FROM account WHERE id = :Useracc.accountid].id;
            agencyProType = Useracc.UserPro_Type__c;
            if (String.isBlank(Useracc.UserPro_Type__c)) {
                agencyProType = 'ET';
            }
            agencyProTypestr = '%' + agencyProType + '%';
            System.debug('accountId = ' + accountId);
            System.debug('agencyProTypestr = ' + agencyProTypestr);
            List<Account> allSelectAccount = [
                SELECT
                    id,
                    Name,
                    State_Master__c,
                    State_Master__r.Name,
                    Salesdepartment_HP__c,
                    CreatedDate,
                    Is_Active__c,
                    RecordType.Name,
                    Grade__c,
                    OCM_Category__c,
                    City_Master__c,
                    City_Master__r.Name,
                    Town__c,
                    Phone
                FROM Account
                WHERE
                    id IN (
                        SELECT Hospital__c
                        FROM Agency_Hospital_Link__c
                        WHERE Agency__c = :accountId AND Hosptial_Type__c LIKE :agencyProTypestr
                    )
            ];
            totalCount = allSelectAccount.size();
 
            List<Account> selectAccList = [
                SELECT
                    id,
                    Name,
                    State_Master__c,
                    State_Master__r.Name,
                    Salesdepartment_HP__c,
                    CreatedDate,
                    Is_Active__c,
                    RecordType.Name,
                    Grade__c,
                    OCM_Category__c,
                    City_Master__c,
                    City_Master__r.Name,
                    Town__c,
                    Phone
                FROM Account
                WHERE
                    id IN (
                        SELECT Hospital__c
                        FROM Agency_Hospital_Link__c
                        WHERE Agency__c = :accountId AND Hosptial_Type__c LIKE :agencyProTypestr
                    )
                LIMIT :pageSize
            ];
            System.debug('selectAccList = ' + selectAccList);
            PaginatedAccounts paginatedAccounts = new PaginatedAccounts();
            paginatedAccounts.nextPageToken = (pageToken + pageSize < totalCount) ? pageToken + pageSize : null;
            paginatedAccounts.recordStart = pageToken + 1;
            paginatedAccounts.pageNumber = pageToken / pageSize + 1;
            Integer recordEnd = pageSize * paginatedAccounts.pageNumber;
            paginatedAccounts.recordEnd = totalCount >= recordEnd ? recordEnd : totalCount;
            paginatedAccounts.totalRecords = totalCount;
 
            //销量前十的客户
            // List<Account> acList = [
            //     SELECT
            //         id,
            //         Name,
            //         State_Master__c,
            //         State_Master__r.Name,
            //         Salesdepartment_HP__c,
            //         CreatedDate,
            //         Is_Active__c,
            //         RecordType.Name,
            //         Grade__c,
            //         OCM_Category__c,
            //         City_Master__c,
            //         City_Master__r.Name,
            //         Town__c,
            //         Phone
            //     FROM Account
            //     WHERE State_Master__r.Name != '' AND City_Master__r.Name != '' AND Is_Active__c = '有効' AND RecordType.Name = '病院'
            //     LIMIT 10
            // ];
            TopInfo topInfo = new TopInfo();
 
            Date today = Date.today();
            Integer lastYear = today.year() - 1;
            Integer thisYear = today.year();
            Integer nextYear = today.year() + 1;
            Date lastDatetime = Date.newInstance(lastYear, 4, 1);
            Date thisDatetime = Date.newInstance(thisYear, 4, 1);
            Date thisDatetime2 = Date.newInstance(thisYear, 4, 1);
            Date nextDatetime = Date.newInstance(nextYear, 4, 1);
 
            AggregateResult[] arList = null;
 
            System.debug('fiscalYear = ' + fiscalYear);
            if (fiscalYear == 'thisYear') {
                AggregateResult[] saleAmountList = LexConsumableAccountSOQL.getAccountTotalSales(thisDatetime, nextDatetime);
                topInfo.saleAmount = Decimal.valueOf(String.valueOf(saleAmountList[0].get('saleAmount')));
                System.debug('topInfo.saleAmount = ' + topInfo.saleAmount);
                arList = LexConsumableAccountSOQL.getAccountBySales(thisDatetime, nextDatetime);
            } else if (fiscalYear == 'lastYear') {
                AggregateResult[] saleAmountList = LexConsumableAccountSOQL.getAccountTotalSales(lastDatetime, thisDatetime2);
                topInfo.saleAmount = Decimal.valueOf(String.valueOf(saleAmountList[0].get('saleAmount')));
                System.debug('topInfo.saleAmount = ' + topInfo.saleAmount);
                arList = LexConsumableAccountSOQL.getAccountBySales(lastDatetime, thisDatetime2);
            } else {
                AggregateResult[] saleAmountList = LexConsumableAccountSOQL.getAccountTotalSales(lastDatetime, nextDatetime);
                topInfo.saleAmount = Decimal.valueOf(String.valueOf(saleAmountList[0].get('saleAmount')));
                System.debug('topInfo.saleAmount = ' + topInfo.saleAmount);
                arList = LexConsumableAccountSOQL.getAccountBySales(lastDatetime, nextDatetime);
            }
 
            // Map<String,Decimal> mapTemp = new Map<String,Decimal>();
            // for (AggregateResult ar : arList) {
            //     if(!mapTemp.get(String.valueOf(ar.get('hospitalName')))){
            //         mapTemp.put(String.valueOf(ar.get('hospitalName'),Decimal.valueOf(String.valueOf(ar.get('thisAmount')))));
            //     }else {
            //         Decimal decimal = mapTemp.get(String.valueOf(ar.get('hospitalName')));
            //         decimal += Decimal.valueOf(String.valueOf(ar.get('thisAmount')));
            //         mapTemp.put(String.valueOf(ar.get('hospitalName'),decimal));
            //     }
            // }
            // System.debug('mapTemp = ' + mapTemp);
            // //排序Decimal从大到小
            // List<Decimal> decimalList = new List<Decimal>(mapTemp.values());
            // decimalList.sort((a, b) => b.compareTo(a));
            // Map<String, Decimal> sortedMap = new Map<String, Decimal>();
            // for (String key : mapTemp.keySet()) {
            //     sortedMap.put(key, mapTemp.get(key));
            // }
 
            // System.debug('Sorted Map: ' + sortedMap);
 
            //查询客户信息(名称,省,县)
            System.debug('arList = ' + arList);
            List<String> accountList = new List<String>();
            for (AggregateResult ar : arList) {
                System.debug('thisAmount = ' + String.valueOf(ar.get('thisAmount')));
                if (String.valueOf(ar.get('thisAmount')) != '0.0') {
                    accountList.add(String.valueOf(ar.get('Order_ForHospital__c')));
                    topInfo.deList.add(Decimal.valueOf(String.valueOf(ar.get('thisAmount'))));
                }
            }
 
            System.debug('accountList = ' + accountList);
            System.debug('topInfo = ' + topInfo);
 
            List<Account> acList = [
                SELECT
                    id,
                    Name,
                    State_Master__c,
                    State_Master__r.Name,
                    Salesdepartment_HP__c,
                    CreatedDate,
                    Is_Active__c,
                    RecordType.Name,
                    Grade__c,
                    OCM_Category__c,
                    City_Master__c,
                    City_Master__r.Name,
                    Town__c,
                    Phone
                FROM Account
                WHERE Id = :accountList
            ];
 
            List<Account> acListTemp = new List<Account>();
 
            for (String s : accountList) {
                for (Account a : acList) {
                    if (s == a.id) {
                        acListTemp.add(a);
                    }
                }
            }
 
            topInfo.acList = acListTemp;
 
            data.put('topInfo', topInfo);
            data.put('pageRecords', selectAccList);
            data.put('paginatedAccounts', paginatedAccounts);
            data.put('accountId', accountId);
            data.put('agencyProType', agencyProType);
            data.put('agencyProTypestr', agencyProTypestr);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + e.getLineNumber(), '');
        }
    }
 
    @AuraEnabled
    public static ResponseBodyLWC changeFiscalYearView(String fiscalYear) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        try {
            TopInfo topInfo = new TopInfo();
 
            Date today = Date.today();
            Integer lastYear = today.year() - 1;
            Integer thisYear = today.year();
            Integer nextYear = today.year() + 1;
            Date lastDatetime = Date.newInstance(lastYear, 4, 1);
            Date thisDatetime = Date.newInstance(thisYear, 4, 1);
            Date thisDatetime2 = Date.newInstance(thisYear, 4, 1);
            Date nextDatetime = Date.newInstance(nextYear, 4, 1);
 
            AggregateResult[] arList = null;
 
            System.debug('fiscalYear = ' + fiscalYear);
            System.debug('thisYear = ' + thisYear);
            System.debug('lastYear = ' + lastYear);
 
            if (fiscalYear == 'thisYear') {
                AggregateResult[] saleAmountList = LexConsumableAccountSOQL.getAccountTotalSales(thisDatetime, nextDatetime);
                if (saleAmountList[0].get('saleAmount') != null) {
                    topInfo.saleAmount = Decimal.valueOf(String.valueOf(saleAmountList[0].get('saleAmount')));
                    System.debug('topInfo.saleAmount = ' + topInfo.saleAmount);
                }
                arList = LexConsumableAccountSOQL.getAccountBySales(thisDatetime, nextDatetime);
            } else if (fiscalYear == 'lastYear') {
                AggregateResult[] saleAmountList = LexConsumableAccountSOQL.getAccountTotalSales(lastDatetime, thisDatetime2);
                if (saleAmountList[0].get('saleAmount') != null) {
                    topInfo.saleAmount = Decimal.valueOf(String.valueOf(saleAmountList[0].get('saleAmount')));
                    System.debug('topInfo.saleAmount = ' + topInfo.saleAmount);
                }
                arList = LexConsumableAccountSOQL.getAccountBySales(lastDatetime, thisDatetime2);
            } else {
                AggregateResult[] saleAmountList = LexConsumableAccountSOQL.getAccountTotalSales(lastDatetime, nextDatetime);
                if (saleAmountList[0].get('saleAmount') != null) {
                    topInfo.saleAmount = Decimal.valueOf(String.valueOf(saleAmountList[0].get('saleAmount')));
                    System.debug('topInfo.saleAmount = ' + topInfo.saleAmount);
                }
                arList = LexConsumableAccountSOQL.getAccountBySales(lastDatetime, nextDatetime);
            }
            //查询客户信息(名称,省,县)
            List<String> accountList = new List<String>();
            for (AggregateResult ar : arList) {
                System.debug('thisAmount = ' + String.valueOf(ar.get('thisAmount')));
                if (String.valueOf(ar.get('thisAmount')) != '0.0') {
                    accountList.add(String.valueOf(ar.get('Order_ForHospital__c')));
                    topInfo.deList.add(Decimal.valueOf(String.valueOf(ar.get('thisAmount'))));
                }
            }
            System.debug('accountList = ' + accountList);
            System.debug('topInfo = ' + topInfo);
            List<Account> acList = [
                SELECT
                    id,
                    Name,
                    State_Master__c,
                    State_Master__r.Name,
                    Salesdepartment_HP__c,
                    CreatedDate,
                    Is_Active__c,
                    RecordType.Name,
                    Grade__c,
                    OCM_Category__c,
                    City_Master__c,
                    City_Master__r.Name,
                    Town__c,
                    Phone
                FROM Account
                WHERE Id = :accountList
            ];
            List<Account> acListTemp = new List<Account>();
            for (String s : accountList) {
                for (Account a : acList) {
                    if (s == a.id) {
                        acListTemp.add(a);
                    }
                }
            }
            topInfo.acList = acListTemp;
            data.put('topInfo', topInfo);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + e.getLineNumber(), '');
        }
    }
 
    @AuraEnabled
    public static ResponseBodyLWC changelistView(
        String filterNameLwc,
        String accountIdLwc,
        String agencyProTypeLwc,
        Integer pageSizeLWC,
        Integer pageTokenLWC,
        String sortFieldLWC,
        String sortOrderLWC
    ) {
        try {
            ResponseBodyLWC res = new ResponseBodyLWC();
            Map<String, object> data = new Map<String, object>();
            res.entity = data;
 
            filterName = filterNameLwc;
            accountId = accountIdLwc;
            agencyProTypestr = agencyProTypeLwc;
            pageSize = pageSizeLWC;
            pageToken = pageTokenLWC > 2000 ? 2000 : pageTokenLWC;
            sortField = sortFieldLWC;
            sortOrder = sortOrderLWC;
 
            String soql = makeSoql(filterName, accountId);
            System.debug('soql1+++++++ ' + soql);
            List<Account> allSelectAccount = Database.query(soql);
 
            totalCount = allSelectAccount.size();
            String nullFL = sortOrder.toLowerCase() == 'asc' ? 'NULLS FIRST' : 'NULLS LAST';
            if (String.isNotBlank(sortField)) {
                soql += ' order by ' + sortField + ' ' + sortOrder + ' ' + nullFL;
            }
            soql += ' limit ' + pagesize + ' offset ' + pageToken;
            System.debug('soql2+++++++ ' + soql);
            List<Account> selectAccList = Database.query(soql);
            System.debug('selectAccList = ' + selectAccList);
 
            PaginatedAccounts paginatedAccounts = new PaginatedAccounts();
            paginatedAccounts.nextPageToken = (pageToken + pageSize < totalCount) ? pageToken + pageSize : null;
            paginatedAccounts.recordStart = pageToken + 1;
            paginatedAccounts.pageNumber = pageToken / pageSize + 1;
            Integer recordEnd = pageSize * paginatedAccounts.pageNumber;
            paginatedAccounts.recordEnd = totalCount >= recordEnd ? recordEnd : totalCount;
            paginatedAccounts.totalRecords = totalCount;
 
            data.put('pageRecords', selectAccList);
            data.put('paginatedAccounts', paginatedAccounts);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
 
    //sql文作成
    private static String makeSoql(String viewName, String accountId) {
        Date st = Date.today().addDays(-7).toStartOfWeek();
        lastweekstart = Datetime.newInstance(st.year(), st.month(), st.day(), 8, 0, 0);
        lastweekend = lastweekstart.addDays(7);
        String soql = 'SELECT id,Name,State_Master__c,Salesdepartment_HP__c,CreatedDate,Is_Active__c,RecordType.Name,State_Master__r.Name,City_Master__r.Name';
        soql += ' ,Grade__c,OCM_Category__c,City_Master__c,Town__c,Phone 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(agencyProTypestr.replaceAll('%', '\\%')) +
            '%\')';
        if (viewName == '61. 医院_Hospital草案中的医院') {
            soql += ' AND Is_Active__c = \'' + '草案中' + '\'';
        }
        if (viewName == '62. 医院_Hospital申请中的医院') {
            soql += ' AND Is_Active__c = \'' + '申请中' + '\'';
        }
        if (viewName == '63. 医院_Hospital上周创建的医院') {
            soql += ' AND Is_Active__c = \'' + '有效' + '\'';
            soql += ' AND CreatedDate >= :lastweekstart ';
            soql += ' AND CreatedDate < :lastweekend ';
        }
        System.debug('soql +++++' + soql);
        return soql;
    }
 
    //分页Bean
    public class PaginatedAccounts {
        @AuraEnabled
        public Integer nextPageToken;
        @AuraEnabled
        public Integer pageNumber { get; set; }
        @AuraEnabled
        public Integer totalRecords { get; set; }
        @AuraEnabled
        public Integer recordStart { get; set; }
        @AuraEnabled
        public Integer recordEnd { get; set; }
    }
 
    //top10 Account
    public class TopInfo {
        @AuraEnabled
        public Decimal saleAmount { get; set; }
        @AuraEnabled
        public List<Account> acList { get; set; }
        @AuraEnabled
        public List<Decimal> deList { get; set; }
 
        public TopInfo() {
            acList = new List<Account>();
            deList = new List<Decimal>();
        }
    }
}