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.

758 lines
35 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using SLH.SSDMS.Common;
using SLH.SSDMS.Model.DTO;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SLH.SSDMS.Services.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=" + ConfigHelper.GetConfig("DataBaseServer") + ";Database="
+ ConfigHelper.GetConfig("DataBaseName") + ";User ID=" + ConfigHelper.GetConfig("DataBaseUser")
+ ";Password=" + ConfigHelper.GetConfig("DataBasePassword"),
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
//InitKey默认SystemTable
});
//可注释
_dbBase.Aop.OnLogExecuting = (sql, pars) => //SQL执行中事件
{
Parallel.For(0, 1, e =>
{
string SqlLog = "";
foreach (string item in new string[] { GetParas(pars), "【SQL语句】" + sql })
{
SqlLog = SqlLog + item;
}
LogHelper.Info("SqlLog:" + SqlLog);
});
};
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return _dbBase;
}
}
private string GetParas(SugarParameter[] pars)
{
string key = "【SQL参数】";
foreach (var param in pars)
{
key += $"{param.ParameterName}:{param.Value}\n";
}
return key;
}
internal SqlSugarClient Db
{
get { return _db; }
}
public async Task<TEntity> QuerySingle(Expression<Func<TEntity, bool>> whereExpression)
{
try
{
return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).SingleAsync();
}
catch (Exception ex)
{
return null;
}
}
public async Task<TEntity> QueryById(object objId)
{
return await _db.Queryable<TEntity>().In(objId).SingleAsync();
}
/// <summary>
/// 功能描述:根据ID查询一条数据
///
/// </summary>
/// <param name="objId">id必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param>
/// <param name="blnUseCache">是否使用缓存</param>
/// <returns>数据实体</returns>
public async Task<TEntity> QueryById(object objId, bool blnUseCache = false)
{
return await _db.Queryable<TEntity>().WithCacheIF(blnUseCache).In(objId).SingleAsync();
}
/// <summary>
/// 功能描述:根据ID查询数据
///
/// </summary>
/// <param name="lstIds">id列表必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param>
/// <returns>数据实体列表</returns>
public async Task<List<TEntity>> QueryByIDs(object[] lstIds)
{
return await _db.Queryable<TEntity>().In(lstIds).ToListAsync();
}
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public async Task<int> Add(TEntity entity)
{
try
{
var insert = _db.Insertable(entity);
var info = await insert.ExecuteReturnIdentityAsync();
return info;
}
catch (Exception ex)
{
return 0;
}
}
public async Task<int> AddLog(TEntity entity)
{
try
{
var insert = _db.Insertable(entity);
return await insert.ExecuteReturnIdentityAsync();
}
catch (Exception ex)
{
LogHelper.Error("插入出錯:" + ex.Message);
return 0;
}
}
/// <summary>
/// 批量插入实体(速度快)
/// </summary>
/// <param name="listEntity">实体集合</param>
/// <returns>影响行数</returns>
public async Task<int> Add(List<TEntity> listEntity)
{
return await _db.Insertable(listEntity.ToArray()).ExecuteCommandAsync();
}
/// <summary>
/// 更新实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public async Task<bool> Update(TEntity entity)
{
////这种方式会以主键为条件
//var i = await Task.Run(() => _db.Updateable(entity).ExecuteCommand());
//return i > 0;
//这种方式会以主键为条件
return await _db.Updateable(entity).ExecuteCommandHasChangeAsync();
}
public async Task<bool> Update(TEntity entity, string strWhere)
{
//return await Task.Run(() => _db.Updateable(entity).Where(strWhere).ExecuteCommand() > 0);
return await _db.Updateable(entity).Where(strWhere).ExecuteCommandHasChangeAsync();
}
public async Task<bool> Update(string strSql, SugarParameter[] parameters = null)
{
//return await Task.Run(() => _db.Ado.ExecuteCommand(strSql, parameters) > 0);
return await _db.Ado.ExecuteCommandAsync(strSql, parameters) > 0;
}
public async Task<bool> Update(object operateAnonymousObjects)
{
return await _db.Updateable<TEntity>(operateAnonymousObjects).ExecuteCommandAsync() > 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();
}
/// <summary>
/// 根据实体删除一条数据
/// </summary>
/// <param name="entity">实体类</param>
/// <returns></returns>
public async Task<bool> Delete(TEntity entity)
{
//var i = await Task.Run(() => _db.Deleteable(entity).ExecuteCommand());
//return i > 0;
return await _db.Deleteable(entity).ExecuteCommandHasChangeAsync();
}
/// <summary>
/// 根据表达式删除
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
public async Task<bool> Delete(Expression<Func<TEntity, bool>> whereExpression)
{
return await _db.Deleteable<TEntity>().Where(whereExpression).ExecuteCommandHasChangeAsync();
}
/// <summary>
/// 删除指定ID的数据
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
public async Task<bool> DeleteById(object id)
{
//var i = await Task.Run(() => _db.Deleteable<TEntity>(id).ExecuteCommand());
//return i > 0;
return await _db.Deleteable<TEntity>(id).ExecuteCommandHasChangeAsync();
}
/// <summary>
/// 删除指定ID集合的数据(批量删除)
/// </summary>
/// <param name="ids">主键ID集合</param>
/// <returns></returns>
public async Task<bool> DeleteByIds(object[] ids)
{
//var i = await Task.Run(() => _db.Deleteable<TEntity>().In(ids).ExecuteCommand());
//return i > 0;
return await _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync();
}
/// <summary>
/// 功能描述:查询所有数据
///
/// </summary>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query()
{
return await _db.Queryable<TEntity>().ToListAsync();
}
/// <summary>
/// 功能描述:查询数据列表
///
/// </summary>
/// <param name="strWhere">条件</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(string strWhere)
{
//return await Task.Run(() => _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
return await _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
}
/// <summary>
/// 功能描述:查询数据列表
///
/// </summary>
/// <param name="whereExpression">whereExpression</param>
/// <returns>数据列表</returns>
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)
{
return null;
}
}
/// <summary>
/// 功能描述:查询一个列表
///
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList());
return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToListAsync();
}
/// <summary>
/// 功能描述:查询一个列表
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="orderByExpression"></param>
/// <param name="isAsc"></param>
/// <returns></returns>
public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList());
try
{
return await _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync();
}
catch (Exception ex)
{
LogHelper.Error(ex.Message);
return null;
}
}
/// <summary>
/// 排序取第一条
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="orderByExpression"></param>
/// <param name="isAsc"></param>
/// <returns></returns>
public async Task<TEntity> QueryFirst(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
{
try
{
TEntity info = await _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).FirstAsync();
return info;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 功能描述:查询一个列表
///
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(string strWhere, string strOrderByFileds)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
}
/// <summary>
/// 功能描述:查询前N条数据
///
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="intTop">前N条</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(
Expression<Func<TEntity, bool>> whereExpression,
int intTop,
string strOrderByFileds)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList());
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync();
}
/// <summary>
/// 功能描述:查询前N条数据
///
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intTop">前N条</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(
string strWhere,
int intTop,
string strOrderByFileds)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList());
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToListAsync();
}
/// <summary>
/// 功能描述:分页查询
///
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="intPageIndex">页码下标0</param>
/// <param name="intPageSize">页大小</param>
/// <param name="intTotalCount">数据总量</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(
Expression<Func<TEntity, bool>> whereExpression,
int intPageIndex,
int intPageSize,
string strOrderByFileds)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize));
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize);
}
/// <summary>
/// 功能描述:分页查询
///
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intPageIndex">页码下标0</param>
/// <param name="intPageSize">页大小</param>
/// <param name="intTotalCount">数据总量</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> Query(
string strWhere,
int intPageIndex,
int intPageSize,
string strOrderByFileds)
{
//return await Task.Run(() => _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize));
return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageListAsync(intPageIndex, intPageSize);
}
/// <summary>
/// 分页查询[使用版本,其他分页未测试]
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="intPageIndex">页码下标0</param>
/// <param name="intPageSize">页大小</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns></returns>
public async Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
{
RefAsync<int> totalCount = 0;
var 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())).ObjToInt();
return new PageModel<TEntity>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="T3">实体3</typeparam>
/// <typeparam name="T4">实体4</typeparam>
/// <typeparam name="T5">实体5</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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();
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="T3">实体3</typeparam>
/// <typeparam name="T4">实体4</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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();
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="T3">实体3</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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();
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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();
}
#region 多表分页
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="T3">实体3</typeparam>
/// <typeparam name="T4">实体4</typeparam>
/// <typeparam name="T5">实体5</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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;
var 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())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="T3">实体3</typeparam>
/// <typeparam name="T4">实体4</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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;
var 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())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="T3">实体3</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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;
var 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())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
}
/// <summary>
///查询-多表查询
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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;
var 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())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
}
/// <summary>
///查询-多表查询(测试)
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体2</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
/// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
/// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
/// <returns>值</returns>
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;
var 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())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
}
#endregion
/// <summary>
/// 存储过程
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parameters"></param>
/// <returns></returns>
public async Task<List<T>> QueryByProc<T>(string proc, List<SugarParameter> parameters) where T : class, new()
{
//var nameP = new SugarParameter("@name", "张三");
//var ageP = new SugarParameter("@age", null, true);//isOutput=true
//var ddd = new List<SugarParameter>();
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);
}
/// <summary>
/// sql语句
/// </summary>
public async Task<List<T>> QueryBySql<T>(string sql, List<SugarParameter> parameters) where T : class, new()
{
return await _db.Ado.SqlQueryAsync<T>(sql, parameters);
}
#region 自定义
/// <summary>
/// 添加上机记录,先判断是否存在该卷信息
/// </summary>
/// <param name="boardEntity"></param>
/// <param name="whereExpression"></param>
/// <returns></returns>
public async Task<int> BoardAdd(TEntity boardEntity, Expression<Func<TEntity, bool>> whereExpression)
{
try
{
TEntity entity = await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).SingleAsync();
if (entity != null)
{
return new int();
}
return await Add(boardEntity);
}
catch (Exception ex)
{
return new int();
}
}
#endregion
}
}