shgx_tz_mom/ZR.Repository/BaseRepository.cs

358 lines
13 KiB
C#
Raw Normal View History

using Infrastructure.Extensions;
2022-11-29 11:43:39 +08:00
using Mapster;
using SqlSugar;
2021-10-11 18:05:01 +08:00
using SqlSugar.IOC;
2021-09-27 08:06:09 +08:00
using System;
using System.Collections.Generic;
2021-11-20 18:24:41 +08:00
using System.Data;
2021-09-27 08:06:09 +08:00
using System.Linq.Expressions;
2022-05-12 21:28:27 +08:00
using System.Reflection;
2021-09-27 16:07:55 +08:00
using ZR.Model;
2021-09-27 08:06:09 +08:00
namespace ZR.Repository
{
/// <summary>
/// 数据仓库类
2021-09-27 08:06:09 +08:00
/// </summary>
/// <typeparam name="T"></typeparam>
2022-03-19 08:04:08 +08:00
public class BaseRepository<T> : SimpleClient<T> where T : class, new()
2021-09-27 08:06:09 +08:00
{
2022-03-19 08:04:08 +08:00
public ITenant itenant = null;//多租户事务
2022-04-09 19:11:16 +08:00
public BaseRepository(ISqlSugarClient context = null) : base(context)
2021-09-27 08:06:09 +08:00
{
2022-03-19 08:04:08 +08:00
//通过特性拿到ConfigId
2022-05-12 21:28:27 +08:00
var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
if (configId != null)
{
Context = DbScoped.SugarScope.GetConnectionScope(configId);//根据类传入的ConfigId自动选择
}
else
2021-11-27 09:43:04 +08:00
{
2022-05-12 21:28:27 +08:00
Context = context ?? DbScoped.SugarScope.GetConnectionScope(0);//没有默认db0
2021-11-27 09:43:04 +08:00
}
2022-05-12 21:28:27 +08:00
//Context = DbScoped.SugarScope.GetConnectionScopeWithAttr<T>();
itenant = DbScoped.SugarScope;//设置租户接口
2021-09-27 08:06:09 +08:00
}
2021-10-29 13:23:22 +08:00
2021-09-27 08:06:09 +08:00
#region add
2021-11-27 09:43:04 +08:00
2021-09-27 16:07:55 +08:00
/// <summary>
/// 插入实体
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
2023-02-17 10:54:18 +08:00
public int Add(T t, bool ignoreNull = true)
2021-09-27 08:06:09 +08:00
{
2023-02-17 10:54:18 +08:00
return Context.Insertable(t).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand();
2021-09-27 08:06:09 +08:00
}
public int Insert(List<T> t)
{
2021-10-17 17:34:57 +08:00
return Context.Insertable(t).ExecuteCommand();
}
2021-11-27 09:43:04 +08:00
public int Insert(T parm, Expression<Func<T, object>> iClumns = null, bool ignoreNull = true)
{
return Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand();
}
public IInsertable<T> Insertable(T t)
{
return Context.Insertable(t);
2021-11-27 09:43:04 +08:00
}
2021-09-27 08:06:09 +08:00
#endregion add
#region update
2023-08-15 09:37:31 +08:00
//public IUpdateable<T> Updateable(T entity)
//{
// return Context.Updateable(entity);
//}
2022-10-20 18:30:05 +08:00
/// <summary>
/// 实体根据主键更新
/// </summary>
/// <param name="entity"></param>
/// <param name="ignoreNullColumns"></param>
/// <returns></returns>
2021-12-14 21:51:19 +08:00
public int Update(T entity, bool ignoreNullColumns = false)
2021-11-27 09:43:04 +08:00
{
2021-12-14 21:51:19 +08:00
return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns).ExecuteCommand();
2021-11-27 09:43:04 +08:00
}
2021-09-27 08:06:09 +08:00
2022-10-20 18:30:05 +08:00
/// <summary>
/// 实体根据主键更新指定字段
/// return Update(new SysUser(){ Status = 1 }, t => new { t.NickName, }, true);
2022-10-20 18:30:05 +08:00
/// </summary>
/// <param name="entity"></param>
/// <param name="expression"></param>
/// <param name="ignoreAllNull"></param>
/// <returns></returns>
2021-12-14 21:51:19 +08:00
public int Update(T entity, Expression<Func<T, object>> expression, bool ignoreAllNull = false)
2021-11-27 09:43:04 +08:00
{
2021-12-14 21:51:19 +08:00
return Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand();
2021-11-27 09:43:04 +08:00
}
2021-09-27 08:06:09 +08:00
2021-12-25 12:06:00 +08:00
/// <summary>
/// 根据指定条件更新指定列 egUpdate(new SysUser(){ Status = 1 }, it => new { it.Status }, f => f.Userid == 1));
2022-10-20 18:30:05 +08:00
/// 只更新Status列条件是包含
2021-12-25 12:06:00 +08:00
/// </summary>
/// <param name="entity">实体类</param>
/// <param name="expression">要更新列的表达式</param>
/// <param name="where">where表达式</param>
2021-12-25 12:06:00 +08:00
/// <returns></returns>
2021-12-14 21:51:19 +08:00
public int Update(T entity, Expression<Func<T, object>> expression, Expression<Func<T, bool>> where)
2021-11-27 09:43:04 +08:00
{
2021-12-14 21:51:19 +08:00
return Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand();
2021-11-27 09:43:04 +08:00
}
2021-09-27 08:06:09 +08:00
2021-12-25 12:06:00 +08:00
/// <summary>
/// 更新指定列 egUpdate(w => w.NoticeId == model.NoticeId, it => new SysNotice(){ Update_time = DateTime.Now, Title = "通知标题" });
/// </summary>
/// <param name="where"></param>
/// <param name="columns"></param>
/// <returns></returns>
2021-12-14 21:51:19 +08:00
public int Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> columns)
2021-09-27 08:06:09 +08:00
{
2021-12-14 21:51:19 +08:00
return Context.Updateable<T>().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand();
2021-09-27 08:06:09 +08:00
}
#endregion update
2021-11-20 18:24:41 +08:00
public DbResult<bool> UseTran(Action action)
{
2022-03-19 08:04:08 +08:00
try
{
var result = Context.Ado.UseTran(() => action());
return result;
}
catch (Exception ex)
{
Context.Ado.RollbackTran();
Console.WriteLine(ex.Message);
throw;
}
2021-11-20 18:24:41 +08:00
}
2022-03-19 08:04:08 +08:00
/// <summary>
2023-11-24 17:16:49 +08:00
/// 使用事务
2022-03-19 08:04:08 +08:00
/// </summary>
/// <param name="client"></param>
/// <param name="action">增删改查方法</param>
/// <returns></returns>
2021-11-20 18:24:41 +08:00
public DbResult<bool> UseTran(SqlSugarClient client, Action action)
{
2022-03-19 08:04:08 +08:00
try
{
var result = client.AsTenant().UseTran(() => action());
return result;
}
catch (Exception ex)
{
client.AsTenant().RollbackTran();
Console.WriteLine(ex.Message);
throw;
}
2021-11-20 18:24:41 +08:00
}
2021-09-27 08:06:09 +08:00
/// <summary>
/// 使用事务
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
2021-11-20 18:24:41 +08:00
public bool UseTran2(Action action)
{
var result = Context.Ado.UseTran(() => action());
return result.IsSuccess;
}
2021-09-27 08:06:09 +08:00
#region delete
2022-02-10 22:01:59 +08:00
public IDeleteable<T> Deleteable()
{
return Context.Deleteable<T>();
}
2021-09-27 08:06:09 +08:00
2021-11-20 18:24:41 +08:00
/// <summary>
/// 批量删除
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int Delete(object[] obj, string title = "")
2021-11-20 18:24:41 +08:00
{
return Context.Deleteable<T>().In(obj).EnableDiffLogEventIF(title.IsNotEmpty(), title).ExecuteCommand();
2021-11-20 18:24:41 +08:00
}
public int Delete(object id, string title = "")
2021-09-27 08:06:09 +08:00
{
return Context.Deleteable<T>(id).EnableDiffLogEventIF(title.IsNotEmpty(), title).ExecuteCommand();
2021-09-27 08:06:09 +08:00
}
2021-12-14 21:51:19 +08:00
public int DeleteTable()
2021-09-27 08:06:09 +08:00
{
2021-12-14 21:51:19 +08:00
return Context.Deleteable<T>().ExecuteCommand();
2021-09-27 08:06:09 +08:00
}
2022-09-21 21:43:05 +08:00
public bool Truncate()
{
return Context.DbMaintenance.TruncateTable<T>();
}
2021-09-27 08:06:09 +08:00
#endregion delete
#region query
2021-09-27 16:07:55 +08:00
public bool Any(Expression<Func<T, bool>> expression)
2021-09-27 08:06:09 +08:00
{
2021-09-27 16:07:55 +08:00
return Context.Queryable<T>().Where(expression).Any();
2021-09-27 08:06:09 +08:00
}
public ISugarQueryable<T> Queryable()
{
2021-09-27 16:07:55 +08:00
return Context.Queryable<T>();
2021-09-27 08:06:09 +08:00
}
2023-10-11 16:56:25 +08:00
public ISugarQueryable<T> Queryable(Expression<Func<T, bool>> expression)
{
return Context.Queryable<T>().Where(expression);
}
2021-09-27 08:06:09 +08:00
2021-11-27 09:43:04 +08:00
public List<T> SqlQueryToList(string sql, object obj = null)
{
return Context.Ado.SqlQuery<T>(sql, obj);
}
2021-09-27 08:06:09 +08:00
/// <summary>
/// 根据主值查询单条数据
/// </summary>
/// <param name="pkValue">主键值</param>
/// <returns>泛型实体</returns>
public T GetId(object pkValue)
{
return Context.Queryable<T>().InSingle(pkValue);
}
2021-09-27 16:07:55 +08:00
/// <summary>
/// 根据条件查询分页数据
/// </summary>
/// <param name="where"></param>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<T> GetPages(Expression<Func<T, bool>> where, PagerInfo 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>
2021-11-27 09:43:04 +08:00
public PagedInfo<T> GetPages(Expression<Func<T, bool>> where, PagerInfo parm, Expression<Func<T, object>> order, OrderByType orderEnum = OrderByType.Asc)
2021-09-27 16:07:55 +08:00
{
var source = Context
.Queryable<T>()
.Where(where)
.OrderByIF(orderEnum == OrderByType.Asc, order, OrderByType.Asc)
.OrderByIF(orderEnum == OrderByType.Desc, order, OrderByType.Desc);
2021-09-27 08:06:09 +08:00
2021-09-27 16:07:55 +08:00
return source.ToPage(parm);
}
2022-01-05 07:17:44 +08:00
public PagedInfo<T> GetPages(Expression<Func<T, bool>> where, PagerInfo parm, Expression<Func<T, object>> order, string orderByType)
{
return GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc);
}
2021-09-27 08:06:09 +08:00
/// <summary>
2021-09-27 16:07:55 +08:00
/// 查询所有数据(无分页,请慎用)
2021-09-27 08:06:09 +08:00
/// </summary>
/// <returns></returns>
2021-09-27 16:07:55 +08:00
public List<T> GetAll(bool useCache = false, int cacheSecond = 3600)
2021-09-27 08:06:09 +08:00
{
2021-09-27 16:07:55 +08:00
return Context.Queryable<T>().WithCacheIF(useCache, cacheSecond).ToList();
2021-09-27 08:06:09 +08:00
}
2021-10-17 17:34:57 +08:00
2021-09-27 08:06:09 +08:00
#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>
2021-11-20 18:24:41 +08:00
public DataTable UseStoredProcedureToDataTable(string procedureName, List<SugarParameter> parameters)
{
return Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters);
}
2021-09-27 08:06:09 +08:00
/// <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>
2021-11-20 18:24:41 +08:00
public (DataTable, List<SugarParameter>) UseStoredProcedureToTuple(string procedureName, List<SugarParameter> parameters)
{
var result = (Context.Ado.UseStoredProcedure().GetDataTable(procedureName, parameters), parameters);
return result;
}
2021-09-27 16:07:55 +08:00
}
2021-12-18 10:56:02 +08:00
/// <summary>
/// 分页查询扩展
/// </summary>
2021-09-27 16:07:55 +08:00
public static class QueryableExtension
{
/// <summary>
/// 读取列表
/// </summary>
/// <typeparam name="T"></typeparam>
2021-12-18 10:56:02 +08:00
/// <param name="source">查询表单式</param>
/// <param name="parm">分页参数</param>
2021-09-27 16:07:55 +08:00
/// <returns></returns>
public static PagedInfo<T> ToPage<T>(this ISugarQueryable<T> source, PagerInfo parm)
2021-09-27 08:06:09 +08:00
{
2021-09-27 16:07:55 +08:00
var page = new PagedInfo<T>();
2021-11-28 11:11:34 +08:00
var total = 0;
2021-09-27 16:07:55 +08:00
page.PageSize = parm.PageSize;
page.PageIndex = parm.PageNum;
2023-07-27 17:56:52 +08:00
if (parm.Sort.IsNotEmpty())
{
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")}")
2021-11-28 11:11:34 +08:00
.ToPageList(parm.PageNum, parm.PageSize, ref total);
page.TotalNum = total;
2021-09-27 16:07:55 +08:00
return page;
2021-09-27 08:06:09 +08:00
}
2021-09-27 16:07:55 +08:00
2022-11-29 11:43:39 +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 PagedInfo<T2> ToPage<T, T2>(this ISugarQueryable<T> source, PagerInfo parm)
{
var page = new PagedInfo<T2>();
var total = 0;
page.PageSize = parm.PageSize;
page.PageIndex = parm.PageNum;
2023-07-27 17:56:52 +08:00
if (parm.Sort.IsNotEmpty())
{
source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc);
}
var result = source
2023-07-27 17:56:52 +08:00
//.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}")
2022-11-29 11:43:39 +08:00
.ToPageList(parm.PageNum, parm.PageSize, ref total);
2022-11-29 11:43:39 +08:00
page.TotalNum = total;
page.Result = result.Adapt<List<T2>>();
return page;
}
2021-09-27 08:06:09 +08:00
}
}