using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Transactions; using System; using Church.Net.DAL.EF; using System.Linq; using Church.Net.Utility; using System.Threading.Tasks; using Church.Net.Entity.Interface; using Church.Net.DAL.EFCoreDBF.Interface; namespace Church.Net.DAL.EFCoreDBF.Core { public class CrudDALCBase : ICrudDAL where T : class, IEntity, new() { private readonly DatabaseOptions databaseOptions; //private readonly ChurchNetContext dbContext; public CrudDALCBase(DatabaseOptions databaseOptions) { this.databaseOptions = databaseOptions; //this.dbContext = databaseOptions.GetDbContext(); } public virtual IQueryable InitQuery(ChurchNetContext dbContext) { return dbContext.Set(); } public ChurchNetContext GetDbContext() { //var result = (DbSet)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null); return databaseOptions.GetDbContext();//.Set().AsNoTracking(); //return result; } public virtual T First(Func filter = null) { using (var dbContext = GetDbContext()) { return InitQuery(dbContext).Where(filter ?? (s => true)).FirstOrDefault(); } } public virtual T GetById(string Id) { using (var dbContext = GetDbContext()) { return InitQuery(dbContext).FirstOrDefault(e => e.Id == Id); } } //public virtual T GetByRefndx(string refndx) //{ // return MapDBModelToEntity(GetFirstDBModelByRefndx(refndx, this.actionScreen)); //} public virtual IEnumerable GetAll(Func filter = null) { using (var dbContext = GetDbContext()) { return InitQuery(dbContext).Where(filter ?? (s => true)).ToList(); } //var dbObjs = GetDbContext().ToArray(); //IEnumerable list = GetDbContext().Where(filter ?? (s => true)).ToList(); } public virtual IEnumerable GetAllById(IEnumerable Ids) { using (var dbContext = GetDbContext()) { var RowIds = Ids.Select(i => i.ToString()); //return GetDbContext().Where(e => RowIds.Any(id => id == e.Id)); //var list = new List(); return InitQuery(dbContext).Where(e => RowIds.Any(id => id == e.Id)).ToArray(); } } private bool needGenId(T entity) { return string.IsNullOrWhiteSpace(entity.Id) || entity.Id.Length <= 3; } public virtual int Create(T entity) { if (needGenId(entity)) { entity.Id = StringHelper.Get33BaseGuid(); } ConvertUTCTime(entity); using (var dbContext = GetDbContext()) { dbContext.Add(entity); return dbContext.SaveChanges(); } } public virtual Task CreateAsync(T entity) { int result = 0; ConvertUTCTime(entity); if (needGenId(entity)) { entity.Id = StringHelper.Get33BaseGuid(); } T newDbObj = new T(); if (newDbObj != null) { //AfterCreatMapping(entity, newDbObj); //newDbObj.RowId = entity.Id.ToString(); //result = Add(newDbObj) ? 1 : 0; } using (var dbContext = GetDbContext()) { dbContext.Add(entity); //CreateDone(entity, newDbObj); return dbContext.SaveChangesAsync(); } } public virtual string CreateReturnId(T entity) { ConvertUTCTime(entity); if (Create(entity) > 0) { return entity.Id; } else { return null; }; } public int CreateOrUpdate(T entity) { ConvertUTCTime(entity); using (var dbContext = GetDbContext()) { if (CheckExist(entity)) { dbContext.Update(entity); } else { if (needGenId(entity)) { entity.Id = StringHelper.Get33BaseGuid(); } dbContext.Add(entity); } return dbContext.SaveChanges(); } } public virtual int Update(T entity) { using (var dbContext = GetDbContext()) { //var dbObj = this.InitQuery(dbContext).Any(e => e.Id == entity.Id); if (!InitQuery(dbContext).Any(e => e.Id == entity.Id)) { throw new ArgumentNullException("the Id is not exist."); } ConvertUTCTime(entity); dbContext.Update(entity); return dbContext.SaveChanges(); } } public virtual void CreateDone(T entity) { } public virtual void UpdateDone(T entity) { } public virtual int UpdateRange(IEnumerable entities) { foreach (var entity in entities) { ConvertUTCTime(entity); } using (var dbContext = GetDbContext()) { dbContext.UpdateRange(entities); return dbContext.SaveChanges(); } } public virtual int Delete(T obj) { using (var dbContext = GetDbContext()) { dbContext.Remove(InitQuery(dbContext).FirstOrDefault(e => e.Id == obj.Id.ToString())); return dbContext.SaveChanges(); } } public int Delete(Func filter) { using (var dbContext = GetDbContext()) { var list = InitQuery(dbContext).Where(filter).ToList(); if (list.Count > 0) { dbContext.RemoveRange(list); return dbContext.SaveChanges(); } return 0; } } public virtual bool CheckExist(T obj) { using (var dbContext = GetDbContext()) { return InitQuery(dbContext).Any(e => e.Id == obj.Id); } } private void ConvertUTCTime(T entity) { var props = typeof(T).GetProperties(); foreach (var prop in props) { if (prop.PropertyType == typeof(DateTime)) { //do stuff like prop.SetValue(t, DateTime.Now, null); DateTime localTime = (DateTime)prop.GetValue(entity); if (localTime.Kind != DateTimeKind.Utc) { localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day, localTime.Hour, localTime.Minute, localTime.Second, localTime.Millisecond, DateTimeKind.Local); } var offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now); var OutputTime = DateTime.SpecifyKind(localTime - offset, DateTimeKind.Utc); DateTime utcTime = localTime.ToUniversalTime(); prop.SetValue(entity, utcTime, null); } else if (prop.PropertyType == typeof(DateTime?)) { DateTime? localTime = (DateTime?)prop.GetValue(entity); if (localTime.HasValue) { if (localTime.Value.Kind != DateTimeKind.Utc) { //localTime = DateTime.SpecifyKind(localTime.Value, DateTimeKind.Local); localTime = new DateTime(localTime.Value.Year, localTime.Value.Month, localTime.Value.Day, localTime.Value.Hour, localTime.Value.Minute, localTime.Value.Second, localTime.Value.Millisecond, DateTimeKind.Local); } DateTime? utcTime = localTime.Value.ToUniversalTime(); prop.SetValue(entity, utcTime, null); } } } } public IQueryable GetDbSet() { return databaseOptions.GetDbContext().Set().AsNoTracking(); } } }