#region << 版 本 注 释 >> /* * 版权所有 (c)2024 保留所有权 * CLR版本 4.0.30319.42000 * * * * * * */ #endregion << 版 本 注 释 >> using AutoMapper; 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; } /// /// 返回list /// /// public List GetAllForList() { return _db.Queryable().ToList(); } public T GetById(object id) { return _db.Queryable().InSingle(id); } /// /// /// /// /// public bool Insert(List entities) { return _db.Insertable(entities.ToArray()).ExecuteCommand() > 0; } /// /// 添加返回实体 /// /// /// public bool InsertForEntity(T entity) { return _db.Insertable(entity).ExecuteCommandIdentityIntoEntity(); } public bool Update(T entity) { return _db.Updateable(entity).ExecuteCommandHasChange(); } /// /// 带事务的添加 /// /// /// 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 带事务的更新 /// /// 带事务的更新 /// /// /// 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 /// /// 带事务的删除 /// /// /// 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(); } /// /// /// /// /// /// 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 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; } } }