253 lines
7.9 KiB
C#
253 lines
7.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
|
|
|
|
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');
|
|
}
|
|
}
|
|
}
|