208 lines
6.8 KiB
C#
208 lines
6.8 KiB
C#
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<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 Dispose(关闭)
|
|
/// <summary>
|
|
/// 关闭
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
this._context.Dispose();
|
|
}
|
|
#endregion
|
|
|
|
#region SelectListPage(查询分页)
|
|
/// <summary>
|
|
/// 例子 "select * from table where id=@id and name=@name",new {id=1,name="a"}
|
|
/// </summary>
|
|
|
|
public PageResult<T> SelectListPage<T>(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<T>();
|
|
|
|
int totalNum = 0;
|
|
|
|
result.DataList = this._context.SqlQueryable<T>(sql).AddParameters(parameters).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNum);
|
|
|
|
result.TotalCount = totalNum;
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region SelectListPage(查询分页)
|
|
public PageResult<T> SelectListPage<T>(string sql, SearchCondition condition,int pageIndex,int pageSize, string order) where T : class, new()
|
|
{
|
|
var conditonModel = condition.BuildConditionSql();
|
|
|
|
return SelectListPage<T>(sql + " where " + conditonModel.Sql, pageIndex, pageSize, order, conditonModel.ListParameter);
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region SelectFirst(用SQL返回单条记录)
|
|
/// <summary>
|
|
/// 例子 "select * from table where id=@id and name=@name",new {id=1,name="a"}
|
|
/// </summary>
|
|
public T SelectFirst<T>(string sql, object parameters=null)
|
|
{
|
|
return this._context.Ado.SqlQuerySingle<T>(sql, parameters);
|
|
}
|
|
#endregion
|
|
|
|
#region SelectFirst(用SQL返回单条记录)
|
|
public T SelectFirst<T>(string sql, SearchCondition searchCondition)
|
|
{
|
|
var where = searchCondition.BuildConditionSql();
|
|
|
|
return this._context.Ado.SqlQuerySingle<T>(sql + " where " + where.Sql, where.ListParameter);
|
|
}
|
|
#endregion
|
|
|
|
#region SelectList(用SQL返回多条记录)
|
|
/// <summary>
|
|
/// 例子 "select * from table where id=@id and name=@name",new {id=1,name="a"}
|
|
/// </summary>
|
|
public List<T> SelectList<T>(string sql,object parameters = null)
|
|
{
|
|
return this._context.Ado.SqlQuery<T>(sql, parameters);
|
|
}
|
|
#endregion
|
|
|
|
#region SelectList(用SQL + 条件返回多条记录)
|
|
public List<T> SelectList<T>(string strSql, SearchCondition where)
|
|
{
|
|
if (where == null)
|
|
{
|
|
return SelectList<T>(strSql);
|
|
}
|
|
var whereSql = where.BuildConditionSql();
|
|
|
|
var sql = strSql + " where " + whereSql.Sql;
|
|
|
|
return this.SelectList<T>(sql,whereSql.ListParameter);
|
|
}
|
|
#endregion
|
|
|
|
#region SelectList(列表)
|
|
public List<T> SelectList<T>(Expression<Func<T, bool>> whereExpression) where T : class ,new()
|
|
{
|
|
return _context.Queryable<T>().Where(whereExpression).ToList();
|
|
}
|
|
#endregion
|
|
|
|
#region Count(得到数量)
|
|
public int Count<T>(Expression<Func<T, bool>> predicate) where T:class
|
|
{
|
|
return this._context.Queryable<T>().Where(predicate).Count();
|
|
}
|
|
#endregion
|
|
|
|
#region IsExist(根据表达式是否存在)
|
|
public bool IsExist<T>(Expression<Func<T, bool>> whereExpression)
|
|
{
|
|
return _context.Queryable<T>().Where(whereExpression).With(SqlWith.NoLock).Any();
|
|
}
|
|
#endregion
|
|
|
|
#region GetSqlHashCode(得到当前的sql对象hashcode)
|
|
/// <summary>
|
|
/// 得到当前的sql对象hashcode
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public int GetSqlHashCode()
|
|
{
|
|
return this._context.GetHashCode();
|
|
}
|
|
#endregion
|
|
|
|
#region Queryable(得到一个更加灵活的查询对象)
|
|
/// <summary>
|
|
/// 得到一个更加灵活的查询对象
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public ISugarQueryable<T> Queryable<T>() where T:class,new()
|
|
{
|
|
return this._context.Queryable<T>().With(SqlWith.NoLock);
|
|
}
|
|
#endregion
|
|
|
|
#region DataTable
|
|
public DataTable GetDataTable(string sql, object parameters = null)
|
|
{
|
|
return this._context.Ado.GetDataTable(sql, parameters);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|