UnitWork.cs
6.58 KB
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
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Linq.Expressions;
using Z.EntityFramework.Plus;
namespace WebRepository
{
public partial class UnitWork : IUnitWork
{
private BaseDBContext _context;
public UnitWork(BaseDBContext context)
{
_context = context;
}
public BaseDBContext GetDbContext()
{
return _context;
}
/// <summary>
/// 根据过滤条件,获取记录
/// </summary>
/// <param name="exp">The exp.</param>
public IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T : class
{
return Filter(exp);
}
public bool IsExist<T>(Expression<Func<T, bool>> exp) where T : class
{
return _context.Set<T>().Any(exp);
}
/// <summary>
/// 查找单个
/// </summary>
public T FindSingle<T>(Expression<Func<T, bool>> exp) where T : class
{
return _context.Set<T>().AsNoTracking().FirstOrDefault(exp);
}
/// <summary>
/// 得到分页记录
/// </summary>
/// <param name="pageindex">The pageindex.</param>
/// <param name="pagesize">The pagesize.</param>
/// <param name="orderby">排序,格式如:"Id"/"Id descending"</param>
/// <param name="exp">表达式</param>
public IQueryable<T> Find<T>(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null) where T : class
{
if (pageindex < 1) pageindex = 1;
if (string.IsNullOrEmpty(orderby))
orderby = "1 asc";
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
}
/// <summary>
/// 根据过滤条件获取记录数
/// </summary>
public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class
{
return Filter(exp).Count();
}
public void Add<T>(T entity) where T : class
{
_context.Set<T>().Add(entity);
Save();
//_context.Entry(entity).State = EntityState.Detached;
}
/// <summary>
/// 批量添加
/// </summary>
/// <param name="entities">The entities.</param>
public void BatchAdd<T>(T[] entities) where T : class
{
_context.Set<T>().AddRange(entities);
Save();
}
public void Update<T>(T entity) where T : class
{
var entry = this._context.Entry(entity);
entry.State = EntityState.Modified;
//如果数据没有发生变化
if (!this._context.ChangeTracker.HasChanges())
{
entry.State = EntityState.Unchanged;
}
Save();
_context.Entry(entity).State = EntityState.Detached;
}
public void Delete<T>(T entity) where T : class
{
_context.Set<T>().Remove(entity);
Save();
}
public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class
{
System.Collections.Generic.List<T> entitys = _context.Set<T>().Where(exp).ToList();
foreach (var item in entitys)
{
Delete(item);
}
}
/// <summary>
/// 实现按需要只更新部分更新
/// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
/// </summary>
/// <param name="where">The where.</param>
/// <param name="entity">The entity.</param>
public int Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T : class
{
int count =_context.Set<T>().Where(where).Update(entity);
Save();
return count;
}
public void Save()
{
try
{
_context.SaveChanges();
}
catch (Exception ex)
{
string sMessage = ex.Message;
if (ex.InnerException != null)
{
sMessage = ex.InnerException.Message;
if (ex.InnerException.InnerException != null)
{
sMessage = ex.InnerException.InnerException.Message;
}
}
throw new Exception(sMessage);
}
}
private IQueryable<T> Filter<T>(Expression<Func<T, bool>> exp) where T : class
{
var dbSet = _context.Set<T>().AsNoTracking().AsQueryable();
if (exp != null)
dbSet = dbSet.Where(exp);
return dbSet;
}
public int ExecuteSql(string sql)
{
try
{
return _context.Database.ExecuteSqlCommand(sql);
}
catch (Exception ex)
{
throw new Exception(ex.Message.Replace("ROLLBACK TRANSACTION 请求没有对应的 BEGIN TRANSACTION。", "", StringComparison.CurrentCulture));
}
}
/// <summary>
/// 获取任务号
/// </summary>
/// <param name="TaskType">TaskType.</param>
/// <param name="SeqLength">长度.</param>
public string GetTaskNo(string TaskType, int SeqLength = 4)
{
string Value = "1";
SysCount sysCount = _context.Set<SysCount>().AsNoTracking().FirstOrDefault(u => u.Type == TaskType);
if (sysCount == null)
{
Value = TaskType + DateTime.Now.ToString("yyyyMMdd") + Value.PadLeft(SeqLength, '0');
sysCount = new SysCount { Type = TaskType, Value = Value };
_context.Set<SysCount>().Add(sysCount);
Save();
}
else
{
string Date = sysCount.Value.Substring(TaskType.Length, 8);
if (Date == DateTime.Now.ToString("yyyyMMdd"))
{
Value = Date + (int.Parse(sysCount.Value.Substring(sysCount.Value.Length - 4, 4)) + 1).ToString().PadLeft(SeqLength, '0');
}
else
{
Value = DateTime.Now.ToString("yyyyMMdd") + Value.PadLeft(SeqLength, '0');
}
Value = TaskType + Value;
_context.Set<SysCount>().Where(u => u.Type == TaskType).Update(u => new SysCount { Value = Value });
Save();
}
return Value;
}
}
}