You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
22 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Mesnac.HighWay.ATC.Common;
using Mesnac.HighWay.ATC.SqlSugar;
using SqlSugar;
namespace Mesnac.HighWay.ATC.SqlSugar.serviceImpl
{
public class BaseServices<TEntity> : IBaseServices<TEntity> where TEntity : class, new()
{
private SqlSugarClient _dbBase;
private SqlSugarClient _db
{
get
{
try
{
_dbBase = new SqlSugarClient(new ConnectionConfig
{
//ConnectionString = "server = 127.0.0.1; uid = sa; pwd = 123456; database = JYD_shengjie",
ConnectionString = "server = 192.168.0.254; uid = sa; pwd = 123456; database = ATJ_HS_DB",
// ConnectionString = "server = 127.0.0.1; uid = sa; pwd = 123456; database = ATJ_HS_DB",
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_dbBase.Aop.OnLogExecuting = delegate (string sql, SugarParameter[] pars)
{
Parallel.For(0, 1, (Action<int>)delegate
{
string text = "";
string[] array = new string[2]
{
GetParas(pars),
"【SQL语句】" + sql
};
foreach (string text2 in array)
{
text += text2;
}
//Console.WriteLine("执行sql" + text);
});
};
}
catch (Exception ex)
{
LogHelper.Error("异常:" + ex.ToString());
Console.WriteLine(ex.Message);
return null;
}
return _dbBase;
}
}
internal SqlSugarClient Db => _db;
private string GetParas(SugarParameter[] pars)
{
string key = "【SQL参数】";
foreach (SugarParameter param in pars)
{
key += $"{param.ParameterName}:{param.Value}\n";
}
return key;
}
public async Task<TEntity> QuerySingle(Expression<Func<TEntity, bool>> whereExpression)
{
return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).SingleAsync();
}
public async Task<TEntity> QueryById(object objId)
{
return await _db.Queryable<TEntity>().In<object>(objId).SingleAsync();
}
public async Task<TEntity> QueryById(object objId, bool blnUseCache = false)
{
return await _db.Queryable<TEntity>().WithCacheIF(blnUseCache).In<object>(objId)
.SingleAsync();
}
public async Task<List<TEntity>> QueryByIDs(object[] lstIds)
{
return await _db.Queryable<TEntity>().In(lstIds).ToListAsync();
}
public async Task<int> Add(TEntity entity)
{
try
{
return await _db.Insertable(entity).ExecuteCommandAsync();
}
catch (Exception ex)
{
return 0;
}
}
public int Add1(TEntity entity)
{
try
{
return _db.Insertable(entity).ExecuteCommand();
}
catch (Exception)
{
return 0;
}
}
public async Task<int> AddLog(TEntity entity)
{
try
{
IInsertable<TEntity> insert = _db.Insertable(entity);
return await insert.ExecuteReturnIdentityAsync();
}
catch (Exception)
{
return 0;
}
}
public async Task<int> Add(List<TEntity> listEntity)
{
return await _db.Insertable(listEntity.ToArray()).ExecuteCommandAsync();
}
public async Task<bool> Update(TEntity entity)
{
return await _db.Updateable(entity).ExecuteCommandHasChangeAsync();
}
public bool Update1(TEntity entity)
{
return _db.Updateable(entity).ExecuteCommandHasChange();
}
public async Task<bool> Update(TEntity entity, string strWhere)
{
return await _db.Updateable(entity).Where(strWhere).ExecuteCommandHasChangeAsync();
}
public async Task<bool> Update(string strSql, SugarParameter[] parameters = null)
{
return await _db.Ado.ExecuteCommandAsync(strSql, parameters) > 0;
}
public async Task<bool> Update(object operateAnonymousObjects)
{
return await _db.Updateable<TEntity>(operateAnonymousObjects).ExecuteCommandAsync() > 0;
}
public bool Update1(object operateAnonymousObjects)
{
return _db.Updateable<TEntity>(operateAnonymousObjects).ExecuteCommand() > 0;
}
public async Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string strWhere = "")
{
IUpdateable<TEntity> up = _db.Updateable(entity);
if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
{
up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
}
if (lstColumns != null && lstColumns.Count > 0)
{
up = up.UpdateColumns(lstColumns.ToArray());
}
if (!string.IsNullOrEmpty(strWhere))
{
up = up.Where(strWhere);
}
return await up.ExecuteCommandHasChangeAsync();
}
public async Task<bool> Delete(TEntity entity)
{
return await _db.Deleteable(entity).ExecuteCommandHasChangeAsync();
}
public async Task<bool> Delete(Expression<Func<TEntity, bool>> whereExpression)
{
return await _db.Deleteable<TEntity>().Where(whereExpression).ExecuteCommandHasChangeAsync();
}
public async Task<bool> DeleteById(object id)
{
return await _db.Deleteable<TEntity>(id).ExecuteCommandHasChangeAsync();
}
public async Task<bool> DeleteByIds(object[] ids)
{
return await _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync();
}
public async Task<List<TEntity>> Query()
{
return await _db.Queryable<TEntity>().ToListAsync();
}
public async Task<List<TEntity>> Query(string strWhere)
{
return await _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
}
public List<TEntity> Query1(Expression<Func<TEntity, bool>> whereExpression)
{
try
{
return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList();
}
catch (Exception)
{
return null;
}
}
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression)
{
try
{
return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
}
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
{
return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds)
.ToListAsync();
}
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
{
try
{
return await _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, (!isAsc) ? OrderByType.Desc : OrderByType.Asc).WhereIF(whereExpression != null, whereExpression)
.ToListAsync();
}
catch (Exception)
{
return null;
}
}
public async Task<TEntity> QueryFirst(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
{
try
{
return await _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, (!isAsc) ? OrderByType.Desc : OrderByType.Asc).WhereIF(whereExpression != null, whereExpression)
.FirstAsync();
}
catch (Exception)
{
return null;
}
}
public async Task<List<TEntity>> Query(string strWhere, string strOrderByFileds)
{
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere)
.ToListAsync();
}
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
{
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression)
.Take(intTop)
.ToListAsync();
}
public async Task<List<TEntity>> Query(string strWhere, int intTop, string strOrderByFileds)
{
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere)
.Take(intTop)
.ToListAsync();
}
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds)
{
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize);
}
public async Task<List<TEntity>> Query(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds)
{
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere)
.ToPageListAsync(intPageIndex, intPageSize);
}
public async Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
{
RefAsync<int> totalCount = 0;
List<TEntity> list = await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = Math.Ceiling(totalCount.ObjToDecimal2() / intPageSize.ObjToDecimal2()).ObjToInt2();
return new PageModel<TEntity>
{
dataCount = totalCount,
pageCount = pageCount,
page = intPageIndex,
PageSize = intPageSize,
data = list
};
}
public async Task<List<TResult>> QueryMuch<T, T2, T3, T4, T5, TResult>(Expression<Func<T, T2, T3, T4, T5, object[]>> joinExpression, Expression<Func<T, T2, T3, T4, T5, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
}
return await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.Where(whereLambda)
.ToListAsync();
}
public async Task<List<TResult>> QueryMuch<T, T2, T3, T4, TResult>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression, Expression<Func<T, T2, T3, T4, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
}
return await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.Where(whereLambda)
.ToListAsync();
}
public async Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
}
return await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.Where(whereLambda)
.ToListAsync();
}
public async Task<List<TResult>> QueryMuch<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
}
return await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.Where(whereLambda)
.ToListAsync();
}
public async Task<PageModel<TResult>> QueryMuchPage<T, T2, T3, T4, T5, TResult>(Expression<Func<T, T2, T3, T4, T5, object[]>> joinExpression, Expression<Func<T, T2, T3, T4, T5, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) where T : class, new()
{
RefAsync<int> totalCount = 0;
List<TResult> list = await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.WhereIF(whereLambda != null, whereLambda)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = Math.Ceiling(totalCount.ObjToDecimal2() / intPageSize.ObjToDecimal2()).ObjToInt2();
return new PageModel<TResult>
{
dataCount = totalCount,
pageCount = pageCount,
page = intPageIndex,
PageSize = intPageSize,
data = list
};
}
public async Task<PageModel<TResult>> QueryMuchPage<T, T2, T3, T4, TResult>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression, Expression<Func<T, T2, T3, T4, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) where T : class, new()
{
RefAsync<int> totalCount = 0;
List<TResult> list = await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.WhereIF(whereLambda != null, whereLambda)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = Math.Ceiling(totalCount.ObjToDecimal2() / intPageSize.ObjToDecimal2()).ObjToInt2();
return new PageModel<TResult>
{
dataCount = totalCount,
pageCount = pageCount,
page = intPageIndex,
PageSize = intPageSize,
data = list
};
}
public async Task<PageModel<TResult>> QueryMuchPage<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) where T : class, new()
{
RefAsync<int> totalCount = 0;
List<TResult> list = await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.WhereIF(whereLambda != null, whereLambda)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = Math.Ceiling(totalCount.ObjToDecimal2() / intPageSize.ObjToDecimal2()).ObjToInt2();
return new PageModel<TResult>
{
dataCount = totalCount,
pageCount = pageCount,
page = intPageIndex,
PageSize = intPageSize,
data = list
};
}
public async Task<PageModel<TResult>> QueryMuchPage<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) where T : class, new()
{
RefAsync<int> totalCount = 0;
List<TResult> list = await _db.Queryable(joinExpression).Select(selectExpression).MergeTable()
.WhereIF(whereLambda != null, whereLambda)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = Math.Ceiling(totalCount.ObjToDecimal2() / intPageSize.ObjToDecimal2()).ObjToInt2();
return new PageModel<TResult>
{
dataCount = totalCount,
pageCount = pageCount,
page = intPageIndex,
PageSize = intPageSize,
data = list
};
}
public async Task<PageModel<TResult>> QueryMuchPage<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<TResult, object>> sortExpression, Expression<Func<T, T2, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) where T : class, new()
{
RefAsync<int> totalCount = 0;
List<TResult> list = await _db.Queryable(joinExpression).WhereIF(whereLambda != null, whereLambda).Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = Math.Ceiling(totalCount.ObjToDecimal2() / intPageSize.ObjToDecimal2()).ObjToInt2();
return new PageModel<TResult>
{
dataCount = totalCount,
pageCount = pageCount,
page = intPageIndex,
PageSize = intPageSize,
data = list
};
}
public async Task<List<T>> QueryByProc<T>(string proc, List<SugarParameter> parameters) where T : class, new()
{
return await _db.Ado.UseStoredProcedure().SqlQueryAsync<T>(proc, parameters);
}
public async Task<Tuple<List<T>, List<T2>>> QueryByProc<T, T2>(string proc, List<SugarParameter> parameters) where T : class, new()
{
return await _db.Ado.UseStoredProcedure().SqlQueryAsync<T, T2>(proc, parameters);
}
public async Task<List<T>> QueryBySql<T>(string sql, List<SugarParameter> parameters) where T : class, new()
{
return await _db.Ado.SqlQueryAsync<T>(sql, parameters);
}
public List<TEntity> QueryList(Expression<Func<TEntity, bool>> whereExpression)
{
return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList();
}
public async Task<int> insertSQLAsync(string sql, List<SugarParameter> parameters)
{
try
{
return await _db.Ado.ExecuteCommandAsync(sql, parameters);
}
catch (Exception)
{
return 0;
}
}
public System.Data.DataTable QueryDatatable(Expression<Func<TEntity, bool>> whereExpression)
{
return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToDataTable();
}
public List<T> QueryBySql1<T>(string sql, List<SugarParameter> parameters) where T : class, new()
{
return _db.Ado.SqlQuery<T>(sql, parameters);
}
public SugarParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null)
{
return _db.Ado.GetParameters(obj, propertyInfo);
}
public System.Data.DataTable UseStoredProcedure(string sql, object parameters)
{
return _db.Ado.UseStoredProcedure().GetDataTable(sql, parameters);
}
}
}