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(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(得到一个更加灵活的查询对象) /// /// 得到一个更加灵活的查询对象 /// /// public ISugarQueryable Queryable(Expression> joinExpression) { return this._context.Queryable(joinExpression); } #endregion #region Queryable(得到一个更加灵活的查询对象) /// /// 得到一个更加灵活的查询对象 /// /// /// /// /// /// /// public ISugarQueryable Queryable(Expression> joinExpression) { return this._context.Queryable(joinExpression); } #endregion #region Queryable(得到一个更加灵活的查询对象) /// /// 得到一个更加灵活的查询对象 /// /// /// /// /// /// /// /// public ISugarQueryable Queryable(Expression> joinExpression) { return this._context.Queryable(joinExpression); } #endregion #region Dispose(关闭) /// /// 关闭 /// public void Dispose() { this._context.Dispose(); } #endregion #region Update(按字段批量修改一组对象) /// /// 按字段批量修改一组对象 /// /// /// /// public int Update(List updateObjs, Expression> columns) where T : class, new() { return this._context.Updateable(updateObjs).UpdateColumns(columns).ExecuteCommand(); } #endregion #region UpdateQueue(按字段批量修改一组对象) /// /// 按字段批量修改一组对象 /// /// /// /// public void UpdateQueue(List updateObjs, Expression> columns) where T : class, new() { this._context.Updateable(updateObjs).UpdateColumns(columns).AddQueue(); } #endregion #region Update(批量修改一组对象) /// /// 按字段批量修改一组对象 /// /// /// /// public int Update(List updateObjs) where T : class, new() { return this._context.Updateable(updateObjs).ExecuteCommand(); } #endregion #region Delete(根据表达式删除) /// /// 根据表达式删除 /// /// public int Delete(Expression> whereExpression) where T:class,new() { return this._context.Deleteable().Where(whereExpression).ExecuteCommand(); } #endregion #region DeleteQueue(根据表达式删除) /// /// 根据表达式删除 /// /// /// public void DeleteQueue(Expression> whereExpression) where T : class, new() { this._context.Deleteable().Where(whereExpression).AddQueue(); } #endregion #region Update(修改实体) /// /// 修改实体 /// /// /// public void Update(T t) where T:class,new() { this._context.Updateable(t).ExecuteCommand(); } #endregion #region Update(按字段修改,满足条件的数据,批量修改的补充) /// /// 按字段修改,满足条件的数据,批量修改的补充。 /// 例子:Update(it => new WorkProcess { Remark = "测试批量修改",SystemType = 0 },p => p.WorkProcessId ==Guid.Parse("7BDDBBD3-B1CD-4C25-93BA-D7BF22032108")); /// /// 要修改的列 /// 要修改的条件 public int Update(Expression> columns, Expression> whereExpression) where T:class,new() { return this._context.Updateable().SetColumns(columns).Where(whereExpression).ExecuteCommand(); } #endregion #region UpdateQueue(增加允许跨仓储调用修改方法) /// /// 增加允许跨仓储调用修改方法 /// /// /// /// public void UpdateQueue(Expression> columns, Expression> whereExpression) where T : class, new() { this._context.Updateable().SetColumns(columns).Where(whereExpression).AddQueue(); } #endregion #region UpdateQueue(添加修改的队列) /// /// 添加修改的队列 /// /// /// public void UpdateQueue(T t) where T:class,new() { this._context.Updateable(t).AddQueue(); } #endregion #region UpdateQueue(修改的队列) /// /// 修改的队列 /// /// /// public void UpdateQueue(List list) where T:class,new() { this._context.Updateable(list).AddQueue(); } #endregion #region AddQueue(按照列添加) /// /// 按照列添加 /// /// /// /// public void AddQueue(T t, Expression> columns) where T : class, new() { this._context.Insertable(t).InsertColumns(columns).AddQueue(); } #endregion #region UseTran(添加一个委托的方式执行事务) /// /// 添加一个委托的方式执行事务 /// /// public void UseTran(Action action) { var result = this._context.UseTran(action); if (!result.IsSuccess) { throw result.ErrorException; } } #endregion #region Add(添加) /// /// 按照列添加 /// /// /// /// public void Add(T t) where T:class,new() { this._context.Insertable(t).ExecuteCommand(); } #endregion #region TruncateTable(清空表数据) /// /// 清空表数据 /// /// /// public void TruncateTable() where T : class, new() { this._context.DbMaintenance.TruncateTable(); } #endregion #region AddQueue(加入数据库队列) /// /// 加入数据库队列 /// /// /// public void AddQueue(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(添加) /// /// 批量添加 /// /// /// /// public void Add(List t) where T : class, new() { this._context.Insertable(t).ExecuteCommand(); } #endregion #region GetSqlHashCode(得到当前的sql对象hashcode) /// /// 得到当前的sql对象hashcode /// /// public int GetSqlHashCode() { return this._context.GetHashCode(); } #endregion #region Queryable(得到一个更加灵活的查询对象) /// /// 得到一个更加灵活的查询对象 /// /// /// /// /// /// public ISugarQueryable Queryable(Expression> joinExpression) { return this._context.Queryable(joinExpression); } #endregion #region UnionAll(Union表操作) /// /// Union表操作 /// /// /// /// public ISugarQueryable UnionAll(params ISugarQueryable[] queryables) where T : class, new() { return this._context.UnionAll(queryables); } #endregion #region Queryable(得到一个更加灵活的查询对象) /// /// 得到一个更加灵活的查询对象 /// /// /// /// public ISugarQueryable Queryable(string shortName = "") where T : class, new() { if (!string.IsNullOrWhiteSpace(shortName)) { return this._context.Queryable().AS(shortName); } return this._context.Queryable(); } #endregion #region SaveQueue(一起执行提交) /// /// 一起执行提交 /// public int SaveQueue() { return this._context.SaveQueues(); } #endregion #region ClearQueue(清除数据库队列) /// /// 清除数据库队列 /// public void ClearQueue() { this._context.Queues.Clear(); } #endregion #region IsExist(根据表达式是否存在) public bool IsExist(Expression> whereExpression) { return _context.Queryable().Where(whereExpression).Any(); } #endregion #region Count(得到数量) /// /// 得到数量 /// /// /// /// public int Count(Expression> predicate) { return this._context.Queryable().Where(predicate).Count(); } #endregion #region AddQueue(按照列添加) /// /// 按照列添加 /// /// /// /// public void AddQueue(T t) where T : class, new() { this._context.Insertable(t).AddQueue(); } #endregion #region AddQueue(批量添加) /// /// 批量添加 /// /// /// public void AddQueue(List list) where T : class, new() { this._context.Insertable(list).AddQueue(); } #endregion #region 禁止参数转换成NVarchar /// /// 禁止参数转换成NVarchar /// public void DisableNVarchar() { this._context.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { MySqlDisableNarvchar = true }; } #endregion #region 启用参数转换成NVarchar /// /// 启用参数转换成NVarchar /// public void EnableNVarchar() { this._context.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { MySqlDisableNarvchar = false }; } #endregion } }