qinchulong
2024-10-12 b17d60113095491a95557f6f785cb1c0e744e8cc
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
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;
 
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;
            }
        }
 
        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();
        }
 
        /// <summary>
        /// 添加多条数据
        /// </summary>
        /// <param name="listEntity"></param>
        /// <returns>影响行数</returns>
        public virtual int AddData(List<TEntity> listEntity)
        {
            IInsertable<TEntity> insert = _db.Insertable(listEntity);
            return insert.ExecuteReturnIdentity();
        }
 
        /// <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, string orderByFields)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).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<TResult, bool>> whereExpression)
        {
            List<TResult> list = _db.Queryable(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);
        }
 
        //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();}
    }
}