468 lines
16 KiB
C#
468 lines
16 KiB
C#
using MyCode.Project.Domain.Config;
|
||
using MyCode.Project.Domain.Repositories;
|
||
using MyCode.Project.Infrastructure.Common;
|
||
using MyCode.Project.Infrastructure.Exceptions;
|
||
using MyCode.Project.Repositories.Common;
|
||
using Newtonsoft.Json;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace MyCode.Project.Repositories
|
||
{
|
||
public class Repository : IDisposable,IRepository
|
||
{
|
||
|
||
#region 初始化
|
||
private MyCodeSqlSugarClient _context;
|
||
|
||
public Repository(MyCodeSqlSugarClient context)
|
||
{
|
||
this._context = context;
|
||
|
||
this._context.Aop.OnExecutingChangeSql = (sql, pars) => //SQL执行前 可以修改SQL
|
||
{
|
||
//处理mysql 的行锁
|
||
if (sql.IndexOf(SqlWith.UpdLock) > 0)
|
||
{
|
||
sql = sql.Replace(SqlWith.UpdLock, "");
|
||
sql = sql + " for update";
|
||
}
|
||
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
|
||
};
|
||
|
||
this._context.Aop.OnLogExecuted = (sql, pars) =>
|
||
{
|
||
|
||
//得到总共执行时间
|
||
var executeSecond = this._context.Ado.SqlExecutionTime.TotalSeconds;
|
||
|
||
if (SystemConfig.IfOutputSql)
|
||
{
|
||
LogHelper.Info($"Sql:{sql}{Environment.NewLine}参数:{JsonConvert.SerializeObject(pars)}");
|
||
}
|
||
|
||
if (executeSecond > 1)
|
||
{
|
||
var slowLog = $"发现慢sql,执行时长:{executeSecond}{Environment.NewLine}Sql:{sql}{Environment.NewLine}参数:{JsonConvert.SerializeObject(pars)}";
|
||
|
||
LogHelper.Info(slowLog);
|
||
|
||
DingDingHelper.SendMsg($"发现慢sql,执行时长:{executeSecond},执行Sql:{sql}");
|
||
}
|
||
|
||
|
||
};
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region Queryable(得到一个更加灵活的查询对象)
|
||
/// <summary>
|
||
/// 得到一个更加灵活的查询对象
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public ISugarQueryable<T, T2> Queryable<T, T2>(Expression<Func<T, T2, object[]>> joinExpression)
|
||
{
|
||
return this._context.Queryable(joinExpression);
|
||
}
|
||
#endregion
|
||
|
||
#region Queryable(得到一个更加灵活的查询对象)
|
||
/// <summary>
|
||
/// 得到一个更加灵活的查询对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="T2"></typeparam>
|
||
/// <typeparam name="T3"></typeparam>
|
||
/// <typeparam name="T4"></typeparam>
|
||
/// <param name="joinExpression"></param>
|
||
/// <returns></returns>
|
||
public ISugarQueryable<T, T2, T3, T4> Queryable<T, T2, T3, T4>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression)
|
||
{
|
||
return this._context.Queryable(joinExpression);
|
||
}
|
||
#endregion
|
||
|
||
#region Queryable(得到一个更加灵活的查询对象)
|
||
/// <summary>
|
||
/// 得到一个更加灵活的查询对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="T2"></typeparam>
|
||
/// <typeparam name="T3"></typeparam>
|
||
/// <typeparam name="T4"></typeparam>
|
||
/// <typeparam name="T5"></typeparam>
|
||
/// <param name="joinExpression"></param>
|
||
/// <returns></returns>
|
||
public ISugarQueryable<T, T2, T3, T4,T5> Queryable<T, T2, T3, T4,T5>(Expression<Func<T, T2, T3, T4,T5, object[]>> joinExpression)
|
||
{
|
||
return this._context.Queryable(joinExpression);
|
||
}
|
||
#endregion
|
||
|
||
#region Dispose(关闭)
|
||
/// <summary>
|
||
/// 关闭
|
||
/// </summary>
|
||
public void Dispose()
|
||
{
|
||
this._context.Dispose();
|
||
}
|
||
#endregion
|
||
|
||
#region Update(按字段批量修改一组对象)
|
||
/// <summary>
|
||
/// 按字段批量修改一组对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="updateObjs"></param>
|
||
/// <param name="columns"></param>
|
||
public int Update<T>(List<T> updateObjs, Expression<Func<T, object>> columns) where T : class, new()
|
||
{
|
||
return this._context.Updateable<T>(updateObjs).UpdateColumns(columns).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region UpdateQueue(按字段批量修改一组对象)
|
||
/// <summary>
|
||
/// 按字段批量修改一组对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="updateObjs"></param>
|
||
/// <param name="columns"></param>
|
||
public void UpdateQueue<T>(List<T> updateObjs, Expression<Func<T, object>> columns) where T : class, new()
|
||
{
|
||
this._context.Updateable<T>(updateObjs).UpdateColumns(columns).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region Update(批量修改一组对象)
|
||
/// <summary>
|
||
/// 按字段批量修改一组对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="updateObjs"></param>
|
||
/// <param name="columns"></param>
|
||
public int Update<T>(List<T> updateObjs) where T : class, new()
|
||
{
|
||
return this._context.Updateable<T>(updateObjs).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region Delete(根据表达式删除)
|
||
/// <summary>
|
||
/// 根据表达式删除
|
||
/// </summary>
|
||
/// <param name="whereExpression"></param>
|
||
public int Delete<T>(Expression<Func<T, bool>> whereExpression) where T:class,new()
|
||
{
|
||
return this._context.Deleteable<T>().Where(whereExpression).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region DeleteQueue(根据表达式删除)
|
||
/// <summary>
|
||
/// 根据表达式删除
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="whereExpression"></param>
|
||
public void DeleteQueue<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
|
||
{
|
||
this._context.Deleteable<T>().Where(whereExpression).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region Update(修改实体)
|
||
/// <summary>
|
||
/// 修改实体
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
public void Update<T>(T t) where T:class,new()
|
||
{
|
||
this._context.Updateable<T>(t).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region Update(按字段修改,满足条件的数据,批量修改的补充)
|
||
/// <summary>
|
||
/// 按字段修改,满足条件的数据,批量修改的补充。
|
||
/// 例子:Update(it => new WorkProcess { Remark = "测试批量修改",SystemType = 0 },p => p.WorkProcessId ==Guid.Parse("7BDDBBD3-B1CD-4C25-93BA-D7BF22032108"));
|
||
/// </summary>
|
||
/// <param name="columns">要修改的列</param>
|
||
/// <param name="whereExpression">要修改的条件</param>
|
||
public int Update<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression) where T:class,new()
|
||
{
|
||
return this._context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region UpdateQueue(增加允许跨仓储调用修改方法)
|
||
/// <summary>
|
||
/// 增加允许跨仓储调用修改方法
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="updateObjs"></param>
|
||
/// <param name="columns"></param>
|
||
public void UpdateQueue<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression) where T : class, new()
|
||
{
|
||
|
||
this._context.Updateable<T>().SetColumns(columns).Where(whereExpression).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region UpdateQueue(添加修改的队列)
|
||
/// <summary>
|
||
/// 添加修改的队列
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
public void UpdateQueue<T>(T t) where T:class,new()
|
||
{
|
||
this._context.Updateable<T>(t).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region UpdateQueue(修改的队列)
|
||
/// <summary>
|
||
/// 修改的队列
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="list"></param>
|
||
public void UpdateQueue<T>(List<T> list) where T:class,new()
|
||
{
|
||
this._context.Updateable<T>(list).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(按照列添加)
|
||
/// <summary>
|
||
/// 按照列添加
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
/// <param name="columns"></param>
|
||
public void AddQueue<T>(T t, Expression<Func<T, object>> columns) where T : class, new()
|
||
{
|
||
this._context.Insertable(t).InsertColumns(columns).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region UseTran(添加一个委托的方式执行事务)
|
||
/// <summary>
|
||
/// 添加一个委托的方式执行事务
|
||
/// </summary>
|
||
/// <param name="action"></param>
|
||
public void UseTran(Action action)
|
||
{
|
||
var result = this._context.UseTran(action);
|
||
|
||
if (!result.IsSuccess) { throw result.ErrorException; }
|
||
}
|
||
#endregion
|
||
|
||
#region Add(添加)
|
||
/// <summary>
|
||
/// 按照列添加
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
/// <param name="columns"></param>
|
||
public void Add<T>(T t) where T:class,new()
|
||
{
|
||
this._context.Insertable(t).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region TruncateTable(清空表数据)
|
||
/// <summary>
|
||
/// 清空表数据
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
public void TruncateTable<T>() where T : class, new()
|
||
{
|
||
this._context.DbMaintenance.TruncateTable<T>();
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(加入数据库队列)
|
||
/// <summary>
|
||
/// 加入数据库队列
|
||
/// </summary>
|
||
/// <param name="instance"></param>
|
||
/// <param name="tablename"></param>
|
||
public void AddQueue<T>(T instance, string tablename = null) where T:class,new()
|
||
{
|
||
if (tablename == null)
|
||
{
|
||
this._context.Insertable(instance).AddQueue();
|
||
return;
|
||
}
|
||
|
||
this._context.Insertable(instance).AS(tablename).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region Add(添加)
|
||
/// <summary>
|
||
/// 批量添加
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
/// <param name="columns"></param>
|
||
public void Add<T>(List<T> t) where T : class, new()
|
||
{
|
||
this._context.Insertable(t).ExecuteCommand();
|
||
}
|
||
#endregion
|
||
|
||
#region GetSqlHashCode(得到当前的sql对象hashcode)
|
||
/// <summary>
|
||
/// 得到当前的sql对象hashcode
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetSqlHashCode()
|
||
{
|
||
return this._context.GetHashCode();
|
||
}
|
||
#endregion
|
||
|
||
#region Queryable(得到一个更加灵活的查询对象)
|
||
/// <summary>
|
||
/// 得到一个更加灵活的查询对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="T2"></typeparam>
|
||
/// <typeparam name="T3"></typeparam>
|
||
/// <param name="joinExpression"></param>
|
||
/// <returns></returns>
|
||
public ISugarQueryable<T, T2, T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression)
|
||
{
|
||
return this._context.Queryable(joinExpression);
|
||
}
|
||
#endregion
|
||
|
||
#region UnionAll(Union表操作)
|
||
/// <summary>
|
||
/// Union表操作
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="queryables"></param>
|
||
/// <returns></returns>
|
||
public ISugarQueryable<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
|
||
{
|
||
return this._context.UnionAll<T>(queryables);
|
||
}
|
||
#endregion
|
||
|
||
#region Queryable(得到一个更加灵活的查询对象)
|
||
/// <summary>
|
||
/// 得到一个更加灵活的查询对象
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="shortName"></param>
|
||
/// <returns></returns>
|
||
public ISugarQueryable<T> Queryable<T>(string shortName = "") where T : class, new()
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(shortName))
|
||
{
|
||
return this._context.Queryable<T>().AS(shortName);
|
||
}
|
||
return this._context.Queryable<T>();
|
||
}
|
||
#endregion
|
||
|
||
#region SaveQueue(一起执行提交)
|
||
/// <summary>
|
||
/// 一起执行提交
|
||
/// </summary>
|
||
public int SaveQueue()
|
||
{
|
||
return this._context.SaveQueues();
|
||
}
|
||
#endregion
|
||
|
||
#region ClearQueue(清除数据库队列)
|
||
/// <summary>
|
||
/// 清除数据库队列
|
||
/// </summary>
|
||
public void ClearQueue()
|
||
{
|
||
this._context.Queues.Clear();
|
||
}
|
||
#endregion
|
||
|
||
#region IsExist(根据表达式是否存在)
|
||
public bool IsExist<T>(Expression<Func<T, bool>> whereExpression)
|
||
{
|
||
return _context.Queryable<T>().Where(whereExpression).Any();
|
||
}
|
||
#endregion
|
||
|
||
#region Count(得到数量)
|
||
/// <summary>
|
||
/// 得到数量
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="predicate"></param>
|
||
/// <returns></returns>
|
||
public int Count<T>(Expression<Func<T, bool>> predicate)
|
||
{
|
||
return this._context.Queryable<T>().Where(predicate).Count();
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(按照列添加)
|
||
/// <summary>
|
||
/// 按照列添加
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="t"></param>
|
||
/// <param name="columns"></param>
|
||
public void AddQueue<T>(T t) where T : class, new()
|
||
{
|
||
this._context.Insertable(t).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region AddQueue(批量添加)
|
||
/// <summary>
|
||
/// 批量添加
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="list"></param>
|
||
public void AddQueue<T>(List<T> list) where T : class, new()
|
||
{
|
||
this._context.Insertable(list).AddQueue();
|
||
}
|
||
#endregion
|
||
|
||
#region 禁止参数转换成NVarchar
|
||
/// <summary>
|
||
/// 禁止参数转换成NVarchar
|
||
/// </summary>
|
||
public void DisableNVarchar()
|
||
{
|
||
this._context.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { MySqlDisableNarvchar = true };
|
||
}
|
||
#endregion
|
||
|
||
#region 启用参数转换成NVarchar
|
||
/// <summary>
|
||
/// 启用参数转换成NVarchar
|
||
/// </summary>
|
||
public void EnableNVarchar()
|
||
{
|
||
this._context.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { MySqlDisableNarvchar = false };
|
||
}
|
||
#endregion
|
||
}
|
||
}
|
||
|