-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseService.cs
162 lines (157 loc) · 6.43 KB
/
BaseService.cs
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
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.SignalR;
using SqlSugar;
using System.Linq.Expressions;
using PageModel = SqlSugar.PageModel;
namespace SqlSugar2
{
public class BaseService<T> where T : class, new()
{
private readonly IServiceProvider _serviceProvider;
private IDBContext _dbContext { get; }
private string _dataSource { get; }
public BaseService(IServiceProvider serviceProvider)
{
//IDBContext
//_dbContext = AutofacHelper.Resolve<IDBContext>();
_dbContext = serviceProvider.GetService<IDBContext>();
//检查是否有配置多数据源,没有则用默认数据源
var dsAttr = GetType().GetCustomAttributes(typeof(DataSourceAttribute), false);
if (dsAttr != null && dsAttr.Length > 0)
{
var dataSource = dsAttr.First() as DataSourceAttribute;
_dataSource = dataSource.Name;
}
//打印执行sql
//DBClient.Aop.OnLogExecuting = (sql, pars) =>
//{
// Console.WriteLine("<------------------ sqlSugar start ------------------>");
// Console.WriteLine(sql);
// Console.WriteLine("<------------------ sqlSugar end ------------------>");
//};
}
private ISqlSugarClient _dbClient;
/// <summary>
/// 当前连接
/// </summary>
protected ISqlSugarClient DBClient
{
get => _dbContext.GetClient(_dataSource);
}
/// <summary>
/// SimpleClient
/// </summary>
protected SimpleClient<T> SimpleClient
{
get => DBClient.GetSimpleClient<T>();
}
public T GetById(dynamic id)
{
return DBClient.Queryable<T>().InSingle(id);
}
public int Count(Expression<Func<T, bool>> whereExpression)
{
return DBClient.Queryable<T>().Where(whereExpression).Count();
}
public List<T> GetList()
{
return DBClient.Queryable<T>().ToList();
}
public T GetFirst(Expression<Func<T, bool>> whereExpression)
{
return DBClient.Queryable<T>().First(whereExpression);
}
public T GetSingle(Expression<Func<T, bool>> whereExpression)
{
return DBClient.Queryable<T>().Single(whereExpression);
}
public List<T> GetList(Expression<Func<T, bool>> whereExpression)
{
return DBClient.Queryable<T>().Where(whereExpression).ToList();
}
public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
{
int count = 0;
var result = DBClient.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
page.TotalCount = count;
return result;
}
public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
{
int count = 0;
var result = DBClient.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
page.TotalCount = count;
return result;
}
public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
{
int count = 0;
var result = DBClient.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
page.TotalCount = count;
return result;
}
public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
{
int count = 0;
var result = DBClient.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
page.TotalCount = count;
return result;
}
public bool IsAny(Expression<Func<T, bool>> whereExpression)
{
return DBClient.Queryable<T>().Where(whereExpression).Any();
}
public bool Insert(T insertObj)
{
return DBClient.Insertable(insertObj).ExecuteCommand() > 0;
}
public int InsertReturnIdentity(T insertObj)
{
return DBClient.Insertable(insertObj).ExecuteReturnIdentity();
}
public long InsertReturnBigIdentity(T insertObj)
{
return DBClient.Insertable(insertObj).ExecuteReturnBigIdentity();
}
public bool InsertRange(T[] insertObjs)
{
return DBClient.Insertable(insertObjs).ExecuteCommand() > 0;
}
public bool InsertRange(List<T> insertObjs)
{
return DBClient.Insertable(insertObjs).ExecuteCommand() > 0;
}
public bool Update(T updateObj)
{
return DBClient.Updateable(updateObj).ExecuteCommand() > 0;
}
public bool UpdateRange(T[] updateObjs)
{
return DBClient.Updateable(updateObjs).ExecuteCommand() > 0;
}
public bool UpdateRange(List<T> updateObjs)
{
return DBClient.Updateable(updateObjs).ExecuteCommand() > 0;
}
public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
{
return DBClient.Updateable(columns).Where(whereExpression).ExecuteCommand() > 0;
}
public bool Delete(T deleteObj)
{
return DBClient.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
}
public bool Delete(Expression<Func<T, bool>> whereExpression)
{
return DBClient.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
}
public bool DeleteById(dynamic id)
{
return DBClient.Deleteable<T>().In(id).ExecuteCommand() > 0;
}
public bool DeleteByIds(dynamic[] ids)
{
return DBClient.Deleteable<T>().In(ids).ExecuteCommand() > 0;
}
}
}