#region << 版 本 注 释 >>
/*
* 版权所有 (c)2024 保留所有权
* CLR版本 4.0.30319.42000
*
*
*
*
*
*
*/
#endregion << 版 本 注 释 >>
using SqlSugar;
using System.Linq.Expressions;
namespace DS.Module.SqlSugar
{
///
/// sqlsugar数据服务基类
///
///
public class DsDataAppService 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().InSingle(tenantId);
// 然后,你可以修改 SQL 语句,添加一个 WHERE 子句来过滤租户数据
// sql += $" WHERE TenantId = {tenant.Id}";
return new KeyValuePair(sql, pars);
};
}
///
/// 返回list
///
///
public List GetAllForList()
{
return _db.Queryable().ToList();
}
///
/// sqlsugar原生DB
///
///
public ISqlSugarClient GetSqlDb()
{
return _db;
}
///
///
///
///
///
public T GetById(object id)
{
return _db.Queryable().InSingle(id);
}
///
///
///
///
///
public bool Insert(List entities)
{
return _db.Insertable(entities.ToArray())
.IgnoreColumns(ignoreNullColumn: true)
.EnableDiffLogEvent()
.ExecuteCommand() > 0;
}
///
/// 添加返回实体
///
///
///
public bool InsertForEntity(T entity)
{
return _db.Insertable(entity)
.IgnoreColumns(ignoreNullColumn: true)
.EnableDiffLogEvent()
.ExecuteCommandIdentityIntoEntity();
}
///
///
///
///
///
public bool Update(T entity)
{
return _db.Updateable(entity)
.IgnoreColumns(ignoreAllNullColumns: true)
.EnableDiffLogEvent()
.ExecuteCommandHasChange();
}
///
/// 带事务的添加
///
///
///
public async Task 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 带事务的更新
///
/// 带事务的更新
///
///
///
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
///
/// 带事务的删除
///
///
///
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;
}
}
///
///
///
///
///
public bool Delete(T entity)
{
return _db.Deleteable(entity)
.EnableDiffLogEvent()
.ExecuteCommandHasChange();
}
///
///
///
///
///
///
public List GetPaged(int pageNumber, int pageSize)
{
return _db.Queryable().ToPageList(pageNumber, pageSize);
}
///
///
///
///
///
public List QueryForList(Expression> whereExpression)
{
return _db.Queryable().Where(whereExpression).ToList();
}
///
///
///
///
///
public async Task QueryForOneEntity(Expression> whereExpression)
{
return await _db.Queryable().Where(whereExpression).FirstAsync();
}
///
/// 查询并返回一个指定类型的实体
///
/// 返回的实体类型
/// 查询条件
/// 选择表达式,定义如何从 T 类型的实体转换为 TResult 类型的实体
/// 返回一个 TResult 类型的实体
public async Task QueryForOneEntity(Expression> whereExpression, Expression> selectExpression)
{
return await _db.Queryable().Where(whereExpression).Select(selectExpression).FirstAsync();
}
///
/// 查询并返回一个指定类型的实体
///
/// 返回的实体类型
/// 查询条件
/// 返回一个 TResult 类型的实体
public async Task QueryForOneEntity(Expression> whereExpression) where TResult : class, new()
{
return await _db.Queryable().Where(whereExpression).Select().FirstAsync();
}
///
///
///
///
///
///
///
public List Query(Expression> whereExpression, Expression> orderByExpression, OrderByType type = OrderByType.Asc)
{
return _db.Queryable().Where(whereExpression).OrderBy(orderByExpression, type).ToList();
}
///
///
///
///
///
///
///
///
public List QueryMultiple(Expression> whereExpression, List>, OrderByType>> orderByExpressions, int pageNumber, int pageSize)
{
var query = _db.Queryable().Where(whereExpression);
foreach (var orderByExpression in orderByExpressions)
{
query = query.OrderBy(orderByExpression.Key, orderByExpression.Value);
}
return query.ToPageList(pageNumber, pageSize);
}
///
///
///
///
///
///
public PagedResult GetPagedForTotalCount(int pageNumber, int pageSize)
{
int totalCount = 0;
var list = _db.Queryable().ToPageList(pageNumber, pageSize, ref totalCount);
int totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
return new PagedResult
{
PageNumber = pageNumber,
PageSize = pageSize,
TotalCount = totalCount,
TotalPages = totalPages,
Results = list
};
}
///
///
///
///
///
///
///
///
///
public List JoinQuery(Expression> joinExpression, Expression> selectExpression)
where T1 : class, new()
where T2 : class, new()
{
return _db.Queryable(joinExpression).Select(selectExpression).ToList();
}
///
///
///
///
///
///
///
///
///
///
public List JoinQuery(
Expression> joinExpression,
Expression> selectExpression)
where T1 : class, new()
where T2 : class, new()
where T3 : class, new()
{
return _db.Queryable(joinExpression).Select(selectExpression).ToList();
}
}
///
///
///
///
public class PagedResult
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public List Results { get; set; }
}
}