|
|
using Admin.Core.IRepository;
|
|
|
using Admin.Core.IService;
|
|
|
using Admin.Core.Model;
|
|
|
using SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Data;
|
|
|
using System.Linq.Expressions;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace Admin.Core.Service
|
|
|
{
|
|
|
public class BaseServices<TEntity> : IBaseServices<TEntity> where TEntity : class, new()
|
|
|
{
|
|
|
//public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>();
|
|
|
public IBaseRepository<TEntity> BaseDal { get; set; }//通过在子类的构造函数中注入,这里是基类,不用构造函数
|
|
|
|
|
|
/// <summary>
|
|
|
/// 正序查询第一条数据
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public async Task<TEntity> FirstAsync()
|
|
|
{
|
|
|
return await BaseDal.FirstAsync();
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 根据条件查询查询第一条数据
|
|
|
/// </summary>
|
|
|
/// <param name="whereExpression"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> whereExpression)
|
|
|
{
|
|
|
return await BaseDal.FirstAsync(whereExpression);
|
|
|
}
|
|
|
|
|
|
|
|
|
public async Task<TEntity> QueryByIdAsync(object objId)
|
|
|
{
|
|
|
return await BaseDal.QueryByIdAsync(objId);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 功能描述:根据ID查询一条数据
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
|
|
/// <param name="blnUseCache">是否使用缓存</param>
|
|
|
/// <returns>数据实体</returns>
|
|
|
public async Task<TEntity> QueryByIdAsync(object objId, bool blnUseCache = false)
|
|
|
{
|
|
|
return await BaseDal.QueryByIdAsync(objId, blnUseCache);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:根据ID查询数据
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="lstIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
|
|
|
/// <returns>数据实体列表</returns>
|
|
|
public async Task<List<TEntity>> QueryByIDsAsync(object[] lstIds)
|
|
|
{
|
|
|
return await BaseDal.QueryByIDsAsync(lstIds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 写入实体数据
|
|
|
/// </summary>
|
|
|
/// <param name="entity">实体类</param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<int> AddAsync(TEntity entity)
|
|
|
{
|
|
|
return await BaseDal.AddAsync(entity);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 批量插入实体(速度快)
|
|
|
/// </summary>
|
|
|
/// <param name="listEntity">实体集合</param>
|
|
|
/// <returns>影响行数</returns>
|
|
|
public async Task<int> AddAsync(List<TEntity> listEntity)
|
|
|
{
|
|
|
return await BaseDal.AddAsync(listEntity);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 更新实体数据
|
|
|
/// </summary>
|
|
|
/// <param name="entity">实体类</param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> UpdateAsync(TEntity entity)
|
|
|
{
|
|
|
return await BaseDal.UpdateAsync(entity);
|
|
|
}
|
|
|
public async Task<bool> UpdateAsync(TEntity entity, string strWhere)
|
|
|
{
|
|
|
return await BaseDal.UpdateAsync(entity, strWhere);
|
|
|
}
|
|
|
public async Task<bool> UpdateAsync(object operateAnonymousObjects)
|
|
|
{
|
|
|
return await BaseDal.UpdateAsync(operateAnonymousObjects);
|
|
|
}
|
|
|
|
|
|
public async Task<bool> UpdateAsync(
|
|
|
TEntity entity,
|
|
|
List<string> lstColumns = null,
|
|
|
List<string> lstIgnoreColumns = null,
|
|
|
string strWhere = ""
|
|
|
)
|
|
|
{
|
|
|
return await BaseDal.UpdateAsync(entity, lstColumns, lstIgnoreColumns, strWhere);
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据实体删除一条数据
|
|
|
/// </summary>
|
|
|
/// <param name="entity">实体类</param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> DeleteAsync(TEntity entity)
|
|
|
{
|
|
|
return await BaseDal.DeleteAsync(entity);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除指定ID的数据
|
|
|
/// </summary>
|
|
|
/// <param name="id">主键ID</param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> DeleteByIdAsync(object id)
|
|
|
{
|
|
|
return await BaseDal.DeleteByIdAsync(id);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除指定ID集合的数据(批量删除)
|
|
|
/// </summary>
|
|
|
/// <param name="ids">主键ID集合</param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> DeletesAsync(List<TEntity> entitys)
|
|
|
{
|
|
|
return await BaseDal.DeletesAsync(entitys);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询所有数据
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync()
|
|
|
{
|
|
|
return await BaseDal.QueryAsync();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询数据列表
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="strWhere">条件</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(string strWhere)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(strWhere);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询数据列表
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="whereExpression">whereExpression</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(whereExpression);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:按照特定列查询数据列表
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <typeparam name="TResult"></typeparam>
|
|
|
/// <param name="expression"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(expression);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:按照特定列查询数据列表带条件排序
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <typeparam name="TResult"></typeparam>
|
|
|
/// <param name="whereExpression">过滤条件</param>
|
|
|
/// <param name="expression">查询实体条件</param>
|
|
|
/// <param name="strOrderByFileds">排序条件</param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(expression, whereExpression, strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询一个列表
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(whereExpression, orderByExpression, isAsc);
|
|
|
}
|
|
|
|
|
|
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(whereExpression, strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询一个列表
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="strWhere">条件</param>
|
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(string strWhere, string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(strWhere, strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据sql语句查询
|
|
|
/// </summary>
|
|
|
/// <param name="strSql">完整的sql语句</param>
|
|
|
/// <param name="parameters">参数</param>
|
|
|
/// <returns>泛型集合</returns>
|
|
|
public async Task<List<TEntity>> QuerySqlAsync(string strSql, SugarParameter[] parameters = null)
|
|
|
{
|
|
|
return await BaseDal.QuerySqlAsync(strSql, parameters);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 执行sql语句
|
|
|
/// </summary>
|
|
|
/// <param name="strSql">完整的sql语句</param>
|
|
|
/// <param name="parameters">参数</param>
|
|
|
/// <returns>泛型集合</returns>
|
|
|
public async Task<int> ExecSqlAsync(string strSql, SugarParameter[] parameters = null)
|
|
|
{
|
|
|
return await BaseDal.ExecSqlAsync(strSql, parameters);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据sql语句查询
|
|
|
/// </summary>
|
|
|
/// <param name="strSql">完整的sql语句</param>
|
|
|
/// <param name="parameters">参数</param>
|
|
|
/// <returns>DataTable</returns>
|
|
|
public async Task<DataTable> QueryTableAsync(string strSql, SugarParameter[] parameters = null)
|
|
|
{
|
|
|
return await BaseDal.QueryTableAsync(strSql, parameters);
|
|
|
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询前N条数据
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
|
/// <param name="intTop">前N条</param>
|
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(whereExpression, intTop, strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:查询前N条数据
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="strWhere">条件</param>
|
|
|
/// <param name="intTop">前N条</param>
|
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(
|
|
|
string strWhere,
|
|
|
int intTop,
|
|
|
string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(strWhere, intTop, strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:分页查询
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="whereExpression">条件表达式</param>
|
|
|
/// <param name="intPageIndex">页码(下标0)</param>
|
|
|
/// <param name="intPageSize">页大小</param>
|
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(
|
|
|
Expression<Func<TEntity, bool>> whereExpression,
|
|
|
int intPageIndex,
|
|
|
int intPageSize,
|
|
|
string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(
|
|
|
whereExpression,
|
|
|
intPageIndex,
|
|
|
intPageSize,
|
|
|
strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 功能描述:分页查询
|
|
|
/// 作 者:Admin.Core
|
|
|
/// </summary>
|
|
|
/// <param name="strWhere">条件</param>
|
|
|
/// <param name="intPageIndex">页码(下标0)</param>
|
|
|
/// <param name="intPageSize">页大小</param>
|
|
|
/// <param name="strOrderByFileds">排序字段,如name asc,age desc</param>
|
|
|
/// <returns>数据列表</returns>
|
|
|
public async Task<List<TEntity>> QueryAsync(
|
|
|
string strWhere,
|
|
|
int intPageIndex,
|
|
|
int intPageSize,
|
|
|
string strOrderByFileds)
|
|
|
{
|
|
|
return await BaseDal.QueryAsync(
|
|
|
strWhere,
|
|
|
intPageIndex,
|
|
|
intPageSize,
|
|
|
strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
public async Task<PageModel<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression,
|
|
|
int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
|
|
|
{
|
|
|
return await BaseDal.QueryPageAsync(whereExpression,
|
|
|
intPageIndex, intPageSize, strOrderByFileds);
|
|
|
}
|
|
|
|
|
|
public async Task<List<TResult>> QueryMuchAsync<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
|
|
|
{
|
|
|
return await BaseDal.QueryMuchAsync(joinExpression, selectExpression, whereLambda);
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 同步
|
|
|
/// </summary>
|
|
|
/// <param name="whereExpression"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression)
|
|
|
{
|
|
|
return BaseDal.Query(whereExpression);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|