服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-11-20 a96b665105c57a4ae67d286b2bb6897988ca590e
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Rhea.Common;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
using Tiger.Model;
 
namespace Tiger.Business
{
    /// <summary>
    /// 数据库操作基础类
    /// </summary>
    public class DbBase : Rhea.Common.DbBase, IDbBase//Rhea.Common.DbBase
    {
        /// <summary>
        /// 数据库操作基础类
        /// </summary>
        /// <param name="db"></param>
        public DbBase(DbClient db) : base(db)
        {
        }
 
        #region 添加操作
 
        /// <summary>
        /// 把单个实体往数据库添加一行数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction<T>> AddAsync<T>(T entity, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction<T>();
            try
            {
                var dbres = isAsync ? await Db.Insertable<T>(entity).ExecuteCommandAsync() : Db.Insertable<T>(entity).ExecuteCommand();
                action.Message = $"Insert {typeof(T).Name} entity into database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Insert {typeof(T).Name} entity into database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 把实体List往数据库批量添加数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">实体List</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction<T>> AddListAsync<T>(List<T> list, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction<T>();
            try
            {
                var dbres = isAsync ? await Db.Insertable<T>(list).ExecuteCommandAsync() : Db.Insertable<T>(list).ExecuteCommand();
                action.Message = $"Insert {typeof(T).Name} entity list into database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Insert {typeof(T).Name} entity list into database exception.");
            }
            return action;
        }
 
        #endregion 添加操作
 
        #region 保存操作
 
        /// <summary>
        /// 把单个实体往数据库保存一行数据,如果存在就更新,不存在就插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction<T>> SaveAsync<T>(T entity, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction<T>();
            try
            {
                var dbres = isAsync ? await Db.Saveable<T>(entity).ExecuteCommandAsync() : Db.Saveable<T>(entity).ExecuteCommand();
                action.Message = $"Save {typeof(T).Name} entity into database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Save {typeof(T).Name} entity into database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 把实体List往数据库批量保存数据,如果存在就更新,不存在就插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">实体List</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction<T>> SaveListAsync<T>(List<T> list, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction<T>();
            try
            {
                var dbres = isAsync ? await Db.Saveable<T>(list).ExecuteCommandAsync() : Db.Saveable<T>(list).ExecuteCommand();
                action.Message = $"Save {typeof(T).Name} entity list into database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Save {typeof(T).Name} entity list into database exception.");
            }
            return action;
        }
 
        /// <summary>
        /// 把单个实体往数据库保存一行数据,如果存在就更新,不存在就插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public async Task<ApiAction<T>> StorageableAsync<T>(T entity, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction<T>();
            try
            {
                var dbres = isAsync ? await Db.Storageable<T>(entity).ExecuteCommandAsync() : Db.Storageable<T>(entity).ExecuteCommand();
                action.Message = $"Storageable {typeof(T).Name} entity into database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Storageable {typeof(T).Name} entity into database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 把实体List往数据库批量保存数据,如果存在就更新,不存在就插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">实体List</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public async Task<ApiAction<T>> StorageableListAsync<T>(List<T> list, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction<T>();
            try
            {
                var dbres = isAsync ? await Db.Storageable<T>(list).ExecuteCommandAsync() : Db.Storageable<T>(list).ExecuteCommand();
                action.Message = $"Storageable {typeof(T).Name} entity list into database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Storageable {typeof(T).Name} entity list into database exception.");
            }
            return action;
        }
 
        #endregion 保存操作
 
        #region 修改操作
 
        /// <summary>
        /// 从数据库中修改单个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> UpdateAsync<T>(T entity, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction();
            try
            {
                var dbres = isAsync ? await Db.Updateable<T>(entity).ExecuteCommandAsync() : Db.Updateable<T>(entity).ExecuteCommand();
                action.Message = $"Update {typeof(T).Name} entity from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Update {typeof(T).Name} entity from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据实体List在数据库批量修改数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">实体List</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> UpdateAsync<T>(List<T> list, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction();
            try
            {
                var dbres = isAsync ? await Db.Updateable<T>(list).ExecuteCommandAsync() : Db.Updateable<T>(list).ExecuteCommand();
                action.Message = $"Update {typeof(T).Name} entity list from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Update {typeof(T).Name} entity list from database exception.");
            }
            return action;
        }
 
        /// <summary>
        /// 按UpdaterAble&lt;T>对象从数据库中修改多个实体的某些字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updater">实体List</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> UpdateAsync<T>(UpdateAble<T> updater, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction();
            try
            {
                var dbres = isAsync ? await Db.Updateable<T>(updater.Items).UpdateColumns(updater.UpdateColumnList).ExecuteCommandAsync() : Db.Updateable<T>(updater.Items).UpdateColumns(updater.UpdateColumnList).ExecuteCommand();
                action.Message = $"Update some columns of {typeof(T).Name} entity list from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Update some columns of {typeof(T).Name} entity list from database exception");
            }
            return action;
        }
 
        #endregion 修改操作
 
        #region 删除操作
 
        /// <summary>
        /// 从数据库中删除多个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="primaryKeys">实体的主键ID数组</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> DeleteAsync<T>(T newEntity, string[] primaryKeys, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction(false);
            try
            {
                var dbres = isAsync ? await Db.Deleteable<T>().In(primaryKeys).ExecuteCommandAsync() : Db.Deleteable<T>().In(primaryKeys).ExecuteCommand();
                action.Message = $"Delete {typeof(T).Name} entity by primary keys from database success";
                action.IsSuccessed = true;
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Delete {typeof(T).Name} entity by primary keys from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 从数据库中删除单个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> DeleteAsync<T>(T entity, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction(false);
            try
            {
                var dbres = isAsync ? await Db.Deleteable<T>().Where(entity).ExecuteCommandAsync() : Db.Deleteable<T>().Where(entity).ExecuteCommand();
                action.Message = $"Delete {typeof(T).Name} entity from database success";
                action.IsSuccessed = true;
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Delete {typeof(T).Name} entity from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 从数据库中删除实体List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">实体List</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> DeleteAsync<T>(List<T> list, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction();
            try
            {
                var dbres = isAsync ? await Db.Deleteable<T>().Where(list).ExecuteCommandAsync() : Db.Deleteable<T>().Where(list).ExecuteCommand();
                action.Message = $"Delete {typeof(T).Name} entity list from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Delete {typeof(T).Name} entity list from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据Where语句删除满足条件的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> DeleteAsync<T>(T newEntity, string where, bool isAsync = true) where T : class, new()
        {
            var action = new ApiAction();
            try
            {
                var dbres = isAsync ? await Db.Deleteable<T>().Where(where).ExecuteCommandAsync() : Db.Deleteable<T>().Where(where).ExecuteCommand();
                action.Message = $"Delete {typeof(T).Name} entity by where from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Delete {typeof(T).Name} entity by where from database exception");
            }
            return action;
        }
 
        #endregion 删除操作
 
        #region 查询操作
 
        /// <summary>
        /// 从数据库中查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<List<T>>> QueryAsync<T>(T newEntity, bool isAsync = true, bool needInclude = false)
        {
            var action = new ApiAction<List<T>>();
            try
            {
                var queryable = Db.Queryable<T>();
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                action.Message = $"Query {typeof(T).Name} entity list from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity list from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据Where语句从数据库中查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<List<T>>> QueryAsync<T>(T newEntity, string where, bool isAsync = true, bool needInclude = false) where T : class, new()
        {
            var action = new ApiAction<List<T>>();
            try
            {
                var queryable = Db.Queryable<T>().Where(where);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                action.Message = $"Query {typeof(T).Name} entity list by where from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity list by where from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 按PageAble&lt;T>对象返回实体当前Page的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page">分页的参数实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<PageAble<T>>> QueryAsync<T>(PageAble<T> page, bool isAsync = true, bool needInclude = false)
        {
            var action = new ApiAction<PageAble<T>>();
            try
            {
                var queryable = Db.Queryable<T>();
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToPageAsync(page.pageIndex, page.pageSize) : queryable.ToPage(page.pageIndex, page.pageSize);
                action.Message = $"Query {typeof(T).Name} entity page from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity page from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 分页直接返回PageAble&lt;T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="isAsync"></param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<PageAble<T>>> QueryPageAsync<T>(PageAble<T> page, bool isAsync = true, bool needInclude = false)
        {
            var action = new ApiAction<PageAble<T>>();
            try
            {
                string where = page.sqlcmd;
                var queryable = Db.Queryable<T>().Where(where);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToPageAsync(page.pageIndex, page.pageSize) : queryable.ToPage(page.pageIndex, page.pageSize);
                action.Data.draw = page.draw;
                action.Message = $"Query {typeof(T).Name} entity page from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity page from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 按QueryAble&lt;T>对象从数据库查询满足条件的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query">QueryAble的参数实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<QueryAble<T>>> QueryAsync<T>(QueryAble<T> query, bool isAsync = true, bool needInclude = false)
        {
            var action = new ApiAction<QueryAble<T>>();
            try
            {
                var queryable = Db.Queryable<T>().WhereIF(!query.where.IsNullOrEmpty(), query.where).OrderByIF(!query.order.IsNullOrEmpty(), query.order);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                if (query.page == null)
                {
                    query.Items = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                }
                else
                {
                    query.page = isAsync ? (await queryable.ToPageAsync(query.page.pageIndex, query.page.pageSize)) : queryable.ToPage(query.page.pageIndex, query.page.pageSize);
                    query.Items = query.page.data;
                }
                action.Data = query;
                action.Message = $"Query {typeof(T).Name} entity by QueryAble from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity by QueryAble from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据SQL语句从数据库中查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="sqlcmd">SQL语句</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<List<T>>> QuerySqlAsync<T>(T newEntity, string sqlcmd, bool isAsync = true, bool needInclude = false) where T : class, new()
        {
            var action = new ApiAction<List<T>>();
            try
            {
                var queryable = Db.SqlQueryable<T>(sqlcmd);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                action.Message = $"Query {typeof(T).Name} entity list from database by sql command success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity list from database by sql command exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据SQL语句从数据库中查询并按PageAble&lt;T>对象返回当前Page的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page">分页的参数实体</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<PageAble<T>>> QuerySqlAsync<T>(PageAble<T> page, bool isAsync = true, bool needInclude = false) where T : class, new()
        {
            var action = new ApiAction<PageAble<T>>();
            try
            {
                var queryable = Db.SqlQueryable<T>(page.sqlcmd);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToPageAsync(page.pageIndex, page.pageSize) : queryable.ToPage(page.pageIndex, page.pageSize);
                action.Data.draw = page.draw;
                action.Message = $"Query {typeof(T).Name} entity page from database by sql command success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity page from database by sql command exception");
            }
            return action;
        }
 
        /// <summary>
        /// 从数据库中查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<List<T>>> QueryByAuthAsync<T>(T newEntity, AuthOption options, bool isAsync = true, bool needInclude = false) where T : iTableHasAuth, new()
        {
            var action = new ApiAction<List<T>>();
            try
            {
                var queryable = Db.Queryable<T>().ByAuth(options);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                action.Message = $"Query {typeof(T).Name} entity list from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity list from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据Where语句从数据库中查询实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<List<T>>> QueryByAuthAsync<T>(T newEntity, string where, AuthOption options, bool isAsync = true, bool needInclude = false) where T : iTableHasAuth, new()
        {
            var action = new ApiAction<List<T>>();
            try
            {
                var queryable = Db.Queryable<T>().ByAuth(options).Where(where);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                action.Message = $"Query {typeof(T).Name} entity list by where from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity list by where from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 按PageAble&lt;T>对象返回实体当前Page的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page">分页的参数实体</param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<PageAble<T>>> QueryByAuthAsync<T>(PageAble<T> page, AuthOption options, bool isAsync = true, bool needInclude = false) where T : iTableHasAuth, new()
        {
            var action = new ApiAction<PageAble<T>>();
            try
            {
                var queryable = Db.Queryable<T>().ByAuth(options);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToPageAsync(page.pageIndex, page.pageSize) : queryable.ToPage(page.pageIndex, page.pageSize);
                action.Message = $"Query {typeof(T).Name} entity page from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity page from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 分页直接返回PageAble&lt;T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<PageAble<T>>> QueryByAuthPageAsync<T>(PageAble<T> page, AuthOption options, bool isAsync = true, bool needInclude = false) where T : iTableHasAuth, new()
        {
            var action = new ApiAction<PageAble<T>>();
            try
            {
                string where = page.sqlcmd;
                var queryable = Db.Queryable<T>().ByAuth(options).Where(where);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                action.Data = isAsync ? await queryable.ToPageAsync(page.pageIndex, page.pageSize) : queryable.ToPage(page.pageIndex, page.pageSize);
                action.Data.draw = page.draw;
                action.Message = $"Query {typeof(T).Name} entity page from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity page from database exception");
            }
            return action;
        }
 
        /// <summary>
        /// 按QueryAble&lt;T>对象从数据库查询满足条件的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query">QueryAble的参数实体</param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <param name="needInclude">是否需要导航查询</param>
        /// <returns></returns>
        public async Task<ApiAction<QueryAble<T>>> QueryByAuthAsync<T>(QueryAble<T> query, AuthOption options, bool isAsync = true, bool needInclude = false) where T : iTableHasAuth, new()
        {
            var action = new ApiAction<QueryAble<T>>();
            try
            {
                var queryable = Db.Queryable<T>().ByAuth(options).WhereIF(!query.where.IsNullOrEmpty(), query.where).OrderByIF(!query.order.IsNullOrEmpty(), query.order);
                if (needInclude)
                {
                    queryable = queryable.IncludesAllFirstLayer();
                }
                if (query.page == null)
                {
                    query.Items = isAsync ? await queryable.ToListAsync() : queryable.ToList();
                }
                else
                {
                    query.page = isAsync ? (await queryable.ToPageAsync(query.page.pageIndex, query.page.pageSize)) : queryable.ToPage(query.page.pageIndex, query.page.pageSize);
                    query.Items = query.page.data;
                }
                action.Data = query;
                action.Message = $"Query {typeof(T).Name} entity by QueryAble from database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Query {typeof(T).Name} entity by QueryAble from database exception");
            }
            return action;
        }
 
        #endregion 查询操作
 
        #region 查询 Count
 
        /// <summary>
        /// 根据Where语句查询满足条件的实体的Count数量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> CountAsync<T>(T newEntity, string where, bool isAsync = true)
        {
            var action = new ApiAction();
            try
            {
                action.Data = isAsync ? await Db.Queryable<T>().Where(where).CountAsync() : Db.Queryable<T>().Where(where).Count();
                action.Message = $"Select Count[{typeof(T).Name} entity) success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Select Count[{typeof(T).Name} entity) exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据Where语句查询满足条件的实体的Count数量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public async Task<ApiAction> CountByAuthAsync<T>(T newEntity, string where, AuthOption options, bool isAsync = true) where T : iTableHasAuth, new()
        {
            var action = new ApiAction();
            try
            {
                action.Data = isAsync ? await Db.Queryable<T>().ByAuth(options).Where(where).CountAsync() : Db.Queryable<T>().ByAuth(options).Where(where).Count();
                action.Message = $"Select Count[{typeof(T).Name} entity) success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Select Count[{typeof(T).Name} entity) exception");
            }
            return action;
        }
 
        #endregion 查询 Count
 
        #region 查询 Exist
 
        /// <summary>
        /// 根据Where语句查询满足条件的Data实体是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> IsExistAsync<T>(T newEntity, string where, bool isAsync = true)
        {
            var action = new ApiAction();
            try
            {
                action.Data = isAsync ? await Db.Queryable<T>().Where(where).AnyAsync() : Db.Queryable<T>().Where(where).Any();
                action.Message = $"Check {typeof(T).Name} entity is exist success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Check {typeof(T).Name} entity is exist exception");
            }
            return action;
        }
 
        /// <summary>
        /// 根据Where语句查询满足条件的Data实体是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity">固定用法:通过ApiAction.NewDataEntity()创建ApiAction中指定类型对象</param>
        /// <param name="where">Where语句</param>
        /// <param name="options">授权查询配置项</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public async Task<ApiAction> IsExistByAuthAsync<T>(T newEntity, string where, AuthOption options, bool isAsync = true) where T : iTableHasAuth, new()
        {
            var action = new ApiAction();
            try
            {
                action.Data = isAsync ? await Db.Queryable<T>().ByAuth(options).Where(where).AnyAsync() : Db.Queryable<T>().ByAuth(options).Where(where).Any();
                action.Message = $"Check {typeof(T).Name} entity is exist success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Check {typeof(T).Name} entity is exist exception");
            }
            return action;
        }
 
        #endregion 查询 Exist
 
        #region 执行SQL
 
        /// <summary>
        /// 执行SQL语句返回影响行数
        /// </summary>
        /// <param name="sqlcmd">要执行的SQL语句</param>
        /// <param name="parameters">SQL语句的参数</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> ExecuteSqlCommandAsync(string sqlcmd, List<SugarParameter> parameters, bool isAsync = true)
        {
            var action = new ApiAction();
            try
            {
                action.Data = isAsync ? await Db.Ado.ExecuteCommandAsync(sqlcmd, parameters) : Db.Ado.ExecuteCommand(sqlcmd, parameters);
                action.Message = $"Execute command success(SQL:{sqlcmd}, Params:{Db.Utilities.SerializeObject(parameters.Select(q => new { ParameterName = q.ParameterName, Value = q.Value }))})";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Execute command success(SQL:{sqlcmd}, Params:{Db.Utilities.SerializeObject(parameters)})");
            }
            return action;
        }
 
        /// <summary>
        /// 执行SQL语句返回影响行数
        /// </summary>
        /// <param name="sqlcmd">要执行的SQL语句</param>
        /// <param name="parameters">SQL语句的参数</param>
        /// <param name="isAsync">是否以异步方式执行</param>
        /// <returns></returns>
        public new async Task<ApiAction> ExecuteSqlCommandAsync(string sqlcmd, bool isAsync = true, params SugarParameter[] parameters)
        {
            var action = new ApiAction();
            try
            {
                action.Data = isAsync ? await Db.Ado.ExecuteCommandAsync(sqlcmd, parameters) : Db.Ado.ExecuteCommand(sqlcmd, parameters);
                action.Message = $"Execute command success(SQL:{sqlcmd}, Params:{Db.Utilities.SerializeObject(parameters.ToDictionary(it => it.ParameterName, it => it.Value))})";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Execute command success(SQL:{sqlcmd}, Params:{Db.Utilities.SerializeObject(parameters.ToDictionary(it => it.ParameterName, it => it.Value))})");
            }
            return action;
        }
 
        /// <summary>
        /// 异步方式执行SQL语句返回影响行数
        /// </summary>
        /// <param name="sqlcmd">要执行的SQL语句</param>
        /// <param name="parameters">SQL语句的参数</param>
        /// <returns></returns>
        public new async Task<ApiAction> ExecuteSqlCommandAsync(string sqlcmd, params SugarParameter[] parameters)
        {
            var action = new ApiAction();
            try
            {
                action.Data = await Db.Ado.ExecuteCommandAsync(sqlcmd, parameters);
                action.Message = $"Execute command success(SQL:{sqlcmd}, Params:{Db.Utilities.SerializeObject(parameters.ToDictionary(it => it.ParameterName, it => it.Value))})";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Execute command success(SQL:{sqlcmd}, Params:{Db.Utilities.SerializeObject(parameters.ToDictionary(it => it.ParameterName, it => it.Value))})");
            }
            return action;
        }
 
        #endregion 执行SQL
 
        #region 先删除后新增
        /// <summary>
        /// 删除完更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public new async Task<ApiAction> AddAfterDeleteAsync<T>(QueryAble<T> query) where T : class, new()
        {
            var action = new ApiAction();
            try
            {
                //保存到数据库
                var db = Business.Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var dbres = !query.where.IsNullOrEmpty() ? db.Deleteable<T>().Where(query.where).ExecuteCommand() : db.Deleteable<T>().ExecuteCommand();
                    if (query.Items.Count > 0)
                    {
                        db.Insertable<T>(query.Items).ExecuteCommand();
                    }
                });
                if (!dbTran.IsSuccess)
                {
                    action.GetResponse().CatchExceptionWithLog(dbTran.ErrorException, $"作业保存到数据库异常");
                }
                action.Message = $"Delete {typeof(T).Name} entity by where and add list in database success";
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"Delete {typeof(T).Name} entity by where and add list in database exception");
            }
            return action;
        }
 
        #endregion
 
    }//endClass
}