102 lines
2.9 KiB
C#
102 lines
2.9 KiB
C#
using SqlSugar;
|
|||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq.Expressions;
|
||
|
|
|
||
|
|
namespace JY.DAL.Repository
|
||
|
|
{
|
||
|
|
public class Repository<T> : IRepository<T> where T : class, new()
|
||
|
|
{
|
||
|
|
public ISqlSugarClient Db => DataContext.Instance;
|
||
|
|
|
||
|
|
public T GetById(object id)
|
||
|
|
{
|
||
|
|
return Db.Queryable<T>().InSingle(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public T GetSingle(Expression<Func<T, bool>> predicate)
|
||
|
|
{
|
||
|
|
return Db.Queryable<T>().Where(predicate).Single();
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<T> GetList()
|
||
|
|
{
|
||
|
|
return Db.Queryable<T>().ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<T> GetList(Expression<Func<T, bool>> predicate)
|
||
|
|
{
|
||
|
|
return Db.Queryable<T>().Where(predicate).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<T> GetListPaged(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> predicate = null, string orderBy = null)
|
||
|
|
{
|
||
|
|
totalCount = 0;
|
||
|
|
var query = Db.Queryable<T>();
|
||
|
|
if (predicate != null)
|
||
|
|
{
|
||
|
|
query = query.Where(predicate);
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrEmpty(orderBy))
|
||
|
|
{
|
||
|
|
query = query.OrderBy(orderBy);
|
||
|
|
}
|
||
|
|
return query.ToPageList(pageIndex, pageSize, ref totalCount);
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Insert(T entity)
|
||
|
|
{
|
||
|
|
return Db.Insertable(entity).ExecuteReturnIdentity();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Insert(List<T> entities)
|
||
|
|
{
|
||
|
|
return Db.Insertable(entities).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Update(T entity)
|
||
|
|
{
|
||
|
|
return Db.Updateable(entity).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Update(T entity, Expression<Func<T, bool>> whereExpression)
|
||
|
|
{
|
||
|
|
return Db.Updateable(entity).Where(whereExpression).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
|
||
|
|
{
|
||
|
|
return Db.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Delete(object id)
|
||
|
|
{
|
||
|
|
return Db.Deleteable<T>().In(id).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Delete(Expression<Func<T, bool>> predicate)
|
||
|
|
{
|
||
|
|
return Db.Deleteable<T>().Where(predicate).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Delete(List<T> entities)
|
||
|
|
{
|
||
|
|
return Db.Deleteable(entities).ExecuteCommand();
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool Any(Expression<Func<T, bool>> predicate)
|
||
|
|
{
|
||
|
|
return Db.Queryable<T>().Where(predicate).Any();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int Count(Expression<Func<T, bool>> predicate = null)
|
||
|
|
{
|
||
|
|
var query = Db.Queryable<T>();
|
||
|
|
if (predicate != null)
|
||
|
|
{
|
||
|
|
query = query.Where(predicate);
|
||
|
|
}
|
||
|
|
return query.Count();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|