#if netcore
using FreeSql.DataAnnotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
namespace FreeSql
{
///
/// 包括 CreateTime/UpdateTime/IsDeleted 的实体基类
///
[Table(DisableSyncStructure = true)]
public abstract class BaseEntity
{
static IFreeSql _ormPriv;
///
/// 全局 IFreeSql orm 对象
///
public static IFreeSql Orm => _ormPriv ?? throw new Exception(@"使用前请初始化 BaseEntity.Initialization(new FreeSqlBuilder()
.UseAutoSyncStructure(true)
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
.Build());");
///
/// 初始化BaseEntity
/// BaseEntity.Initialization(new FreeSqlBuilder()
///
/// .UseAutoSyncStructure(true)
///
/// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
///
/// .Build());
///
/// IFreeSql orm 对象
public static void Initialization(IFreeSql fsql)
{
_ormPriv = fsql;
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine($"\r\n线程{Thread.CurrentThread.ManagedThreadId}: {e.Sql}\r\n");
}
///
/// 创建时间
///
[Column(Position = -4)]
public DateTime CreateTime { get; set; } = DateTime.Now;
///
/// 更新时间
///
[Column(Position = -3)]
public DateTime UpdateTime { get; set; }
///
/// 逻辑删除
///
[Column(Position = -2)]
public bool IsDeleted { get; set; }
///
/// 排序
///
[Column(Position = -1)]
public int Sort { get; set; }
///
/// 开启工作单元事务
///
///
public static IUnitOfWork Begin() => Begin(null);
///
/// 开启工作单元事务
///
/// 事务等级
///
public static IUnitOfWork Begin(IsolationLevel? level)
{
var uow = Orm.CreateUnitOfWork();
uow.IsolationLevel = level;
return uow;
}
static readonly AsyncLocal _AsyncTenantId = new AsyncLocal();
///
/// 获取或设置当前租户id
///
public static string CurrentTenantId
{
get => _AsyncTenantId.Value;
set => _AsyncTenantId.Value = value;
}
}
///
/// 租户
///
public interface ITenant
{
///
/// 租户id
///
string TenantId { get; set; }
}
public abstract class BaseEntityReadOnly : BaseEntity where TEntity : class
{
///
/// 查询数据
///
///
public static ISelect Select
{
get
{
var select = Orm.Select()
.TrackToList(TrackToList) //自动为每个元素 Attach
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction(false));
if (string.IsNullOrEmpty(CurrentTenantId) == false)
select.WhereCascade(a => (a as ITenant).TenantId == CurrentTenantId);
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
}
}
static void TrackToList(object list)
{
if (list == null) return;
var ls = list as IList;
if (ls == null)
{
var ie = list as IEnumerable;
if (ie == null) return;
var isFirst = true;
foreach (var item in ie)
{
if (item == null) return;
if (isFirst)
{
isFirst = false;
var itemType = item.GetType();
if (itemType == typeof(object)) return;
if (itemType.FullName.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType;
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
if (item is BaseEntity == false) return;
}
(item as BaseEntity)?.Attach();
}
return;
}
if (ls.Any() == false) return;
if (ls.FirstOrDefault() is BaseEntity == false) return;
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
foreach (var item in ls)
(item as BaseEntity)?.Attach();
}
///
/// 设置当前租户id
///
protected void SetTenantId()
{
if (string.IsNullOrEmpty(CurrentTenantId) == false)
{
var ten = this as ITenant;
if (ten != null)
ten.TenantId = CurrentTenantId;
}
}
///
/// 查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com")
///
/// lambda表达式
///
public static ISelect Where(Expression> exp) => Select.Where(exp);
///
/// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com")
///
/// true 时生效
/// lambda表达式
///
public static ISelect WhereIf(bool condition, Expression> exp) => Select.WhereIf(condition, exp);
///
/// 仓储对象
///
protected IBaseRepository Repository { get; set; }
///
/// 附加实体,在更新数据时,只更新变化的部分
///
public TEntity Attach()
{
if (this.Repository == null)
this.Repository = Orm.GetRepository();
var item = this as TEntity;
this.SetTenantId();
this.Repository.Attach(item);
return item;
}
}
}
#endif