|
|
#region << 版 本 注 释 >>
|
|
|
|
|
|
/*
|
|
|
* 版权所有 (c)2024 保留所有权
|
|
|
* CLR版本 4.0.30319.42000
|
|
|
*
|
|
|
*
|
|
|
*
|
|
|
*
|
|
|
*
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
#endregion << 版 本 注 释 >>
|
|
|
|
|
|
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;
|
|
|
//多租户
|
|
|
_db.Aop.OnExecutingChangeSql = (sql, pars) =>
|
|
|
{
|
|
|
// 在这里,你可以从数据库中加载租户信息,然后修改 SQL 语句。
|
|
|
// 例如,你可以添加一个 WHERE 子句来过滤租户数据。
|
|
|
|
|
|
// 假设你的租户信息存储在一个名为 Tenant 的表中
|
|
|
// var tenantId = ...; // 从某处获取当前租户 ID
|
|
|
// var tenant = _db.Queryable<Tenant>().InSingle(tenantId);
|
|
|
|
|
|
// 然后,你可以修改 SQL 语句,添加一个 WHERE 子句来过滤租户数据
|
|
|
// sql += $" WHERE TenantId = {tenant.Id}";
|
|
|
|
|
|
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 返回list
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<T> GetAllForList()
|
|
|
{
|
|
|
return _db.Queryable<T>().ToList();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// sqlsugar原生DB
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public ISqlSugarClient GetSqlDb()
|
|
|
{
|
|
|
return _db;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <returns></returns>
|
|
|
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())
|
|
|
.IgnoreColumns(ignoreNullColumn: true)
|
|
|
.EnableDiffLogEvent()
|
|
|
.ExecuteCommand() > 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 添加返回实体
|
|
|
/// </summary>
|
|
|
/// <param name="entity"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool InsertForEntity(T entity)
|
|
|
{
|
|
|
return _db.Insertable(entity)
|
|
|
.IgnoreColumns(ignoreNullColumn: true)
|
|
|
.EnableDiffLogEvent()
|
|
|
.ExecuteCommandIdentityIntoEntity();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="entity"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool Update(T entity)
|
|
|
{
|
|
|
return _db.Updateable(entity)
|
|
|
.IgnoreColumns(ignoreAllNullColumns: true)
|
|
|
.EnableDiffLogEvent()
|
|
|
.ExecuteCommandHasChange();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 带事务的添加
|
|
|
/// </summary>
|
|
|
/// <param name="entity"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<bool> InsertForTran(T entity)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
_db.Ado.BeginTran();
|
|
|
var result = await _db.Insertable(entity)
|
|
|
.IgnoreColumns(ignoreNullColumn: true)
|
|
|
.EnableDiffLogEvent()
|
|
|
.ExecuteCommandIdentityIntoEntityAsync();
|
|
|
_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)
|
|
|
.IgnoreColumns(ignoreAllNullColumns: true)
|
|
|
.EnableDiffLogEvent()
|
|
|
.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)
|
|
|
.EnableDiffLogEvent()
|
|
|
.ExecuteCommandHasChange();
|
|
|
_db.Ado.CommitTran();
|
|
|
return result;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_db.Ado.RollbackTran();
|
|
|
throw ex;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="entity"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool Delete(T entity)
|
|
|
{
|
|
|
return _db.Deleteable(entity)
|
|
|
.EnableDiffLogEvent()
|
|
|
.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
|
|
|
};
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T1"></typeparam>
|
|
|
/// <typeparam name="T2"></typeparam>
|
|
|
/// <typeparam name="TResult"></typeparam>
|
|
|
/// <param name="joinExpression"></param>
|
|
|
/// <param name="selectExpression"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<TResult> JoinQuery<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression)
|
|
|
where T1 : class, new()
|
|
|
where T2 : class, new()
|
|
|
{
|
|
|
return _db.Queryable(joinExpression).Select(selectExpression).ToList();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T1"></typeparam>
|
|
|
/// <typeparam name="T2"></typeparam>
|
|
|
/// <typeparam name="T3"></typeparam>
|
|
|
/// <typeparam name="TResult"></typeparam>
|
|
|
/// <param name="joinExpression"></param>
|
|
|
/// <param name="selectExpression"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<TResult> JoinQuery<T1, T2, T3, TResult>(
|
|
|
Expression<Func<T1, T2, T3, object[]>> joinExpression,
|
|
|
Expression<Func<T1, T2, T3, TResult>> selectExpression)
|
|
|
where T1 : class, new()
|
|
|
where T2 : class, new()
|
|
|
where T3 : class, new()
|
|
|
{
|
|
|
return _db.Queryable(joinExpression).Select(selectExpression).ToList();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
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; }
|
|
|
}
|
|
|
} |