using FreeSql; using FreeSql.DataAnnotations; using Newtonsoft.Json; using System; using System.Diagnostics; using System.Linq.Expressions; [Table(DisableSyncStructure = true)] public abstract class BaseEntity { private static Lazy _ormLazy = new Lazy(() => { var orm = new FreeSqlBuilder() .UseAutoSyncStructure(true) .UseNoneCommandParameter(true) .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5") .Build(); orm.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n"); return orm; }); public static IFreeSql Orm => _ormLazy.Value; /// /// 创建时间 /// public DateTime CreateTime { get; set; } /// /// 更新时间 /// public DateTime UpdateTime { get; set; } /// /// 逻辑删除 /// public bool IsDeleted { get; set; } } [Table(DisableSyncStructure = true)] public abstract class BaseEntity : BaseEntity where TEntity : class { public static ISelect Select => Orm.Select().WhereCascade(a => (a as BaseEntity).IsDeleted == false); public static ISelect Where(Expression> exp) => Select.Where(exp); public static ISelect WhereIf(bool condition, Expression> exp) => Select.WhereIf(condition, exp); [JsonIgnore] protected IBaseRepository Repository { get; set; } bool UpdateIsDeleted(bool value) { if (this.Repository == null) return Orm.Update(this as TEntity).Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1; this.IsDeleted = value; return this.Repository.Update(this as TEntity) == 1; } /// /// 删除数据 /// /// public virtual bool Delete() => this.UpdateIsDeleted(true); /// /// 恢复删除的数据 /// /// public virtual bool Restore() => this.UpdateIsDeleted(false); /// /// 附加实体,在更新数据时,只更新变化的部分 /// public void Attach() { if (this.Repository == null) this.Repository = Orm.GetRepository(); this.Repository.Attach(this as TEntity); } /// /// 更新数据 /// /// public virtual bool Update() { if (this.Repository == null) return Orm.Update().SetSource(this as TEntity).ExecuteAffrows() == 1; return this.Repository.Update(this as TEntity) == 1; } /// /// 插入数据 /// public virtual void Insert() { if (this.Repository == null) this.Repository = Orm.GetRepository(); this.Repository.Insert(this as TEntity); } /// /// 更新或插入 /// /// public virtual void Save() { if (this.Repository == null) this.Repository = Orm.GetRepository(); this.Repository.InsertOrUpdate(this as TEntity); } } [Table(DisableSyncStructure = true)] public abstract class BaseEntity : BaseEntity where TEntity : class { static BaseEntity() { var tkeyType = typeof(TKey)?.NullableTypeOrThis(); if (tkeyType == typeof(int) || tkeyType == typeof(long)) Orm.CodeFirst.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true)); } /// /// 主键 /// public virtual TKey Id { get; set; } /// /// 根据主键值获取数据 /// /// /// public static TEntity Find(TKey id) { var item = Select.WhereDynamic(id).First(); (item as BaseEntity)?.Attach(); return item; } } [Table(DisableSyncStructure = true)] public abstract class BaseEntity : BaseEntity where TEntity : class { /// /// 主键1 /// [Column(IsPrimary = true)] public virtual TKey1 PkId1 { get; set; } /// /// 主键2 /// [Column(IsPrimary = true)] public virtual TKey2 PkId2 { get; set; } /// /// 根据主键值获取数据 /// /// 主键1 /// 主键2 /// public static TEntity Find(TKey1 pkid1, TKey1 pkid2) { var item = Select.WhereDynamic(new { PkId1 = pkid1, PkId2 = pkid2 }).First(); (item as BaseEntity).Attach(); return item; } }