using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using HHWMS.Web.Models; using Microsoft.EntityFrameworkCore; using NPOI.POIFS.FileSystem; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; using Z.EntityFramework.Plus; namespace HHWMS.Web { public class UnitWork : EmptyContext, IUnitWork { private IDataContext _data; private LoginUserInfo _info; public UnitWork(WTMContext context) { _data = context.DC; _info = context.LoginUserInfo; } #region find public T FindSingle(Expression> where) where T : class { return _data.Set().AsNoTracking().FirstOrDefault(where); } public T FindById(Guid id) where T : class { return _data.Set().Find(id); } public T FindById(int id) where T : class { return _data.Set().Find(id); } public IQueryable Find(Expression> where) where T : class { return _data.Set().Where(where); } public bool IsExit(Expression> where) where T : class { return _data.Set().Any(where); } public int GetCount(Expression> where) where T : class { return _data.Set().Count(where); } #endregion find #region insert public new int Add(T entity) where T : class { if (typeof(T).IsSubclassOf(typeof(BasePoco))) { if ((entity as BasePoco).CreateTime == null) { (entity as BasePoco).CreateTime = DateTime.Now; } if (string.IsNullOrEmpty((entity as BasePoco).CreateBy)) { (entity as BasePoco).CreateBy = _info?.ITCode; } } _data.Set().Add(entity); return _data.SaveChanges(); } public int BatchAdd(IEnumerable entities) where T : class { foreach (T entity in entities) { //自动设定添加日期和添加人 if (typeof(T).IsSubclassOf(typeof(BasePoco))) { if ((entity as BasePoco).CreateTime == null) { (entity as BasePoco).CreateTime = DateTime.Now; } if (string.IsNullOrEmpty((entity as BasePoco).CreateBy)) { (entity as BasePoco).CreateBy = _info?.ITCode; } } } _data.Set().AddRange(entities); return _data.SaveChanges(); } public int AddToHistory(string name, Expression> where, Expression> entity) where T : class { return _data.Set().Where(where).InsertFromQuery(name,entity); } #endregion insert #region update public new int Update(T entity) where T : class { if (typeof(T).IsSubclassOf(typeof(BasePoco))) { (entity as BasePoco).UpdateTime = DateTime.Now; (entity as BasePoco).UpdateBy = _info?.ITCode; } _data.Set().Update(entity); return _data.SaveChanges(); } public int BatchUpdate(IEnumerable entities) where T : class { foreach (T entity in entities) { //自动设定添加日期和添加人 if (typeof(T).IsSubclassOf(typeof(BasePoco))) { if ((entity as BasePoco).CreateTime == null) { (entity as BasePoco).CreateTime = DateTime.Now; } if (string.IsNullOrEmpty((entity as BasePoco).CreateBy)) { (entity as BasePoco).CreateBy = _info?.ITCode; } } } _data.Set().UpdateRange(entities); return _data.SaveChanges(); } public int BatchUpdate(Expression> where, Expression> entity) where T : class { return _data.Set().Where(where).Update(entity); } #endregion update #region delete public int Delete(T entity) where T : class { _data.Set().Remove(entity); return _data.SaveChanges(); } public int BatchDelete(IEnumerable entities) where T : class { if (_data.DBType.Equals(DBTypeEnum.SqlServer)) { return _data.Set().DeleteRangeByKey(entities); } else { _data.Set().RemoveRange(entities); return _data.SaveChanges(); } } public int BatchDelete(Expression> where) where T : class { return _data.Set().Where(where).Delete(); } /// /// 测试 不支持mysql /// /// /// /// public int DeleteById(T entity) where T : class { return _data.Set().DeleteByKey((entity as BasePoco).ID); } /// /// 测试 不支持mysql /// /// /// /// public int DeleteById(int id) where T : class { return _data.Set().DeleteByKey(id); } #endregion delete } }