227 lines
7.5 KiB
C#
227 lines
7.5 KiB
C#
using System;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using HHWMS.Web.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
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;
|
||
}
|
||
|
||
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>().Where(where).Count();
|
||
}
|
||
|
||
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>(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
|
||
{
|
||
var query = _data.Set<T>().Where(where);
|
||
var count = query.InsertFromQuery(typeof(T).Name + "History", entity);
|
||
var count1 = _data.Set<T>().DeleteRangeByKey(query.ToArray());
|
||
if(count == count1)
|
||
{
|
||
return count;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
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>(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
|
||
{
|
||
/*if (typeof(T).IsSubclassOf(typeof(BasePoco)))
|
||
{
|
||
(entity as BasePoco).UpdateTime = DateTime.Now;
|
||
(entity as BasePoco).UpdateBy = _info?.ITCode;
|
||
}*/
|
||
return _data.Set<T>().Where(where).Update(entity);
|
||
}
|
||
|
||
public int Delete<T>(T entity) where T : class
|
||
{
|
||
_data.Set<T>().Remove(entity);
|
||
return _data.SaveChanges();
|
||
}
|
||
|
||
public int BatchDelete<T>(Expression<Func<T, bool>> where) where T : class
|
||
{
|
||
var query = _data.Set<T>().Where(where);
|
||
return _data.Set<T>().DeleteRangeByKey(query.ToArray());
|
||
}
|
||
|
||
public int DeleteById<T>(T entity) where T : class
|
||
{
|
||
return _data.Set<T>().DeleteByKey(entity);
|
||
}
|
||
|
||
public int DeleteById<T>(int id) where T : class
|
||
{
|
||
return _data.Set<T>().DeleteByKey(id);
|
||
}
|
||
|
||
public int BatchDelete<T>(T[] entities) where T : class
|
||
{
|
||
return _data.Set<T>().DeleteRangeByKey(entities);
|
||
}
|
||
|
||
public string GetSerialNumber(string prefix, int length = 4)
|
||
{
|
||
int seq = 0;
|
||
using (var tran = _data.BeginTransaction())
|
||
{
|
||
try
|
||
{
|
||
var syscount = FindSingle<SysCount>(x => x.Prefix == prefix);
|
||
if (syscount == null)
|
||
{
|
||
syscount = new SysCount
|
||
{
|
||
Prefix = prefix,
|
||
Value = 1,
|
||
UpdateTime = DateTime.Now
|
||
};
|
||
Add(syscount);
|
||
seq = 1;
|
||
}
|
||
else
|
||
{
|
||
// 判断日期是否是当天,否则从1开始
|
||
if (DateTime.Now.Day == syscount.UpdateTime.Value.Day)
|
||
{
|
||
_data.Set<SysCount>().Where(x => x.Prefix == prefix)
|
||
.Update(x => new SysCount
|
||
{
|
||
Value = x.Value + 1,
|
||
UpdateTime = DateTime.Now
|
||
});
|
||
seq = FindSingle<SysCount>(x => x.Prefix == prefix).Value;
|
||
}
|
||
else
|
||
{
|
||
_data.Set<SysCount>().Where(x => x.Prefix == prefix)
|
||
.Update(x => new SysCount
|
||
{
|
||
Value = 1,
|
||
UpdateTime = DateTime.Now
|
||
});
|
||
seq = 1;
|
||
}
|
||
}
|
||
tran.Commit();
|
||
}
|
||
catch (Exception)
|
||
{
|
||
tran.Rollback();
|
||
throw;
|
||
}
|
||
}
|
||
return prefix + DateTime.Now.ToString("yyyyMMdd") + seq.ToString().PadLeft(length, '0');
|
||
}
|
||
}
|
||
}
|