using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MyCode.Project.Domain.Repositories; using System.Data.Entity; using System.Linq.Expressions; using System.Reflection; using System.Threading; using MyCode.Project.Infrastructure.Common; using MyCode.Project.Infrastructure.Search; using System.Data.SqlClient; using MyCode.Project.Infrastructure; using MyCode.Project.Infrastructure.Cache; using System.Runtime.Remoting.Messaging; using MyCode.Project.Repositories.Common; using SqlSugar; using MyCode.Project.Infrastructure.Exceptions; using Newtonsoft.Json; using MyCode.Project.Domain.Config; using MyCode.Project.Infrastructure.UnityExtensions; using System.Data; namespace MyCode.Project.Repositories.Common { public class WMSRepository :IDisposable { #region 初始化 private WMSSqlSugarClient _context; public WMSRepository(WMSSqlSugarClient context) { this._context = context; //this._context.Aop.OnExecutingChangeSql = (sql, pars) => //SQL执行前 可以修改SQL //{ // //处理mysql 的行锁 // if (sql.IndexOf(SqlWith.no) > 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 Dispose(关闭) /// /// 关闭 /// public void Dispose() { this._context.Dispose(); } #endregion #region SelectListPage(查询分页) /// /// 例子 "select * from table where id=@id and name=@name",new {id=1,name="a"} /// public PageResult SelectListPage(string sql, int pageIndex, int pageSize, string order, object parameters = null) where T : class, new() { if (string.IsNullOrEmpty(order)) { throw new BaseException("排序字段不可空"); } var result = new PageResult(); int totalNum = 0; result.DataList = this._context.SqlQueryable(sql).AddParameters(parameters).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNum); result.TotalCount = totalNum; return result; } #endregion #region SelectListPage(查询分页) public PageResult SelectListPage(string sql, SearchCondition condition,int pageIndex,int pageSize, string order) where T : class, new() { var conditonModel = condition.BuildConditionSql(); return SelectListPage(sql + " where " + conditonModel.Sql, pageIndex, pageSize, order, conditonModel.ListParameter); } #endregion #region SelectFirst(用SQL返回单条记录) /// /// 例子 "select * from table where id=@id and name=@name",new {id=1,name="a"} /// public T SelectFirst(string sql, object parameters=null) { return this._context.Ado.SqlQuerySingle(sql, parameters); } #endregion #region SelectFirst(用SQL返回单条记录) public T SelectFirst(string sql, SearchCondition searchCondition) { var where = searchCondition.BuildConditionSql(); return this._context.Ado.SqlQuerySingle(sql + " where " + where.Sql, where.ListParameter); } #endregion #region SelectList(用SQL返回多条记录) /// /// 例子 "select * from table where id=@id and name=@name",new {id=1,name="a"} /// public List SelectList(string sql,object parameters = null) { return this._context.Ado.SqlQuery(sql, parameters); } #endregion #region SelectList(用SQL + 条件返回多条记录) public List SelectList(string strSql, SearchCondition where) { if (where == null) { return SelectList(strSql); } var whereSql = where.BuildConditionSql(); var sql = strSql + " where " + whereSql.Sql; return this.SelectList(sql,whereSql.ListParameter); } #endregion #region SelectList(列表) public List SelectList(Expression> whereExpression) where T : class ,new() { return _context.Queryable().Where(whereExpression).ToList(); } #endregion #region Count(得到数量) public int Count(Expression> predicate) where T:class { return this._context.Queryable().Where(predicate).Count(); } #endregion #region IsExist(根据表达式是否存在) public bool IsExist(Expression> whereExpression) { return _context.Queryable().Where(whereExpression).With(SqlWith.NoLock).Any(); } #endregion #region GetSqlHashCode(得到当前的sql对象hashcode) /// /// 得到当前的sql对象hashcode /// /// public int GetSqlHashCode() { return this._context.GetHashCode(); } #endregion #region Queryable(得到一个更加灵活的查询对象) /// /// 得到一个更加灵活的查询对象 /// /// public ISugarQueryable Queryable() where T:class,new() { return this._context.Queryable().With(SqlWith.NoLock); } #endregion #region DataTable public DataTable GetDataTable(string sql, object parameters = null) { return this._context.Ado.GetDataTable(sql, parameters); } #endregion } }