schangxiang@126.com
2024-09-04 9242d0da6005e070b0197a26b12ba24d1361466d
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
using iWare.Wms.Core;
using iWare.Wms.Core.Entity;
using Furion;
using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System.Linq.Expressions;
using Yitter.IdGenerator;
 
namespace iWare.Wms.EntityFramework.Core
{
    [AppDbContext("DefaultConnection", DbProvider.SqlServer)]
    public class DefaultDbContext : AppDbContext<DefaultDbContext>, IModelBuilderFilter
    {
        public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
        {
            // 启用实体数据更改监听
            EnabledEntityChangedListener = true;
 
            // 忽略空值更新
            InsertOrUpdateIgnoreNullValues = true;
        }
 
        /// <summary>
        /// 获取租户Id
        /// </summary>
        /// <returns></returns>
        //public object GetTenantId()
        //{
        //    // 流程中没有用到多租户 这里默认返回一个租户
        //    if (App.User == null) return 142307070918780;
        //    return Convert.ToInt64(App.User.FindFirst(ClaimConst.TENANT_ID)?.Value);
        //}
 
        protected override void OnModelCreating(ModelBuilder builder)
        {
            if (Database.ProviderName == DbProvider.Sqlite)
            {
                // SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
                // here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
                // To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset
                // use the DateTimeOffsetToBinaryConverter
                // Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
                // This only supports millisecond precision, but should be sufficient for most use cases.
                foreach (var entityType in builder.Model.GetEntityTypes())
                {
                    var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(DateTimeOffset)
                                                                                || p.PropertyType == typeof(DateTimeOffset?));
                    foreach (var property in properties)
                    {
                        builder
                            .Entity(entityType.Name)
                            .Property(property.Name)
                            .HasConversion(new DateTimeOffsetToBinaryConverter());
                    }
                }
            }
            // 处理mysql时区问题 https://gitee.com/dotnetchina/Furion/issues/I3RSCO#note_5685893_link
            else if (Database.ProviderName == DbProvider.MySql || Database.ProviderName == DbProvider.MySqlOfficial)
            {
                var converter = new ValueConverter<DateTimeOffset, DateTime>(v => v.LocalDateTime, v => v);
 
                // 扫描程序集,获取数据库实体相关类型
                var types = App.EffectiveTypes.Where(t => (typeof(IPrivateEntity).IsAssignableFrom(t) || typeof(IPrivateModelBuilder).IsAssignableFrom(t))
                     && t.IsClass && !t.IsAbstract && !t.IsGenericType && !t.IsInterface && !t.IsDefined(typeof(ManualAttribute), true));
 
                if (types.Any())
                {
                    foreach (var item in types)
                    {
                        if (item.IsSubclassOf(typeof(DEntityBase)) || item.IsSubclassOf(typeof(EntityBase)))
                        {
                            foreach (var property in item.GetProperties())
                            {
                                if (property.PropertyType == typeof(DateTimeOffset?) || property.PropertyType == typeof(DateTimeOffset))
                                {
                                    builder.Entity(item).Property(property.Name).HasConversion(converter);
                                }
                            }
                        }
                    }
                }
            }
 
            base.OnModelCreating(builder);
        }
 
        /// <summary>
        /// 配置租户Id过滤器
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        public void OnCreating(ModelBuilder modelBuilder, EntityTypeBuilder entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            // 配置假删除过滤器
            LambdaExpression expression = FakeDeleteQueryFilterExpression(entityBuilder, dbContext);
            if (expression != null)
                entityBuilder.HasQueryFilter(expression);
            // 配置数据权限动态表达式
            LambdaExpression dataScopesExpression = DataScopesFilterExpression(entityBuilder, dbContext);
            if (dataScopesExpression != null)
                entityBuilder.HasQueryFilter(dataScopesExpression);
        }
 
        protected override void SavingChangesEvent(DbContextEventData eventData, InterceptionResult<int> result)
        {
            // 获取当前事件对应上下文
            var dbContext = eventData.Context;
            // 获取所有更改,删除,新增的实体,但排除审计实体(避免死循环)
            var entities = dbContext.ChangeTracker.Entries()
                  .Where(u => u.Entity.GetType() != typeof(SysLogAudit) && u.Entity.GetType() != typeof(SysLogOp) &&
                              u.Entity.GetType() != typeof(SysLogVis) && u.Entity.GetType() != typeof(SysLogEx) &&
                        (u.State == EntityState.Modified || u.State == EntityState.Deleted || u.State == EntityState.Added)).ToList();
            if (entities == null || entities.Count < 1) return;
 
            //// 判断是否是演示环境
            //var demoEnvFlag = App.GetService<ISysConfigService>().GetDemoEnvFlag().GetAwaiter().GetResult();
            //if (demoEnvFlag)
            //{
            //    var sysUser = entities.Find(u => u.Entity.GetType() == typeof(SysUser));
            //    if (sysUser == null || string.IsNullOrEmpty((sysUser.Entity as SysUser).LastLoginTime.ToString())) // 排除登录
            //        throw Oops.Oh(ErrorCode.D1200);
            //}
 
            // 当前操作者信息
            var userId = App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
            var userName = App.User?.FindFirst(ClaimConst.CLAINM_ACCOUNT)?.Value;
            // 当前操作者机构信息
            var orgId = App.User?.FindFirst(ClaimConst.CLAINM_ORGID)?.Value;
            var orgName = App.User?.FindFirst(ClaimConst.CLAINM_ORGNAME)?.Value;
 
            foreach (var entity in entities)
            {
                if (entity.Entity.GetType().IsSubclassOf(typeof(DEntityBase)))
                {
                    var obj = entity.Entity as DEntityBase;
                    if (entity.State == EntityState.Added)
                    {
                        obj.Id = obj.Id == 0 ? YitIdHelper.NextId() : obj.Id;
                        obj.CreatedTime = DateTimeOffset.Now;
                        if (!string.IsNullOrEmpty(userId))
                        {
                            obj.CreatedUserId = long.Parse(userId);
                            obj.CreatedUserName = userName;
                            if (entity.Entity.GetType().GetInterface(typeof(IDataPermissions).Name) != null)
                            {
                                ((IDataPermissions)obj).CreatedUserOrgId = long.Parse(orgId);
                                ((IDataPermissions)obj).CreatedUserOrgName = orgName;
                            }
                        }
                    }
                    else if (entity.State == EntityState.Modified)
                    {
                        // 排除创建人
                        entity.Property(nameof(DEntityBase.CreatedUserId)).IsModified = false;
                        entity.Property(nameof(DEntityBase.CreatedUserName)).IsModified = false;
                        // 排除创建日期
                        entity.Property(nameof(DEntityBase.CreatedTime)).IsModified = false;
 
                        obj.UpdatedTime = DateTimeOffset.Now;
                        if (!string.IsNullOrEmpty(userId))
                        {
                            obj.UpdatedUserId = long.Parse(userId);
                            obj.UpdatedUserName = userName;
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 构建租户Id以及假删除过滤器
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="isDeletedKey"></param>
        /// <param name="filterValue"></param>
        /// <returns></returns>
        protected static LambdaExpression FakeDeleteQueryFilterExpression(EntityTypeBuilder entityBuilder, DbContext dbContext, string onTableTenantId = null, string isDeletedKey = null, object filterValue = null)
        {
            //onTableTenantId ??= "TenantId";
            isDeletedKey ??= "IsDeleted";
            IMutableEntityType metadata = entityBuilder.Metadata;
            //if (metadata.FindProperty(onTableTenantId) == null && metadata.FindProperty(isDeletedKey) == null)
            //{
            //    return null;
            //}
            //解决实体继承报错问题,基类表才有IsDeleted、TenantId字段
            if (metadata.BaseType != null)
            {
                return null;
            }
 
            Expression finialExpression = Expression.Constant(true);
            ParameterExpression parameterExpression = Expression.Parameter(metadata.ClrType, "u");
 
 
            // 假删除过滤器
            if (metadata.FindProperty(isDeletedKey) != null)
            {
                ConstantExpression constantExpression = Expression.Constant(isDeletedKey);
                ConstantExpression right = Expression.Constant(filterValue ?? false);
                var fakeDeleteQueryExpression = Expression.Equal(Expression.Call(typeof(EF), "Property", new Type[1]
                {
                    typeof(bool)
                }, parameterExpression, constantExpression), right);
                finialExpression = Expression.AndAlso(finialExpression, fakeDeleteQueryExpression);
            }
 
            return Expression.Lambda(finialExpression, parameterExpression);
        }
 
        #region 数据权限
 
        /// <summary>
        /// 获取用户Id
        /// </summary>
        /// <returns></returns>
        public object GetUserId()
        {
            if (App.User == null) return null;
            return App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
        }
 
        /// <summary>
        /// 获取数据范围
        /// </summary>
        /// <returns></returns>
        public List<object> GetDataScopes()
        {
            var userId = this.GetUserId();
            if (userId == null)
            {
                return new List<object>();
            }
 
            var dataScopes = JsonUtil.FromJson<List<object>>(App.User.FindFirst(ClaimConst.DATA_SCOPES)?.Value);
            if (dataScopes != null)
            {
                return dataScopes;
            }
            return new List<object>();
        }
 
        /// <summary>
        /// 构建数据范围过滤器
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="onTableCreatedUserId"></param>
        /// <param name="onTableCreatedUserOrgId"></param>
        /// <param name="filterValue"></param>
        /// <returns></returns>
        protected LambdaExpression DataScopesFilterExpression(EntityTypeBuilder entityBuilder, DbContext dbContext, string onTableCreatedUserId = null, string onTableCreatedUserOrgId = null)
        {
            onTableCreatedUserId ??= nameof(IDataPermissions.CreatedUserId);//用户id字段
            onTableCreatedUserOrgId ??= nameof(IDataPermissions.CreatedUserOrgId);//用户部门字段
 
            IMutableEntityType metadata = entityBuilder.Metadata;
            if (metadata.FindProperty(onTableCreatedUserId) == null || metadata.FindProperty(onTableCreatedUserOrgId) == null)
            {
                return null;
            }
 
            Expression finialExpression = Expression.Constant(true);
            ParameterExpression parameterExpression = Expression.Parameter(metadata.ClrType, "u");
 
            // 个人用户数据过滤器
            if (metadata.FindProperty(onTableCreatedUserId) != null)
            {
                ConstantExpression constantExpression = Expression.Constant(onTableCreatedUserId);
                MethodCallExpression right = Expression.Call(Expression.Constant(dbContext), dbContext.GetType().GetMethod("GetUserId"));
                finialExpression = Expression.AndAlso(finialExpression, Expression.Equal(Expression.Call(typeof(EF), "Property", new Type[1]
                {
                        typeof(object)
                }, parameterExpression, constantExpression), right));
            }
 
            //数据权限过滤器
            if (metadata.FindProperty(onTableCreatedUserOrgId) != null)
            {
                ConstantExpression constantExpression = Expression.Constant(onTableCreatedUserOrgId);
 
                MethodCallExpression dataScopesLeft = Expression.Call(Expression.Constant(dbContext), dbContext.GetType().GetMethod("GetDataScopes"));
                var firstOrDefaultCall = Expression.Call(typeof(EF), "Property", new Type[1]
                    {
                        typeof(object)
                    }, parameterExpression, constantExpression);
 
                var createdUserOrgIdQueryExpression = Expression.Call(dataScopesLeft, typeof(List<object>).GetMethod("Contains"), firstOrDefaultCall);
 
                finialExpression = Expression.Or(finialExpression, createdUserOrgIdQueryExpression);
            }
 
            return Expression.Lambda(finialExpression, parameterExpression);
        }
 
        #endregion 数据权限
    }
}