You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
7.9 KiB
C#

#region << 版 本 注 释 >>
/*
* c2024
* CLR 4.0.30319.42000
*
*
*
*
*
*
*/
#endregion << 版 本 注 释 >>
using AutoMapper;
using SqlSugar;
using System.Linq.Expressions;
namespace DS.Module.SqlSugar
{
/// <summary>
/// sqlsugar数据服务基类
/// </summary>
/// <typeparam name="T"></typeparam>
public class DsDataAppService<T> where T : class, new()
{
private readonly ISqlSugarClient _db;
public DsDataAppService(ISqlSugarClient db)
{
_db = db;
}
/// <summary>
/// 返回list
/// </summary>
/// <returns></returns>
public List<T> GetAllForList()
{
return _db.Queryable<T>().ToList();
}
public T GetById(object id)
{
return _db.Queryable<T>().InSingle(id);
}
/// <summary>
///
/// </summary>
/// <param name="entities"></param>
/// <returns></returns>
public bool Insert(List<T> entities)
{
return _db.Insertable(entities.ToArray()).ExecuteCommand() > 0;
}
/// <summary>
/// 添加返回实体
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool InsertForEntity(T entity)
{
return _db.Insertable(entity).ExecuteCommandIdentityIntoEntity();
}
public bool Update(T entity)
{
return _db.Updateable(entity).ExecuteCommandHasChange();
}
/// <summary>
/// 带事务的添加
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool InsertForTran(T entity)
{
try
{
_db.Ado.BeginTran();
var result = _db.Insertable(entity).ExecuteCommandIdentityIntoEntity();
_db.Ado.CommitTran();
return result;
}
catch (Exception ex)
{
_db.Ado.RollbackTran();
throw ex;
}
}
#region 带事务的更新
/// <summary>
/// 带事务的更新
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool UpdateForTran(T entity)
{
try
{
_db.Ado.BeginTran();
var result = _db.Updateable(entity).ExecuteCommandHasChange();
_db.Ado.CommitTran();
return result;
}
catch (Exception ex)
{
_db.Ado.RollbackTran();
throw ex;
}
}
#endregion
/// <summary>
/// 带事务的删除
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public bool DeleteForTran(T entity)
{
try
{
_db.Ado.BeginTran();
var result = _db.Deleteable(entity).ExecuteCommandHasChange();
_db.Ado.CommitTran();
return result;
}
catch (Exception ex)
{
_db.Ado.RollbackTran();
throw ex;
}
}
public bool Delete(T entity)
{
return _db.Deleteable(entity).ExecuteCommandHasChange();
}
/// <summary>
///
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public List<T> GetPaged(int pageNumber, int pageSize)
{
return _db.Queryable<T>().ToPageList(pageNumber, pageSize);
}
/// <summary>
///
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
public List<T> QueryForList(Expression<Func<T, bool>> whereExpression)
{
return _db.Queryable<T>().Where(whereExpression).ToList();
}
/// <summary>
///
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
public async Task<T> QueryForOneEntity(Expression<Func<T, bool>> whereExpression)
{
return await _db.Queryable<T>().Where(whereExpression).FirstAsync();
}
/// <summary>
/// 查询并返回一个指定类型的实体
/// </summary>
/// <typeparam name="TResult">返回的实体类型</typeparam>
/// <param name="whereExpression">查询条件</param>
/// <param name="selectExpression">选择表达式,定义如何从 T 类型的实体转换为 TResult 类型的实体</param>
/// <returns>返回一个 TResult 类型的实体</returns>
public async Task<TResult> QueryForOneEntity<TResult>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, TResult>> selectExpression)
{
return await _db.Queryable<T>().Where(whereExpression).Select(selectExpression).FirstAsync();
}
/// <summary>
/// 查询并返回一个指定类型的实体
/// </summary>
/// <typeparam name="TResult">返回的实体类型</typeparam>
/// <param name="whereExpression">查询条件</param>
/// <returns>返回一个 TResult 类型的实体</returns>
public async Task<TResult> QueryForOneEntity<TResult>(Expression<Func<T, bool>> whereExpression) where TResult : class, new()
{
return await _db.Queryable<T>().Where(whereExpression).Select<TResult>().FirstAsync();
}
/// <summary>
///
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="orderByExpression"></param>
/// <param name="type"></param>
/// <returns></returns>
public List<T> Query(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderByExpression, OrderByType type = OrderByType.Asc)
{
return _db.Queryable<T>().Where(whereExpression).OrderBy(orderByExpression, type).ToList();
}
/// <summary>
///
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="orderByExpressions"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public List<T> QueryMultiple(Expression<Func<T, bool>> whereExpression, List<KeyValuePair<Expression<Func<T, object>>, OrderByType>> orderByExpressions, int pageNumber, int pageSize)
{
var query = _db.Queryable<T>().Where(whereExpression);
foreach (var orderByExpression in orderByExpressions)
{
query = query.OrderBy(orderByExpression.Key, orderByExpression.Value);
}
return query.ToPageList(pageNumber, pageSize);
}
/// <summary>
///
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public PagedResult<T> GetPagedForTotalCount(int pageNumber, int pageSize)
{
int totalCount = 0;
var list = _db.Queryable<T>().ToPageList(pageNumber, pageSize, ref totalCount);
int totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new PagedResult<T>
{
PageNumber = pageNumber,
PageSize = pageSize,
TotalCount = totalCount,
TotalPages = totalPages,
Results = list
};
}
}
public class PagedResult<T>
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public List<T> Results { get; set; }
}
}