heshaofeng
2025-12-04 1e8ca092e08494e78c398c6262aff94ac5a5bb22
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using WIDESEA_Core.Helper;
using Microsoft.Data.SqlClient;
using System.Drawing.Printing;
using WIDESEA_Core.Tenants;
using WIDESEA_Core.Seed;
using WIDESEA_Core.DB;
using WIDESEA_Core.Const;
using WIDESEA_Core.AOP;
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Utilities;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NetTaste;
using WIDESEA_Core.DB.Models;
 
namespace WIDESEA_Core.BaseRepository
{
    public class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class, new()
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly SqlSugarClient _dbBase;
 
        private ISqlSugarClient _db
        {
            get
            {
                ISqlSugarClient db = _dbBase;
 
                //多租户
                //var mta = typeof(TEntity).GetCustomAttribute<MultiTenantAttribute>();
                //if (mta is { TenantType: TenantTypeEnum.Db })
                //{
                //    //获取租户信息 租户信息可以提前缓存下来 
                //    if (App.User is { TenantId: > 0 })
                //    {
                //        dynamic tenant = db.Queryable(MainDb.TenantTableName, "x").Where(MainDb.TenantId, "=", App.User.TenantId).First();
                //        if (tenant != null)
                //        {
                //            var iTenant = db.AsTenant();
                //            if (!iTenant.IsAnyConnection(tenant.TenantId))
                //            {
                //                string conStr = tenant.ConnectionString;
                //                ConnectionConfig connectionConfig = new ConnectionConfig()
                //                {
                //                    ConfigId = tenant.TenantId,
                //                    ConnectionString = conStr.DecryptDES(AppSecret.DB),
                //                    DbType = (SqlSugar.DbType)tenant.DbType,
                //                    IsAutoCloseConnection = true,
                //                    AopEvents = new AopEvents()
                //                    {
                //                        DataExecuting = SqlSugarAop.DataExecuting,
 
                //                    }
                //                };
                //                iTenant.AddConnection(connectionConfig);
                //            }
 
                //            return iTenant.GetConnection(tenant.TenantId);
                //        }
                //    }
                //}
 
                return db;
            }
        }
        /// <summary>
        /// 创建数据库连接对象
        /// </summary>
        public ISqlSugarClient Db => _db;
 
        public RepositoryBase(IUnitOfWorkManage unitOfWorkManage)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _dbBase = unitOfWorkManage.GetDbClient();
        }
 
        /// <summary>
        /// 通过主键查询数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>查询结果</returns>
        public virtual TEntity QureyDataById(object id)
        {
            return _db.Queryable<TEntity>().In(id).Single();
        }
 
        /// <summary>
        /// 通过主键数组查询数据
        /// </summary>
        /// <param name="lstIds">主键数组</param>
        /// <returns>查询结果集合</returns>
        public virtual List<TEntity> QureyDataByIds(object[] lstIds)
        {
            return _db.Queryable<TEntity>().In(lstIds).ToList();
        }
 
        /// <summary>
        /// 通过主键集合查询数据
        /// </summary>
        /// <param name="lstIds">主键集合</param>
        /// <returns>查询结果集合</returns>
        public virtual List<TEntity> QureyDataByIds(List<object> lstIds)
        {
            return _db.Queryable<TEntity>().In(lstIds).ToList();
        }
 
        /// <summary>
        /// 添加单条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>影响行数</returns>
        public virtual int AddData(TEntity entity)
        {
            IInsertable<TEntity> insert = _db.Insertable(entity);
            return insert.ExecuteReturnIdentity();
        }
 
        public virtual bool AddData<TChild>(TEntity entity, Expression<Func<TEntity, List<TChild>>> expression) where TChild : class, new()
        {
            return _db.InsertNav(entity).Include(expression).ExecuteCommand();
        }
 
        /// <summary>
        /// 添加多条数据
        /// </summary>
        /// <param name="listEntity"></param>
        /// <returns>影响行数</returns>
        public virtual int AddData(List<TEntity> listEntity)
        {
            IInsertable<TEntity> insert = _db.Insertable(listEntity);
            return insert.ExecuteCommand();
        }
 
        /// <summary>
        /// 通过主键删除数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>删除结果</returns>
        public virtual bool DeleteDataById(object id)
        {
            return _db.Deleteable<TEntity>().In(id).ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 通过主键数据删除多条数据
        /// </summary>
        /// <param name="ids">主键数组</param>
        /// <returns>删除结果</returns>
        public virtual bool DeleteDataByIds(object[] ids)
        {
            return _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 通过实体数据删除数据
        /// </summary>
        /// <param name="ids">主键数组</param>
        /// <returns>删除结果</returns>
        public virtual bool DeleteData(TEntity entity)
        {
            return _db.Deleteable(entity).ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 通过实体集合数据删除数据
        /// </summary>
        /// <param name="ids">主键数组</param>
        /// <returns>删除结果</returns>
        public virtual bool DeleteData(List<TEntity> listEntity)
        {
            return _db.Deleteable(listEntity).ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 更新单条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool UpdateData(TEntity entity)
        {
            return _db.Updateable(entity).ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 更新多条数据
        /// </summary>
        /// <param name="listEntity"></param>
        /// <returns></returns>
        public virtual bool UpdateData(List<TEntity> listEntity)
        {
            return _db.Updateable(listEntity).ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 指定列更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="lstColumns"></param>
        /// <param name="lstIgnoreColumns"></param>
        /// <returns></returns>
        public virtual bool UpdateData(TEntity entity, List<string> lstColumns, List<string>? lstIgnoreColumns = null)
        {
            IUpdateable<TEntity> update = _db.Updateable(entity);
 
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                update = update.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
 
            if (lstColumns != null && lstColumns.Count > 0)
            {
                update = update.UpdateColumns(lstColumns.ToArray());
            }
 
            return update.ExecuteCommandHasChange();
        }
 
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public virtual List<TEntity> QueryData()
        {
            return _db.Queryable<TEntity>().ToList();
        }
 
        /// <summary>
        /// 条件查询数据
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(string where)
        {
            return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).ToList();
        }
 
        /// <summary>
        /// 条件查询数据
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList();
        }
 
        public virtual TEntity QueryFirst(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).First();
        }
 
        public virtual TResult QueryFirst<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Select(expression).First();
        }
 
        /// <summary>
        /// 条件查询数据并排序
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).ToList();
        }
 
        /// <summary>
        /// 条件查询数据并排序
        /// </summary>
        /// <param name="where"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(string where, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToList();
        }
 
        /// <summary>
        /// 查询指定数据对象
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual List<TResult> QueryData<TResult>(Expression<Func<TEntity, TResult>> expression)
        {
            return _db.Queryable<TEntity>().Select(expression).ToList();
        }
 
        /// <summary>
        /// 条件查询指定数据对象
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <param name="whereExpression"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TResult> QueryData<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields = "")
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList();
        }
 
        /// <summary>
        /// 条件查询数据并排序
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList();
        }
 
        /// <summary>
        /// 条件查询数据并排序
        /// </summary>
        /// <param name="where"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(string where, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).ToList();
        }
 
        /// <summary>
        /// 原生Sql语句查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryDataBySql(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.SqlQuery<TEntity>(sql, parameters);
        }
 
        /// <summary>
        /// 原生Sql语句查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual List<dynamic> QueryDynamicDataBySql(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.SqlQuery<dynamic>(sql, parameters);
        }
 
        /// <summary>
        /// 原生Sql语句查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual List<object> QueryObjectDataBySql(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.SqlQuery<object>(sql, parameters);
        }
 
        /// <summary>
        /// 原生Sql语句执行操作
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public virtual int ExecuteSqlCommand(string sql, params SqlParameter[] sqlParameters)
        {
            return _db.Ado.ExecuteCommand(sql, sqlParameters);
        }
 
        /// <summary>
        /// 原生Sql语句查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual DataTable QueryTable(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.GetDataTable(sql, parameters);
        }
 
        /// <summary>
        /// 条件查询数据指定数量的行
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="top"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Take(top).ToList();
        }
 
        /// <summary>
        /// 条件查询指定数量的行
        /// </summary>
        /// <param name="where"></param>
        /// <param name="top"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(string where, int top, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).Take(top).ToList();
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
               .WhereIF(whereExpression != null, whereExpression).ToPageList(pageIndex, pageSize);
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual List<TEntity> QueryData(string where, int pageIndex, int pageSize, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
                .WhereIF(!string.IsNullOrEmpty(where), where).ToPageList(pageIndex, pageSize);
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual PageGridData<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string? orderByFields = null)
        {
            int totalCount = 0;
            var list = _db.Queryable<TEntity>()
                .OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
                .WhereIF(whereExpression != null, whereExpression)
                .ToPageList(pageIndex, pageSize, ref totalCount);
 
            return new PageGridData<TEntity> { Rows = list, Total = totalCount };
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public virtual PageGridData<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            int totalCount = 0;
            List<TEntity> list = _db.Queryable<TEntity>()
                .OrderBy(orderByModels)
                .WhereIF(whereExpression != null, whereExpression)
                .ToPageList(pageIndex, pageSize, ref totalCount);
 
            return new PageGridData<TEntity>(totalCount, list);
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual PageGridData<TEntity> QueryPage(string where, int pageIndex, int pageSize, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            int totalCount = 0;
            List<TEntity> list = _db.Queryable<TEntity>()
                .WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToPageList(pageIndex, pageSize, ref totalCount);
 
            return new PageGridData<TEntity>(totalCount, list);
        }
 
        /// <summary>
        /// 两表联查
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public virtual List<TResult> QueryTabs<T, T2, TResult>(
           Expression<Func<T, T2, object[]>> joinExpression,
           Expression<Func<T, T2, TResult>> selectExpression,
           Expression<Func<T, T2, bool>> whereExpressionT1,
           Expression<Func<TResult, bool>> whereExpression)
        {
            List<TResult> list = _db.Queryable(joinExpression).WhereIF(whereExpressionT1 != null, whereExpressionT1)
               .Select(selectExpression)
               .WhereIF(whereExpression != null, whereExpression).ToList();
            return list;
        }
 
        public virtual List<TResult> QueryTabs<T, T2, TResult>(
          Expression<Func<T, T2, bool>> joinExpression,
          Expression<Func<T, T2, TResult>> selectExpression,
          Expression<Func<T, bool>> whereExpressionT1,
          Expression<Func<T2, bool>> whereExpressionT2,
          Expression<Func<TResult, bool>> whereExpression)
        {
            List<TResult> list = _db.Queryable<T>().WhereIF(whereExpressionT1 != null, whereExpressionT1).InnerJoin(_db.Queryable<T2>().WhereIF(whereExpressionT2 != null, whereExpressionT2), joinExpression).Select(selectExpression)
               .WhereIF(whereExpression != null, whereExpression).ToList();
            return list;
        }
 
        /// <summary>
        /// 两表联查-分页
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, int pageIndex, int pageSize, string? orderByFields = null)
        {
            int totalCount = 0;
            List<TResult> list = _db.Queryable(joinExpression)
                .Select(selectExpression)
                .OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
                .WhereIF(whereExpression != null, whereExpression)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            return new PageGridData<TResult>(totalCount, list);
        }
 
        /// <summary>
        /// 两表联合查询-分页-分组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="joinExpression"></param>
        /// <param name="selectExpression"></param>
        /// <param name="whereExpression"></param>
        /// <param name="groupExpression"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderByFields"></param>
        /// <returns></returns>
        public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, Expression<Func<T1, object>> groupExpression, int pageIndex, int pageSize, string? orderByFields = null)
        {
            int totalCount = 0;
            List<TResult> list = _db.Queryable(joinExpression).GroupBy(groupExpression)
                .Select(selectExpression)
                .OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
                .WhereIF(whereExpression != null, whereExpression)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            return new PageGridData<TResult>(totalCount, list);
        }
 
        public Task<TEntity> QureyDataByIdAsync(object id)
        {
            return _db.Queryable<TEntity>().In(id).SingleAsync();
        }
 
        public Task<List<TEntity>> QureyDataByIdsAsync(object[] lstIds)
        {
            return _db.Queryable<TEntity>().In(lstIds).ToListAsync();
        }
 
        public Task<List<TEntity>> QureyDataByIdsAsync(List<object> lstIds)
        {
            return _db.Queryable<TEntity>().In(lstIds).ToListAsync();
        }
 
        public Task<int> AddDataAsync(TEntity entity)
        {
            IInsertable<TEntity> insert = _db.Insertable(entity);
            return insert.ExecuteReturnIdentityAsync();
        }
 
        public Task<int> AddDataAsync(List<TEntity> listEntity)
        {
            IInsertable<TEntity> insert = _db.Insertable(listEntity);
            return insert.ExecuteReturnIdentityAsync();
        }
 
        public Task<bool> DeleteDataByIdAsync(object id)
        {
            return _db.Deleteable<TEntity>().In(id).ExecuteCommandHasChangeAsync();
        }
 
        public Task<bool> DeleteDataByIdsAsync(object[] ids)
        {
            return _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync();
        }
 
        public Task<bool> DeleteDataAsync(TEntity entity)
        {
            return _db.Deleteable(entity).ExecuteCommandHasChangeAsync();
        }
 
        public Task<bool> DeleteDataAsync(List<TEntity> listEntity)
        {
            return _db.Deleteable(listEntity).ExecuteCommandHasChangeAsync();
        }
 
        public Task<bool> UpdateDataAsync(TEntity entity)
        {
            return _db.Updateable(entity).ExecuteCommandHasChangeAsync();
        }
 
        public Task<bool> UpdateDataAsync(List<TEntity> listEntity)
        {
            return _db.Updateable(listEntity).ExecuteCommandHasChangeAsync();
        }
 
        public Task<bool> UpdateDataAsync(TEntity entity, List<string> lstColumns, List<string>? lstIgnoreColumns = null)
        {
            IUpdateable<TEntity> update = _db.Updateable(entity);
 
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                update = update.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
 
            if (lstColumns != null && lstColumns.Count > 0)
            {
                update = update.UpdateColumns(lstColumns.ToArray());
            }
 
            return update.ExecuteCommandHasChangeAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync()
        {
            return _db.Queryable<TEntity>().ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(string where)
        {
            return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }
 
        public Task<TEntity> QueryFirstAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).FirstAsync();
        }
 
        public Task<TResult> QueryFirstAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Select(expression).FirstAsync();
        }
 
        public TResult QueryFirst<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).Select(expression).First();
        }
 
        public Task<TResult> QueryFirstAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).Select(expression).FirstAsync();
        }
 
        public TEntity QueryFirst(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).First();
        }
 
        public Task<TEntity> QueryFirstAsync(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).FirstAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, string orderByFields)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(string where, Dictionary<string, OrderByType> orderBy)
        {
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderBy)
            {
                OrderByModel orderByModel = new OrderByModel()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToListAsync();
        }
 
        public Task<List<TResult>> QueryDataAsync<TResult>(Expression<Func<TEntity, TResult>> expression)
        {
            return _db.Queryable<TEntity>().Select(expression).ToListAsync();
        }
 
        public Task<List<TResult>> QueryDataAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(string where, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataBySqlAsync(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.SqlQueryAsync<TEntity>(sql, parameters);
        }
 
        public Task<List<dynamic>> QueryDynamicDataBySqlAsync(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.SqlQueryAsync<dynamic>(sql, parameters);
        }
 
        public Task<List<object>> QueryObjectDataBySqlAsync(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.SqlQueryAsync<object>(sql, parameters);
        }
 
        public Task<int> ExecuteSqlCommandAsync(string sql, params SqlParameter[] sqlParameters)
        {
            return _db.Ado.ExecuteCommandAsync(sql, sqlParameters);
        }
 
        public Task<DataTable> QueryTableAsync(string sql, SugarParameter[]? parameters = null)
        {
            return _db.Ado.GetDataTableAsync(sql, parameters);
        }
 
        public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Take(top).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(string where, int top, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).Take(top).ToListAsync();
        }
 
        public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
              .WhereIF(whereExpression != null, whereExpression).ToPageListAsync(pageIndex, pageSize);
        }
 
        public Task<List<TEntity>> QueryDataAsync(string where, int pageIndex, int pageSize, string orderByFields)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
                .WhereIF(!string.IsNullOrEmpty(where), where).ToPageListAsync(pageIndex, pageSize);
        }
 
        public Task<List<TResult>> QueryTabsAsync<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression)
        {
            return _db.Queryable(joinExpression)
             .Select(selectExpression)
             .WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }
 
        //public bool DeleteAndMoveIntoHty(TEntity entity, OperateTypeEnum operateType)
        //{
        //    Type type = entity.GetType();
        //    Assembly assembly = type.Assembly;
        //    Type? htyType = assembly.GetType(type.FullName + "_Hty");
        //    if (htyType != null)
        //    {
        //        object? obj = Activator.CreateInstance(htyType);
        //        PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
        //        PropertyInfo? operateTypePro = htyType.GetProperty(nameof(IBaseHistoryEntity.OperateType));
        //        PropertyInfo? sourceIdPro = htyType.GetProperty(nameof(IBaseHistoryEntity.SourceId));
        //        if (obj != null && keyPro != null && operateTypePro != null && sourceIdPro != null)
        //        {
        //            operateTypePro.SetValue(obj, operateType.ToString());
        //            sourceIdPro.SetValue(obj, keyPro.GetValue(entity));
 
        //            List<PropertyInfo> propertyInfos = htyType.GetProperties().Where(x => x.Name != operateTypePro.Name && x.Name != sourceIdPro.Name && x.Name != keyPro.Name).ToList();
 
        //            for (int i = 0; i < propertyInfos.Count; i++)
        //            {
        //                PropertyInfo propertyInfo = propertyInfos[i];
        //                PropertyInfo? property = type.GetProperty(propertyInfo.Name);
 
        //                if (property != null)
        //                {
        //                    if (propertyInfo.Name == nameof(BaseEntity.Modifier))
        //                    {
        //                        propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : "System");
        //                    }
        //                    else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate))
        //                    {
        //                        propertyInfo.SetValue(obj, DateTime.Now);
        //                    }                            
        //                    else
        //                    {
        //                        propertyInfo.SetValue(obj, property.GetValue(entity));
        //                    }
        //                }
        //            }
        //            if (obj != null)
        //                _db.InsertableByObject(obj).AS(type.Name + "_Hty").ExecuteCommand();
        //        }
        //    }
        //    return DeleteData(entity);
 
        //}
 
        public bool DeleteAndMoveIntoHty(TEntity entity, OperateTypeEnum operateType)
        {
            // 核心逻辑:用事务保证原子性,异常捕获避免流程中断,日志辅助排查
            bool isSuccess = false;
            string entityTypeName = entity?.GetType().Name ?? "未知实体";
 
            try
            {
                // 前置校验:实体不能为空
                if (entity == null)
                {
                    return false;
                }
 
                Type entityType = entity.GetType();
                Assembly assembly = entityType.Assembly;
                string htyTypeName = $"{entityType.FullName}_Hty";
                Type? htyType = assembly.GetType(htyTypeName);
 
                // 1. 检查历史表类型是否存在
                if (htyType == null)
                {
                 
                    return false;
                }
 
                // 2. 创建历史表实例(处理无参构造函数不存在的情况)
                object? htyObj;
                try
                {
                    htyObj = Activator.CreateInstance(htyType);
                }
                catch (Exception ex)
                {
                    // _logger.LogError(ex, "DeleteAndMoveIntoHty:创建历史表实例 {HtyTypeName} 失败", htyTypeName);
                    return false;
                }
                if (htyObj == null)
                {
                    // _logger.LogWarning("DeleteAndMoveIntoHty:历史表实例 {HtyTypeName} 创建结果为null", htyTypeName);
                    return false;
                }
 
                // 3. 获取核心属性(指定BindingFlags确保获取公共实例属性)
                BindingFlags propFlags = BindingFlags.Public | BindingFlags.Instance;
                PropertyInfo? keyPro = typeof(TEntity).GetKeyProperty(); // 自定义方法需确保返回非空,此处增加判空
                PropertyInfo? operateTypePro = htyType.GetProperty(nameof(IBaseHistoryEntity.OperateType), propFlags);
                PropertyInfo? sourceIdPro = htyType.GetProperty(nameof(IBaseHistoryEntity.SourceId), propFlags);
 
                // 校验核心属性是否存在
                if (keyPro == null)
                {
                    //_logger.LogError("DeleteAndMoveIntoHty:实体 {EntityType} 未找到主键属性", entityType.FullName);
                    return false;
                }
                if (operateTypePro == null)
                {
                    //_logger.LogError("DeleteAndMoveIntoHty:历史表 {HtyTypeName} 未找到OperateType属性", htyTypeName);
                    return false;
                }
                if (sourceIdPro == null)
                {
                    // _logger.LogError("DeleteAndMoveIntoHty:历史表 {HtyTypeName} 未找到SourceId属性", htyTypeName);
                    return false;
                }
 
                // 4. 赋值核心属性(校验类型匹配,避免SetValue抛异常)
                try
                {
                    // 处理OperateType类型匹配:若历史表属性是枚举类型,直接传枚举而非字符串
                    if (operateTypePro.PropertyType == typeof(OperateTypeEnum))
                    {
                        operateTypePro.SetValue(htyObj, operateType);
                    }
                    else if (operateTypePro.PropertyType == typeof(string))
                    {
                        operateTypePro.SetValue(htyObj, operateType.ToString());
                    }
                    else
                    {
                        //_logger.LogError("DeleteAndMoveIntoHty:历史表 {HtyTypeName} 的OperateType属性类型 {PropType} 不匹配(仅支持枚举/字符串)", htyTypeName, operateTypePro.PropertyType.FullName);
                        return false;
                    }
 
                    // 赋值SourceId(校验类型匹配)
                    object sourceIdValue = keyPro.GetValue(entity)!;
                    if (sourceIdPro.PropertyType != sourceIdValue.GetType())
                    {
                        sourceIdValue = Convert.ChangeType(sourceIdValue, sourceIdPro.PropertyType); // 类型转换
                    }
                    sourceIdPro.SetValue(htyObj, sourceIdValue);
                }
                catch (Exception ex)
                {
                    // _logger.LogError(ex, "DeleteAndMoveIntoHty:历史表 {HtyTypeName} 核心属性赋值失败", htyTypeName);
                    return false;
                }
 
                // 5. 赋值其他属性(排除核心属性)
                List<PropertyInfo> htyProperties = htyType.GetProperties(propFlags)
                    .Where(x => x.Name != operateTypePro.Name
                             && x.Name != sourceIdPro.Name
                             && x.Name != keyPro.Name)
                    .ToList();
 
                foreach (PropertyInfo htyProp in htyProperties)
                {
                    PropertyInfo? entityProp = entityType.GetProperty(htyProp.Name, propFlags);
                    if (entityProp == null) continue; // 实体无该属性则跳过
 
                    try
                    {
                        object propValue;
                        // 处理修改人:避免App.User空引用
                        if (htyProp.Name == nameof(BaseEntity.Modifier))
                        {
                            propValue = App.User?.UserId > 0 ? App.User?.UserName : "System";
                        }
                        // 处理修改时间
                        else if (htyProp.Name == nameof(BaseEntity.ModifyDate))
                        {
                            propValue = DateTime.Now;
                        }
                        // 其他属性从原实体取值
                        else
                        {
                            propValue = entityProp.GetValue(entity) ?? DBNull.Value; // 处理null值
                        }
 
                        // 类型转换后赋值(避免类型不匹配)
                        if (propValue != DBNull.Value && propValue != null)
                        {
                            propValue = Convert.ChangeType(propValue, htyProp.PropertyType);
                        }
                        htyProp.SetValue(htyObj, propValue);
                    }
                    catch (Exception ex)
                    {
                        // _logger.LogWarning(ex, "DeleteAndMoveIntoHty:历史表 {HtyTypeName} 属性 {PropName} 赋值失败,跳过该属性", htyTypeName, htyProp.Name);
                    }
                }
                try
                {                   
                    // 执行插入历史表
                    int insertRows = _db.InsertableByObject(htyObj).AS(entityType.Name + "_Hty").ExecuteCommand();
 
                    if (insertRows <= 0)
                    {
                        // _logger.LogError("DeleteAndMoveIntoHty:历史表 {HtyTypeName} 插入失败(影响行数0)", htyTypeName);
                        _db.InsertableByObject(htyObj).AS(entityType.Name + "_Hty").ExecuteCommand();
                        return false;
                    }
                    // 插入成功后执行删除
                    bool deleteSuccess = DeleteData(entity);
                    if (!deleteSuccess)
                    {
                        //_logger.LogError("DeleteAndMoveIntoHty:实体 {EntityType} 删除失败", entityType.FullName);
                        DeleteData(entity);
                        return false;
                    }
                    // 提交事务
                 
                    isSuccess = true;
                    //_logger.LogInformation("DeleteAndMoveIntoHty:实体 {EntityType} 已成功移入历史表并删除原数据", entityType.FullName);
                }
                catch (Exception ex)
                {
                   
                    // _logger.LogError(ex, "DeleteAndMoveIntoHty:事务执行失败(插入历史表/删除原数据)", entityType.FullName);
                    return false;
                }         
            }
            catch (Exception ex)
            {
                //  _logger.LogError(ex, "DeleteAndMoveIntoHty:处理实体 {EntityTypeName} 时发生未捕获异常", entityTypeName);
                return false;
            }
 
            return isSuccess;
        }
 
 
 
public bool DeleteAndMoveIntoHty(List<TEntity> entities, OperateTypeEnum operateType)
{
    Type type = typeof(TEntity);
    Assembly assembly = type.Assembly;
    Type? htyType = assembly.GetType(type.FullName + "_Hty");
    if (htyType != null)
    {
        object? obj2 = Activator.CreateInstance(htyType);
        PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
        PropertyInfo? operateTypePro = htyType.GetProperty(nameof(IBaseHistoryEntity.OperateType));
        PropertyInfo? sourceIdPro = htyType.GetProperty(nameof(IBaseHistoryEntity.SourceId));
        if (obj2 != null && keyPro != null && operateTypePro != null && sourceIdPro != null)
        {
            List<PropertyInfo> propertyInfos = htyType.GetProperties().Where(x => x.Name != operateTypePro.Name && x.Name != sourceIdPro.Name && x.Name != keyPro.Name).ToList();
            List<object> list = new List<object>();
            foreach (var item in entities)
            {
                object? obj = Activator.CreateInstance(htyType);
                if (obj != null)
                {
                    operateTypePro.SetValue(obj, operateType.ToString());
                    sourceIdPro.SetValue(obj, keyPro.GetValue(item));
                    for (int i = 0; i < propertyInfos.Count; i++)
                    {
                        PropertyInfo propertyInfo = propertyInfos[i];
                        PropertyInfo? property = type.GetProperty(propertyInfo.Name);
 
                        if (property != null)
                        {
                            if (propertyInfo.Name == nameof(BaseEntity.Modifier))
                            {
                                propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : "System");
                            }
                            else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate))
                            {
                                propertyInfo.SetValue(obj, DateTime.Now);
                            }
                            else
                            {
                                propertyInfo.SetValue(obj, property.GetValue(item));
                            }
                        }
                    }
                    list.Add(obj);
                }
            }
            if (list.Count > 0)
                _db.InsertableByObject(list).AS(type.Name + "_Hty").ExecuteCommand();
 
        }
    }
    return DeleteData(entities);
}
        //List<TResult> QueryMuch<T, T2, T3, TResult>(
        //    Expression<Func<T, T2, T3, object[]>> joinExpression,
        //    Expression<Func<T, T2, T3, TResult>> selectExpression,
        //    Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new(){throw new NotImplementedException();}
        //Task<PageModel<TEntity>> QueryPage(PaginationModel pagination){throw new NotImplementedException();}
    }
}