using jiajie.Model;
using jiajie.Repository.UnitOfWork;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace jiajie.Repository
{
///
///
///
///
public class BaseRepository : IBaseRepository where TEntity : class, new()
{
///
/// 服务提供器
///
private readonly IServiceProvider _serviceProvider;
private readonly IUnitOfWork _unitOfWork;
private readonly SqlSugarScope _dbBase;
private ISqlSugarClient _db { get { return _dbBase; } }
public BaseRepository(IUnitOfWork unitOfWork, IServiceProvider serviceProvider)
{
_unitOfWork = unitOfWork;
_dbBase = unitOfWork.GetDbClient();
_serviceProvider = serviceProvider;
Ado = unitOfWork.GetDbClient().Ado;
}
public ISqlSugarClient Db => _db;
///
/// 实体集合
///
public ISugarQueryable Entities => _db.Queryable();
///
/// 原生 Ado 对象
///
public IAdo Ado { get; }
///
/// 切换仓储
///
/// 实体类型
/// 仓储
public IBaseRepository ChangeRepository() where NewEntity : class, new()
{
return _serviceProvider.GetService>();
}
///
/// 获取总数
///
///
///
public async Task CountAsync(Expression> whereExpression)
{
return await Entities.CountAsync(whereExpression);
}
///
/// 检查是否存在
///
///
///
public async Task AnyAsync(Expression> whereExpression)
{
return await Entities.AnyAsync(whereExpression);
}
///
/// 获取一个实体
///
///
///
public async Task SingleAsync(Expression> whereExpression)
{
return await Entities.SingleAsync(whereExpression);
}
///
/// 获取一个实体
///
///
///
public async Task SingleByIdAsync(object objId)
{
return await Entities.In(objId).SingleAsync();
}
///
/// 获取一个实体
///
///
///
public async Task FirstOrDefaultAsync(Expression> whereExpression)
{
return await Entities.FirstAsync(whereExpression);
}
///
/// 获取一个实体 主键
///
///
///
public async Task FirstOrDefaultByIdAsync(object id)
{
return await Entities.In(id).FirstAsync();
}
///
/// 检查存在
///
///
///
public async Task IsExistsByIdAsync(object id)
{
return await Entities.In(id).AnyAsync();
}
///
/// 检查存在
///
///
///
public async Task IsExistsAsync(Expression> whereExpression)
{
return await Entities.AnyAsync(whereExpression);
}
///
/// 新增一条记录
///
///
///
public async Task InsertAsync(TEntity entity)
{
return await _db.Insertable(entity).ExecuteCommandAsync();
}
///
/// 新增多条记录
///
///
///
public async Task InsertAsync(params TEntity[] entities)
{
return await _db.Insertable(entities).ExecuteCommandAsync();
}
///
/// 新增多条记录
///
///
///
public async Task InsertAsync(IEnumerable entities)
{
if (entities != null && entities.Any())
{
return await _db.Insertable(entities.ToArray()).ExecuteCommandAsync();
}
return await Task.FromResult(0);
}
///
/// 新增一条记录返回自增Id
///
///
///
public async Task InsertReturnIdentityAsync(TEntity entity)
{
return await _db.Insertable(entity).ExecuteReturnBigIdentityAsync();
}
///
/// 更新一条记录
///
///
///
public async Task UpdateAsync(TEntity entity)
{
return await _db.Updateable(entity).ExecuteCommandHasChangeAsync();
}
///
/// 无主键更新一条记录
///
/// 更新的实体
/// 根据那些字段更新
///
public async Task UpdateNoPrimaryKeyAsync(TEntity entity, Expression> columns)
{
return await _db.Updateable(entity).WhereColumns(columns).ExecuteCommandHasChangeAsync();
}
///
/// 无主键更新多条记录
///
/// 更新的实体
/// 根据那些字段更新
///
public async Task UpdateNoPrimaryKeyAsync(List entitys, Expression> columns)
{
return await _db.Updateable(entitys).WhereColumns(columns).ExecuteCommandHasChangeAsync();
}
///
/// 更新多条记录
///
///
///
public async Task UpdateAsync(params TEntity[] entities)
{
return await _db.Updateable(entities).ExecuteCommandHasChangeAsync();
}
///
/// 更新多条记录
///
///
///
public async Task UpdateAsync(IEnumerable entities)
{
return await _db.Updateable(entities.ToArray()).ExecuteCommandHasChangeAsync();
}
///
/// 删除一条记录
///
///
///
public async Task DeleteAsync(TEntity entity)
{
return await _db.Deleteable(entity).ExecuteCommandHasChangeAsync();
}
///
/// 删除一条记录
///
///
///
public async Task DeleteAsync(object key)
{
return await _db.Deleteable().In(key).ExecuteCommandHasChangeAsync();
}
///
/// 删除多条记录
///
///
///
public async Task DeleteAsync(params object[] keys)
{
return await _db.Deleteable().In(keys).ExecuteCommandHasChangeAsync();
}
///
/// 自定义条件删除记录
///
///
///
public async Task DeleteAsync(Expression> whereExpression)
{
return await _db.Deleteable().Where(whereExpression).ExecuteCommandHasChangeAsync();
}
///
/// 通过主键查询
///
///
///
public async Task> QueryByIDsAsync(object[] lstIds)
{
return await Entities.In(lstIds).ToListAsync();
}
///
/// 获取列表
///
///
public async Task> QueryAsync()
{
return await Entities.ToListAsync();
}
///
/// 获取列表
///
///
///
public async Task> QueryAsync(Expression> whereExpression)
{
return await Entities.Where(whereExpression).ToListAsync();
}
///
/// 获取列表 带排序
///
///
///
///
public async Task> QueryAsync(Expression> whereExpression, string orderByFields)
{
return await Entities.WhereIF(whereExpression != null, whereExpression).OrderByIF(orderByFields != null, orderByFields).ToListAsync();
}
///
/// 获取列表 带排序
///
///
///
///
///
public async Task> QueryAsync(Expression> whereExpression, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
{
return await Entities.OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToListAsync();
}
///
/// 查询数据列表 带条件
///
/// 条件
/// 数据列表
public async Task> QueryAsync(string where)
{
return await Entities.WhereIF(!string.IsNullOrEmpty(where), where).ToListAsync();
}
///
/// 查询数据列表 带条件 带排序
///
///
///
///
public async Task> QueryAsync(string where, string orderByFields)
{
return await Entities.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).ToListAsync();
}
///
/// 查询数据列表 返回指定列
///
///
///
///
public async Task> QueryAsync(Expression> expression)
{
return await Entities.Select(expression).ToListAsync();
}
///
/// 查询数据列表 返回指定列 带条件 带排序
///
///
///
///
///
///
public async Task> QueryAsync(Expression> expression, Expression> whereExpression, string orderByFields)
{
return await Entities.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync();
}
///
/// 根据sql语句查询
///
/// 完整的sql语句
/// 参数
/// 泛型集合
public async Task> QuerySqlAsync(string sql, SugarParameter[] parameters = null)
{
return await _db.Ado.SqlQueryAsync(sql, parameters);
}
///
/// 分页查询数据列表
///
///
///
///
///
///
public async Task> QueryAsync(
Expression> whereExpression,
int pageIndex,
int pageSize,
string orderByFields)
{
return await Entities.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
.WhereIF(whereExpression != null, whereExpression).ToPageListAsync(pageIndex, pageSize);
}
///
/// 分页查询数据列表
///
///
///
///
///
///
public async Task> QueryAsync(
string where,
int pageIndex,
int pageSize,
string orderByFields)
{
return await Entities.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
.WhereIF(!string.IsNullOrEmpty(where), where).ToPageListAsync(pageIndex, pageSize);
}
///
/// 分页查询数据列表
///
///
///
///
///
///
public async Task> QueryPageAsync(
Expression> whereExpression,
int pageIndex = 1,
int pageSize = 20,
string orderByFields = null
)
{
RefAsync totalCount = 0;
var list = await Entities
.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
.WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(pageIndex, pageSize, totalCount);
return new PageModel(pageIndex, totalCount, pageSize, list);
}
///
///查询-多表查询
///
/// 实体1
/// 实体2
/// 实体3
/// 返回对象
/// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}
/// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}
/// 查询表达式 (w1, w2) =>w1.UserNo == "")
/// 值
public async Task> QueryMuch(
Expression> joinExpression,
Expression> selectExpression,
Expression> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
}
return await _db.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToListAsync();
}
///
/// 两表联合查询-分页
///
/// 实体1
/// 实体1
/// 返回对象
/// 关联表达式
/// 返回表达式
/// 查询表达式
/// 页码
/// 页大小
/// 排序字段
///
public async Task> QueryTabsPage(
Expression> joinExpression,
Expression> selectExpression,
Expression> whereExpression,
int pageIndex = 1,
int pageSize = 20,
string orderByFields = null)
{
RefAsync totalCount = 0;
var list = await _db.Queryable(joinExpression)
.Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
.WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(pageIndex, pageSize, totalCount);
return new PageModel(pageIndex, totalCount, pageSize, list);
}
///
/// 两表联合查询-分页-分组
///
/// 实体1
/// 实体1
/// 返回对象
/// 关联表达式
/// 返回表达式
/// 查询表达式
/// group表达式
/// 页码
/// 页大小
/// 排序字段
///
public async Task> QueryTabsPage(
Expression> joinExpression,
Expression> selectExpression,
Expression> whereExpression,
Expression> groupExpression,
int pageIndex = 1,
int pageSize = 20,
string orderByFields = null)
{
RefAsync totalCount = 0;
var list = await _db.Queryable(joinExpression).GroupBy(groupExpression)
.Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields)
.WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(pageIndex, pageSize, totalCount);
return new PageModel(pageIndex, totalCount, pageSize, list);
}
}
}