2026-06-02 14:10:43 +08:00
|
|
|
|
using Mapster;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Context;
|
2026-06-01 13:42:40 +08:00
|
|
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
|
using SqlSugar;
|
2026-06-02 14:10:43 +08:00
|
|
|
|
using SqlSugar.IOC;
|
|
|
|
|
|
using System.Data;
|
2026-06-01 13:42:40 +08:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
namespace QYZH.InteractiveMagazine.Repository
|
2026-06-01 13:42:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// 数据仓库类
|
2026-06-01 13:42:40 +08:00
|
|
|
|
/// </summary>
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
public class BaseRepository<T> : SimpleClient<T> where T : class, new()
|
2026-06-01 13:42:40 +08:00
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
private readonly ILogger<BaseRepository<T>> _logger;
|
|
|
|
|
|
public BaseRepository(ISqlSugarClient context = null) : base(context)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context = DbScoped.SugarScope;
|
|
|
|
|
|
_logger = ServiceContext.GetService<ILogger<BaseRepository<T>>>();
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
#region add
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public IInsertable<T> Insertable(T t)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Insertable(t);
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
#endregion add
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
#region update
|
|
|
|
|
|
public IUpdateable<T> Updateable(T t)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Updateable(t);
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public IUpdateable<T> Updateable()
|
2026-06-01 13:42:40 +08:00
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
return Context.Updateable<T>();
|
2026-06-01 13:42:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public IUpdateable<T1> Updateable<T1>() where T1 : class, new()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Updateable<T1>();
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据指定条件更新指定列 eg:Update(new SysUser(){ Status = 1 }, it => new { it.Status }, f => f.Userid == 1));
|
|
|
|
|
|
/// 只更新Status列,条件是包含
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="entity">实体类</param>
|
|
|
|
|
|
/// <param name="expression">要更新列的表达式</param>
|
|
|
|
|
|
/// <param name="where">where表达式</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> UpdateAsync(Expression<Func<T, bool>> columns, Expression<Func<T, bool>> where)
|
2026-06-01 13:42:40 +08:00
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
return await Context.Updateable<T>().SetColumns(columns).Where(where).ExecuteCommandAsync() > 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion update
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 事务 异步 无返回值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task UseTranAsync(Func<Task> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.Ado.BeginTran();//using不能少
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await action();
|
|
|
|
|
|
Context.Ado.CommitTran();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.Ado.RollbackTran();
|
|
|
|
|
|
_logger.LogError($"UseTran 异常:{ex.StackTrace},{ex.Message}");
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 事务 异步 返回bool
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> UseTranAsync(Func<Task<bool>> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.Ado.BeginTran();//using不能少
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await action();
|
|
|
|
|
|
if (result)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.Ado.CommitTran();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.Ado.RollbackTran();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.Ado.RollbackTran();
|
|
|
|
|
|
_logger.LogError($"UseTran 异常:{ex.StackTrace},{ex.Message}");
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
#region delete
|
|
|
|
|
|
public IDeleteable<T> Deleteable()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Deleteable<T>();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion delete
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
#region query
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public bool Any(Expression<Func<T, bool>> expression)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T>().Any(expression);
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public ISugarQueryable<T> Queryable()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ISugarQueryable<T1> Queryable<T1>()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T1>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据条件表达式查询单条数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="expression">表达式</param>
|
|
|
|
|
|
/// <returns>泛型实体</returns>
|
|
|
|
|
|
public Task<R> GetByIdAsync<R>(Expression<Func<T, bool>> expression) where R : class
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T>().Where(expression).Select<R>().FirstAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<R> GetByExpressionAsync<R>(Expression<Func<T, bool>> expression) where R : class
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T>().Where(expression).Select<R>().FirstAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<List<T2>> GetListByExpression<T2>(Expression<Func<T, bool>> expression) where T2 : class
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T>().Where(expression).Select<T2>().ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据条件查询分页数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="where"></param>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public PageListModel<T> GetPages(Expression<Func<T, bool>> where, PageQueryModel parm)
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = Context.Queryable<T>().Where(where);
|
|
|
|
|
|
|
|
|
|
|
|
return source.ToPage(parm);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分页获取数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="where">条件表达式</param>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <param name="order"></param>
|
|
|
|
|
|
/// <param name="orderEnum"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public PageListModel<T> GetPages(Expression<Func<T, bool>> where, PageQueryModel parm, Expression<Func<T, object>> order, OrderByType orderEnum = OrderByType.Asc)
|
|
|
|
|
|
{
|
|
|
|
|
|
var source = Context
|
|
|
|
|
|
.Queryable<T>()
|
|
|
|
|
|
.Where(where)
|
|
|
|
|
|
.OrderByIF(orderEnum == OrderByType.Asc, order, OrderByType.Asc)
|
|
|
|
|
|
.OrderByIF(orderEnum == OrderByType.Desc, order, OrderByType.Desc);
|
|
|
|
|
|
|
|
|
|
|
|
return source.ToPage(parm);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PageListModel<T> GetPages(Expression<Func<T, bool>> where, PageQueryModel parm, Expression<Func<T, object>> order, string orderByType)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询所有数据(无分页,请慎用)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<T> GetAll(bool useCache = false, int cacheSecond = 3600)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<T>().WithCacheIF(useCache, cacheSecond).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion query
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 此方法不带output返回值
|
|
|
|
|
|
/// var list = new List<SugarParameter>();
|
|
|
|
|
|
/// list.Add(new SugarParameter(ParaName, ParaValue)); input
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="procedureName"></param>
|
|
|
|
|
|
/// <param name="parameters"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public DataTable UseStoredProcedureToDataTable(string procedureName, List<SugarParameter> parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 带output返回值
|
|
|
|
|
|
/// var list = new List<SugarParameter>();
|
|
|
|
|
|
/// list.Add(new SugarParameter(ParaName, ParaValue, true)); output
|
|
|
|
|
|
/// list.Add(new SugarParameter(ParaName, ParaValue)); input
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="procedureName"></param>
|
|
|
|
|
|
/// <param name="parameters"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public (DataTable, List<SugarParameter>) UseStoredProcedureToTuple(string procedureName, List<SugarParameter> parameters)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = (Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters), parameters);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2026-06-01 13:42:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// 分页查询扩展
|
2026-06-01 13:42:40 +08:00
|
|
|
|
/// </summary>
|
2026-06-02 14:10:43 +08:00
|
|
|
|
public static class QueryableExtension
|
2026-06-01 13:42:40 +08:00
|
|
|
|
{
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 读取列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="source">查询表单式</param>
|
|
|
|
|
|
/// <param name="parm">分页参数</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static PageListModel<T> ToPage<T>(this ISugarQueryable<T> source, PageQueryModel parm)
|
|
|
|
|
|
{
|
|
|
|
|
|
var page = new PageListModel<T>();
|
|
|
|
|
|
var total = 0;
|
|
|
|
|
|
page.PageSize = parm.PageSize;
|
|
|
|
|
|
page.PageIndex = parm.PageIndex;
|
|
|
|
|
|
if (string.IsNullOrEmpty(parm.Sort))
|
|
|
|
|
|
{
|
|
|
|
|
|
source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc);
|
|
|
|
|
|
}
|
|
|
|
|
|
page.Result = source
|
|
|
|
|
|
//.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}")
|
|
|
|
|
|
.ToPageList(parm.PageIndex, parm.PageSize, ref total);
|
|
|
|
|
|
page.TotalNum = total;
|
|
|
|
|
|
return page;
|
|
|
|
|
|
}
|
2026-06-01 17:59:23 +08:00
|
|
|
|
|
2026-06-02 14:10:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 转指定实体类Dto
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <typeparam name="T2"></typeparam>
|
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static PageListModel<T2> ToPage<T, T2>(this ISugarQueryable<T> source, PageQueryModel parm)
|
|
|
|
|
|
{
|
|
|
|
|
|
var page = new PageListModel<T2>();
|
|
|
|
|
|
var total = 0;
|
|
|
|
|
|
page.PageSize = parm.PageSize;
|
|
|
|
|
|
page.PageIndex = parm.PageIndex;
|
|
|
|
|
|
if (string.IsNullOrEmpty(parm.Sort))
|
|
|
|
|
|
{
|
|
|
|
|
|
source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc);
|
|
|
|
|
|
}
|
|
|
|
|
|
var result = source
|
|
|
|
|
|
//.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}")
|
|
|
|
|
|
.ToPageList(parm.PageIndex, parm.PageSize, ref total);
|
|
|
|
|
|
|
|
|
|
|
|
page.TotalNum = total;
|
|
|
|
|
|
page.Result = result.Adapt<List<T2>>();
|
|
|
|
|
|
return page;
|
|
|
|
|
|
}
|
2026-06-01 17:59:23 +08:00
|
|
|
|
}
|
2026-06-02 14:10:43 +08:00
|
|
|
|
}
|