281 lines
8.7 KiB
C#
281 lines
8.7 KiB
C#
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;
|
|
|
|
namespace Church.Net.DAL.EFCoreDBF
|
|
{
|
|
public class CrudDALCBase<T> : ICrudDAL<T> where T : class, Church.Net.Entity.Interface.IEntity, new()
|
|
{
|
|
private readonly DatabaseOptions databaseOptions;
|
|
|
|
//private readonly ChurchNetContext dbContext;
|
|
|
|
public CrudDALCBase(DatabaseOptions databaseOptions)
|
|
{
|
|
this.databaseOptions = databaseOptions;
|
|
//this.dbContext = databaseOptions.GetDbContext();
|
|
}
|
|
public ChurchNetContext GetDbContext()
|
|
{
|
|
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
|
|
|
|
return databaseOptions.GetDbContext();//.Set<T>().AsNoTracking();
|
|
//return result;
|
|
}
|
|
|
|
public virtual T First(Func<T, bool> filter = null)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
return dbContext.Set<T>().Where(filter ?? (s => true)).FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
public virtual T GetById(string Id)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
return dbContext.Set<T>().FirstOrDefault(e => e.Id == Id);
|
|
}
|
|
}
|
|
|
|
//public virtual T GetByRefndx(string refndx)
|
|
//{
|
|
// return MapDBModelToEntity(GetFirstDBModelByRefndx<ST>(refndx, this.actionScreen));
|
|
//}
|
|
|
|
public virtual IEnumerable<T> GetAll(Func<T, bool> filter = null)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
return dbContext.Set<T>().Where(filter ?? (s => true)).ToList();
|
|
}
|
|
//var dbObjs = GetDbContext().ToArray();
|
|
|
|
//IEnumerable<T> list = GetDbContext().Where(filter ?? (s => true)).ToList();
|
|
|
|
}
|
|
|
|
public virtual IEnumerable<T> GetAllById(IEnumerable<string> 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<T>();
|
|
|
|
return dbContext.Set<T>().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();
|
|
}
|
|
|
|
this.ConvertUTCTime(entity);
|
|
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.Add(entity);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
public virtual Task<int> CreateAsync(T entity)
|
|
{
|
|
int result = 0;
|
|
this.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)
|
|
{
|
|
this.ConvertUTCTime(entity);
|
|
if (Create(entity) > 0)
|
|
{
|
|
return entity.Id;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
};
|
|
}
|
|
|
|
public int CreateOrUpdate(T entity)
|
|
{
|
|
this.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 = dbContext.Set<T>().Any(e => e.Id == entity.Id);
|
|
if (!dbContext.Set<T>().Any(e => e.Id == entity.Id))
|
|
{
|
|
throw new ArgumentNullException("the Id is not exist.");
|
|
}
|
|
this.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<T> entities)
|
|
{
|
|
foreach (var entity in entities)
|
|
{
|
|
this.ConvertUTCTime(entity);
|
|
}
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.UpdateRange(entities);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public virtual int Delete(T obj)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.Remove(dbContext.Set<T>().FirstOrDefault(e => e.Id == obj.Id.ToString()));
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public int Delete(Func<T, bool> filter)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
|
|
var list = dbContext.Set<T>().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 dbContext.Set<T>().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<T> GetDbSet()
|
|
{
|
|
return databaseOptions.GetDbContext().Set<T>().AsNoTracking();
|
|
}
|
|
}
|
|
|
|
|
|
} |