WMS/HHWMS.Web/Resposity/UnitWork.cs
2024-06-20 12:29:49 +08:00

199 lines
5.9 KiB
C#

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<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().AsNoTracking().FirstOrDefault(where);
}
public T FindById<T>(Guid id) where T : class
{
return _data.Set<T>().Find(id);
}
public T FindById<T>(int id) where T : class
{
return _data.Set<T>().Find(id);
}
public IQueryable<T> Find<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Where(where);
}
public bool IsExit<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Any(where);
}
public int GetCount<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Count(where);
}
#endregion find
#region insert
public new int Add<T>(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<T>().Add(entity);
return _data.SaveChanges();
}
public int BatchAdd<T>(IEnumerable<T> 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<T>().AddRange(entities);
return _data.SaveChanges();
}
public int AddToHistory<T>(string name, Expression<Func<T, bool>> where, Expression<Func<T, object>> entity) where T : class
{
return _data.Set<T>().Where(where).InsertFromQuery(name,entity);
}
#endregion insert
#region update
public new int Update<T>(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<T>().Update(entity);
return _data.SaveChanges();
}
public int BatchUpdate<T>(IEnumerable<T> 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<T>().UpdateRange(entities);
return _data.SaveChanges();
}
public int BatchUpdate<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T : class
{
return _data.Set<T>().Where(where).Update(entity);
}
#endregion update
#region delete
public int Delete<T>(T entity) where T : class
{
_data.Set<T>().Remove(entity);
return _data.SaveChanges();
}
public int BatchDelete<T>(IEnumerable<T> entities) where T : class
{
if (_data.DBType.Equals(DBTypeEnum.SqlServer))
{
return _data.Set<T>().DeleteRangeByKey(entities);
}
else
{
_data.Set<T>().RemoveRange(entities);
return _data.SaveChanges();
}
}
public int BatchDelete<T>(Expression<Func<T, bool>> where) where T : class
{
return _data.Set<T>().Where(where).Delete();
}
/// <summary>
/// 测试 不支持mysql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public int DeleteById<T>(T entity) where T : class
{
return _data.Set<T>().DeleteByKey((entity as BasePoco).ID);
}
/// <summary>
/// 测试 不支持mysql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <returns></returns>
public int DeleteById<T>(int id) where T : class
{
return _data.Set<T>().DeleteByKey(id);
}
#endregion delete
}
}