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.

1074 lines
48 KiB
C#

1 year ago
using Admin.Core.Common;
using Admin.Core.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Admin.Core.IRepository;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
namespace Admin.Core.Repository
{
public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
{
private readonly IUnitOfWork _unitOfWork;
private SqlSugarClient _dbBase;
private ISqlSugarClient _db
{
get
{
/*
* 1appsettings.json MutiDBEnabledtrue
* 2IDMainDBEnabledtrue
*/
if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool())
{
if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription))
{
_dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower());
}
else
{
_dbBase.ChangeDatabase(MainDb.CurrentDbConnId.ToLower());
}
}
return _dbBase;
}
}
public ISqlSugarClient Db
{
get { return _db; }
}
public BaseRepository(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_dbBase = unitOfWork.GetDbClient();
}
#region 异步方法
public async Task<TEntity> QueryByIdAsync(object objId)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().InSingle(objId));
return await _db.CopyNew().Queryable<TEntity>().In(objId).SingleAsync();
1 year ago
}
/// <summary>
/// 功能描述:根据ID查询一条数据
/// 作  者:Admin.Core
/// </summary>
/// <param name="objId">id必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param>
/// <param name="blnUseCache">是否使用缓存</param>
/// <returns>数据实体</returns>
public async Task<TEntity> QueryByIdAsync(object objId, bool blnUseCache = false)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().WithCacheIF(blnUseCache).InSingle(objId));
return await _db.CopyNew().Queryable<TEntity>().WithCacheIF(blnUseCache).In(objId).SingleAsync();
1 year ago
}
/// <summary>
/// 功能描述:根据ID查询数据
/// 作  者:Admin.Core
/// </summary>
/// <param name="lstIds">id列表必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param>
/// <returns>数据实体列表</returns>
public async Task<List<TEntity>> QueryByIDsAsync(object[] lstIds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().In(lstIds).ToList());
return await _db.CopyNew().Queryable<TEntity>().In(lstIds).ToListAsync();
1 year ago
}
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public async Task<int> AddAsync(TEntity entity)
{
10 months ago
//var i = await Task.Run(() => _db.CopyNew().Insertable(entity).ExecuteReturnBigIdentity());
1 year ago
////返回的i是long类型,这里你可以根据你的业务需要进行处理
//return (int)i;
10 months ago
var insert = _db.CopyNew().Insertable(entity);
1 year ago
//这里你可以返回TEntity这样的话就可以获取id值无论主键是什么类型
//var return3 = await insert.ExecuteReturnEntityAsync();
return await insert.ExecuteReturnIdentityAsync();
}
/// <summary>
/// 写入实体数据
/// </summary>
/// <param name="entity">实体类</param>
/// <param name="insertColumns">指定只插入列</param>
/// <returns>返回自增量列</returns>
public async Task<int> AddAsync(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
{
10 months ago
var insert = _db.CopyNew().Insertable(entity);
1 year ago
if (insertColumns == null)
{
return await insert.ExecuteReturnIdentityAsync();
}
else
{
return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync();
}
}
/// <summary>
/// 批量插入实体(速度快)
/// </summary>
/// <param name="listEntity">实体集合</param>
/// <returns>影响行数</returns>
public async Task<int> AddAsync(List<TEntity> listEntity)
{
10 months ago
return await _db.CopyNew().Insertable(listEntity.ToArray()).ExecuteCommandAsync();
1 year ago
}
/// <summary>
/// 更新实体数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public async Task<bool> UpdateAsync(TEntity entity)
{
////这种方式会以主键为条件
10 months ago
//var i = await Task.Run(() => _db.CopyNew().Updateable(entity).ExecuteCommand());
1 year ago
//return i > 0;
//这种方式会以主键为条件
10 months ago
return await _db.CopyNew().Updateable(entity).ExecuteCommandHasChangeAsync();
1 year ago
}
public async Task<bool> UpdateAsync(TEntity entity, string strWhere)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Updateable(entity).Where(strWhere).ExecuteCommand() > 0);
return await _db.CopyNew().Updateable(entity).Where(strWhere).ExecuteCommandHasChangeAsync();
1 year ago
}
public async Task<bool> UpdateAsync(string strSql, SugarParameter[] parameters = null)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Ado.ExecuteCommand(strSql, parameters) > 0);
return await _db.CopyNew().Ado.ExecuteCommandAsync(strSql, parameters) > 0;
1 year ago
}
public async Task<bool> UpdateAsync(object operateAnonymousObjects)
{
10 months ago
return await _db.CopyNew().Updateable<TEntity>(operateAnonymousObjects).ExecuteCommandAsync() > 0;
1 year ago
}
public async Task<bool> UpdateAsync(
TEntity entity,
List<string> lstColumns = null,
List<string> lstIgnoreColumns = null,
string strWhere = ""
)
{
10 months ago
//IUpdateable<TEntity> up = await Task.Run(() => _db.CopyNew().Updateable(entity));
1 year ago
//if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
//{
// up = await Task.Run(() => up.IgnoreColumns(it => lstIgnoreColumns.Contains(it)));
//}
//if (lstColumns != null && lstColumns.Count > 0)
//{
// up = await Task.Run(() => up.UpdateColumns(it => lstColumns.Contains(it)));
//}
//if (!string.IsNullOrEmpty(strWhere))
//{
// up = await Task.Run(() => up.Where(strWhere));
//}
//return await Task.Run(() => up.ExecuteCommand()) > 0;
10 months ago
IUpdateable<TEntity> up = _db.CopyNew().Updateable(entity);
1 year ago
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>
/// <returns></returns>
public async Task<TEntity> FirstAsync()
{
10 months ago
return await _db.CopyNew().Queryable<TEntity>().FirstAsync();
1 year ago
}
/// <summary>
/// 查询第一条数据
/// </summary>
/// <param name="whereExpression"></param>
/// <returns></returns>
public async Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> whereExpression)
{
10 months ago
return await _db.CopyNew().Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).FirstAsync();
1 year ago
}
/// <summary>
/// 根据实体删除一条数据
/// </summary>
/// <param name="entity">博文实体类</param>
/// <returns></returns>
public async Task<bool> DeleteAsync(TEntity entity)
{
10 months ago
//var i = await Task.Run(() => _db.CopyNew().Deleteable(entity).ExecuteCommand());
1 year ago
//return i > 0;
10 months ago
return await _db.CopyNew().Deleteable(entity).ExecuteCommandHasChangeAsync();
1 year ago
}
/// <summary>
/// 删除指定ID的数据
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
public async Task<bool> DeleteByIdAsync(object id)
{
10 months ago
//var i = await Task.Run(() => _db.CopyNew().Deleteable<TEntity>(id).ExecuteCommand());
1 year ago
//return i > 0;
10 months ago
return await _db.CopyNew().Deleteable<TEntity>(id).ExecuteCommandHasChangeAsync();
1 year ago
}
/// <summary>
/// 删除指定ID集合的数据(批量删除)
/// </summary>
/// <param name="ids">主键ID集合</param>
/// <returns></returns>
public async Task<bool> DeletesAsync(List<TEntity> entitys)
{
10 months ago
//var i = await Task.Run(() => _db.CopyNew().Deleteable<TEntity>().In(ids).ExecuteCommand());
1 year ago
//return i > 0;
10 months ago
return await _db.CopyNew().Deleteable<TEntity>(entitys).ExecuteCommandHasChangeAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询所有数据
/// 作  者:Admin.Core
/// </summary>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> QueryAsync()
{
10 months ago
return await _db.CopyNew().Queryable<TEntity>().ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询数据列表
/// 作  者:Admin.Core
/// </summary>
/// <param name="strWhere">条件</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> QueryAsync(string strWhere)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
return await _db.CopyNew().Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询数据列表
/// 作  者:Admin.Core
/// </summary>
/// <param name="whereExpression">whereExpression</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression)
{
10 months ago
return await _db.CopyNew().Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:按照特定列查询数据列表
/// 作  者:Admin.Core
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression)
{
10 months ago
return await _db.CopyNew().Queryable<TEntity>().Select(expression).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:按照特定列查询数据列表带条件排序
/// 作  者:Admin.Core
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="whereExpression">过滤条件</param>
/// <param name="expression">查询实体条件</param>
/// <param name="strOrderByFileds">排序条件</param>
/// <returns></returns>
public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
{
10 months ago
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询一个列表
/// 作  者:Admin.Core
/// </summary>
/// <param name="whereExpression">条件表达式</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList());
return await _db.CopyNew().Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询一个列表
/// </summary>
/// <param name="whereExpression"></param>
/// <param name="orderByExpression"></param>
/// <param name="isAsc"></param>
/// <returns></returns>
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList());
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询一个列表
/// 作  者:Admin.Core
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> QueryAsync(string strWhere, string strOrderByFileds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询前N条数据
/// 作  者:Admin.Core
/// </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>> QueryAsync(
Expression<Func<TEntity, bool>> whereExpression,
int intTop,
string strOrderByFileds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList());
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync();
1 year ago
}
/// <summary>
/// 功能描述:查询前N条数据
/// 作  者:Admin.Core
/// </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>> QueryAsync(
string strWhere,
int intTop,
string strOrderByFileds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList());
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToListAsync();
1 year ago
}
/// <summary>
/// 根据sql语句查询
/// </summary>
/// <param name="strSql">完整的sql语句</param>
/// <param name="parameters">参数</param>
/// <returns>泛型集合</returns>
public async Task<List<TEntity>> QuerySqlAsync(string strSql, SugarParameter[] parameters = null)
{
10 months ago
return await _db.CopyNew().Ado.SqlQueryAsync<TEntity>(strSql, parameters);
1 year ago
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="strSql">完整的sql语句</param>
/// <param name="parameters">参数</param>
/// <returns>泛型集合</returns>
public async Task<int> ExecSqlAsync(string strSql, SugarParameter[] parameters = null)
{
10 months ago
return await _db.CopyNew().Ado.ExecuteCommandAsync(strSql, parameters);
1 year ago
}
/// <summary>
/// 根据sql语句查询
/// </summary>
/// <param name="strSql">完整的sql语句</param>
/// <param name="parameters">参数</param>
/// <returns>DataTable</returns>
public async Task<DataTable> QueryTableAsync(string strSql, SugarParameter[] parameters = null)
{
10 months ago
return await _db.CopyNew().Ado.GetDataTableAsync(strSql, parameters);
1 year ago
}
/// <summary>
/// 功能描述:分页查询
/// 作  者:Admin.Core
/// </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<List<TEntity>> QueryAsync(
Expression<Func<TEntity, bool>> whereExpression,
int intPageIndex,
int intPageSize,
string strOrderByFileds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize));
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize);
1 year ago
}
/// <summary>
/// 功能描述:分页查询
/// 作  者:Admin.Core
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="intPageIndex">页码下标0</param>
/// <param name="intPageSize">页大小</param>
/// <param name="strOrderByFileds">排序字段如name asc,age desc</param>
/// <returns>数据列表</returns>
public async Task<List<TEntity>> QueryAsync(
string strWhere,
int intPageIndex,
int intPageSize,
string strOrderByFileds)
{
10 months ago
//return await Task.Run(() => _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize));
return await _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageListAsync(intPageIndex, intPageSize);
1 year ago
}
/// <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>> QueryPageAsync(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
{
RefAsync<int> totalCount = 0;
10 months ago
var list = await _db.CopyNew().Queryable<TEntity>()
1 year ago
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).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="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>> QueryMuchAsync<T, T2, T3, TResult>(
Expression<Func<T, T2, T3, object[]>> joinExpression,
Expression<Func<T, T2, T3, TResult>> selectExpression,
Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
10 months ago
return await _db.CopyNew().Queryable(joinExpression).Select(selectExpression).ToListAsync();
1 year ago
}
10 months ago
return await _db.CopyNew().Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToListAsync();
1 year ago
}
/// <summary>
/// 两表联合查询-分页
/// </summary>
/// <typeparam name="T">实体1</typeparam>
/// <typeparam name="T2">实体1</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式</param>
/// <param name="selectExpression">返回表达式</param>
/// <param name="whereExpression">查询表达式</param>
/// <param name="intPageIndex">页码</param>
/// <param name="intPageSize">页大小</param>
/// <param name="strOrderByFileds">排序字段</param>
/// <returns></returns>
public async Task<PageModel<TResult>> QueryTabsPageAsync<T, T2, TResult>(
Expression<Func<T, T2, object[]>> joinExpression,
Expression<Func<T, T2, TResult>> selectExpression,
Expression<Func<TResult, bool>> whereExpression,
int intPageIndex = 1,
int intPageSize = 20,
string strOrderByFileds = null)
{
RefAsync<int> totalCount = 0;
10 months ago
var list = await _db.CopyNew().Queryable<T, T2>(joinExpression)
1 year ago
.Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).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">实体1</typeparam>
/// <typeparam name="TResult">返回对象</typeparam>
/// <param name="joinExpression">关联表达式</param>
/// <param name="selectExpression">返回表达式</param>
/// <param name="whereExpression">查询表达式</param>
/// <param name="intPageIndex">页码</param>
/// <param name="intPageSize">页大小</param>
/// <param name="strOrderByFileds">排序字段</param>
/// <returns></returns>
public async Task<PageModel<TResult>> QueryTabsPageAsync<T, T2, TResult>(
Expression<Func<T, T2, object[]>> joinExpression,
Expression<Func<T, T2, TResult>> selectExpression,
Expression<Func<TResult, bool>> whereExpression,
Expression<Func<T, object>> groupExpression,
int intPageIndex = 1,
int intPageSize = 20,
string strOrderByFileds = null)
{
RefAsync<int> totalCount = 0;
10 months ago
var list = await _db.CopyNew().Queryable<T, T2>(joinExpression).GroupBy(groupExpression)
1 year ago
.Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list };
}
#endregion
#region 同步方法
public TEntity QueryById(object objId)
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().In(objId).Single();
1 year ago
}
/// <summary
/// 功能描述:根据ID查询一条数据
/// 作  者:Admin.Core
/// </summary
/// <param name="objId"id必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param
/// <param name="blnUseCache"是否使用缓存</param
/// <returns数据实体</returns
public TEntity QueryById(object objId, bool blnUseCache = false)
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().WithCacheIF(blnUseCache).In(objId).Single();
1 year ago
}
/// <summary
/// 功能描述:根据ID查询数据
/// 作  者:Admin.Core
/// </summary
/// <param name="lstIds"id列表必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]如果是联合主键请使用Where条件</param
/// <returns数据实体列表</returns
public List<TEntity> QueryByIDs(object[] lstIds)
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().In(lstIds).ToList();
1 year ago
}
/// <summary
/// 写入实体数据
/// </summary
/// <param name="entity"博文实体类</param
/// <returns</returns
public async Task<int> Add(TEntity entity)
1 year ago
{
//var insert = _db.CopyNew().Insertable(entity);
//return insert.ExecuteReturnIdentity();
try
{
return await _db.Insertable(entity).ExecuteCommandAsync();
}
catch (Exception ex)
{
return 0;
}
1 year ago
}
1 year ago
/// <summary
/// 写入实体数据
/// </summary
/// <param name="entity"实体类</param
/// <param name="insertColumns"指定只插入列</param
/// <returns返回自增量列</returns
public int Add(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
{
10 months ago
var insert = _db.CopyNew().Insertable(entity);
1 year ago
if (insertColumns == null)
{
return insert.ExecuteReturnIdentity();
}
else
{
return insert.InsertColumns(insertColumns).ExecuteReturnIdentity();
}
}
/// <summary
/// 批量插入实体(速度快)
/// </summary
/// <param name="listEntity"实体集合</param
/// <returns影响行数</returns
public int Add(List<TEntity> listEntity)
{
10 months ago
return _db.CopyNew().Insertable(listEntity.ToArray()).ExecuteCommand();
1 year ago
}
/// <summary
/// 更新实体数据
/// </summary
/// <param name="entity"博文实体类</param
/// <returns</returns
public bool Update(TEntity entity)
{
////这种方式会以主键为条件
10 months ago
//var i = Task.Run(() = _db.CopyNew().Updateable(entity).ExecuteCommand());
1 year ago
//return i 0;
//这种方式会以主键为条件
10 months ago
return _db.CopyNew().Updateable(entity).ExecuteCommandHasChange();
1 year ago
}
public bool Update(TEntity entity, string strWhere)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Updateable(entity).Where(strWhere).ExecuteCommand() 0);
return _db.CopyNew().Updateable(entity).Where(strWhere).ExecuteCommandHasChange();
1 year ago
}
public bool Update(string strSql, SugarParameter[] parameters = null)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Ado.ExecuteCommand(strSql, parameters) 0);
return _db.CopyNew().Ado.ExecuteCommand(strSql, parameters) > 0;
1 year ago
}
public bool Update(object operateAnonymousObjects)
{
10 months ago
return _db.CopyNew().Updateable<TEntity>(operateAnonymousObjects).ExecuteCommand() > 0;
1 year ago
}
public bool Update(
TEntity entity,
List<string> lstColumns = null,
List<string> lstIgnoreColumns = null,
string strWhere = ""
)
{
10 months ago
IUpdateable<TEntity> up = _db.CopyNew().Updateable(entity);
1 year ago
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 up.ExecuteCommandHasChange();
}
/// <summary
/// 根据实体删除一条数据
/// </summary
/// <param name="entity"博文实体类</param
/// <returns</returns
public bool Delete(TEntity entity)
{
10 months ago
//var i = Task.Run(() = _db.CopyNew().Deleteable(entity).ExecuteCommand());
1 year ago
//return i 0;
10 months ago
return _db.CopyNew().Deleteable(entity).ExecuteCommandHasChange();
1 year ago
}
/// <summary
/// 删除指定ID的数据
/// </summary
/// <param name="id"主键ID</param
/// <returns</returns
public bool DeleteById(object id)
{
10 months ago
//var i = Task.Run(() = _db.CopyNew().Deleteable<TEntity(id).ExecuteCommand());
1 year ago
//return i 0;
10 months ago
return _db.CopyNew().Deleteable<TEntity>(id).ExecuteCommandHasChange();
1 year ago
}
/// <summary
/// 删除指定ID集合的数据(批量删除)
/// </summary
/// <param name="ids"主键ID集合</param
/// <returns</returns
public bool Deletes(List<TEntity> entitys)
{
10 months ago
//var i = Task.Run(() = _db.CopyNew().Deleteable<TEntity().In(ids).ExecuteCommand());
1 year ago
//return i 0;
10 months ago
return _db.CopyNew().Deleteable<TEntity>(entitys).ExecuteCommandHasChange();
1 year ago
}
/// <summary
/// 功能描述:查询所有数据
/// 作  者:Admin.Core
/// </summary
/// <returns数据列表</returns
public List<TEntity> Query()
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().ToList();
1 year ago
}
/// <summary
/// 功能描述:查询数据列表
/// 作  者:Admin.Core
/// </summary
/// <param name="strWhere"条件</param
/// <returns数据列表</returns
public List<TEntity> Query(string strWhere)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
return _db.CopyNew().Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
1 year ago
}
/// <summary
/// 功能描述:查询数据列表
/// 作  者:Admin.Core
/// </summary
/// <param name="whereExpression"whereExpression</param
/// <returns数据列表</returns
public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression)
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList();
1 year ago
}
/// <summary
/// 功能描述:按照特定列查询数据列表
/// 作  者:Admin.Core
/// </summary
/// <typeparam name="TResult"</typeparam
/// <param name="expression"</param
/// <returns</returns
public List<TResult> Query<TResult>(Expression<Func<TEntity, TResult>> expression)
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().Select(expression).ToList();
1 year ago
}
/// <summary
/// 功能描述:按照特定列查询数据列表带条件排序
/// 作  者:Admin.Core
/// </summary
/// <typeparam name="TResult"</typeparam
/// <param name="whereExpression"过滤条件</param
/// <param name="expression"查询实体条件</param
/// <param name="strOrderByFileds"排序条件</param
/// <returns</returns
public List<TResult> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
{
10 months ago
return _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList();
1 year ago
}
/// <summary
/// 功能描述:查询一个列表
/// 作  者:Admin.Core
/// </summary
/// <param name="whereExpression"条件表达式</param
/// <param name="strOrderByFileds"排序字段如name asc,age desc</param
/// <returns数据列表</returns
public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList());
return _db.CopyNew().Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToList();
1 year ago
}
/// <summary
/// 功能描述:查询一个列表
/// </summary
/// <param name="whereExpression"</param
/// <param name="orderByExpression"</param
/// <param name="isAsc"</param
/// <returns</returns
public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList());
return _db.CopyNew().Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList();
1 year ago
}
/// <summary
/// 功能描述:查询一个列表
/// 作  者:Admin.Core
/// </summary
/// <param name="strWhere"条件</param
/// <param name="strOrderByFileds"排序字段如name asc,age desc</param
/// <returns数据列表</returns
public List<TEntity> Query(string strWhere, string strOrderByFileds)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
return _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
1 year ago
}
/// <summary
/// 功能描述:查询前N条数据
/// 作  者:Admin.Core
/// </summary
/// <param name="whereExpression"条件表达式</param
/// <param name="intTop"前N条</param
/// <param name="strOrderByFileds"排序字段如name asc,age desc</param
/// <returns数据列表</returns
public List<TEntity> Query(
Expression<Func<TEntity, bool>> whereExpression,
int intTop,
string strOrderByFileds)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList());
return _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList();
1 year ago
}
/// <summary
/// 功能描述:查询前N条数据
/// 作  者:Admin.Core
/// </summary
/// <param name="strWhere"条件</param
/// <param name="intTop"前N条</param
/// <param name="strOrderByFileds"排序字段如name asc,age desc</param
/// <returns数据列表</returns
public List<TEntity> Query(
string strWhere,
int intTop,
string strOrderByFileds)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList());
return _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList();
1 year ago
}
/// <summary
/// 根据sql语句查询
/// </summary
/// <param name="strSql"完整的sql语句</param
/// <param name="parameters"参数</param
/// <returns泛型集合</returns
public List<TEntity> QuerySql(string strSql, SugarParameter[] parameters = null)
{
10 months ago
return _db.CopyNew().Ado.SqlQuery<TEntity>(strSql, parameters);
1 year ago
}
/// <summary
/// 执行sql语句
/// </summary
/// <param name="strSql"完整的sql语句</param
/// <param name="parameters"参数</param
/// <returns泛型集合</returns
public int ExecSql(string strSql, SugarParameter[] parameters = null)
{
10 months ago
return _db.CopyNew().Ado.ExecuteCommand(strSql, parameters);
1 year ago
}
/// <summary
/// 根据sql语句查询
/// </summary
/// <param name="strSql"完整的sql语句</param
/// <param name="parameters"参数</param
/// <returnsDataTable</returns
public DataTable QueryTable(string strSql, SugarParameter[] parameters = null)
{
10 months ago
return _db.CopyNew().Ado.GetDataTable(strSql, parameters);
1 year ago
}
/// <summary
/// 功能描述:分页查询
/// 作  者:Admin.Core
/// </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 List<TEntity> Query(
Expression<Func<TEntity, bool>> whereExpression,
int intPageIndex,
int intPageSize,
string strOrderByFileds)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize));
return _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize);
1 year ago
}
/// <summary
/// 功能描述:分页查询
/// 作  者:Admin.Core
/// </summary
/// <param name="strWhere"条件</param
/// <param name="intPageIndex"页码下标0</param
/// <param name="intPageSize"页大小</param
/// <param name="strOrderByFileds"排序字段如name asc,age desc</param
/// <returns数据列表</returns
public List<TEntity> Query(
string strWhere,
int intPageIndex,
int intPageSize,
string strOrderByFileds)
{
10 months ago
//return Task.Run(() = _db.CopyNew().Queryable<TEntity().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize));
return _db.CopyNew().Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize);
1 year ago
}
/// <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 PageModel<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
{
int totalCount = 0;
10 months ago
var list = _db.CopyNew().Queryable<TEntity>()
1 year ago
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageList(intPageIndex, intPageSize, ref totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).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="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 List<TResult> QueryMuch<T, T2, T3, TResult>(
Expression<Func<T, T2, T3, object[]>> joinExpression,
Expression<Func<T, T2, T3, TResult>> selectExpression,
Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
{
if (whereLambda == null)
{
10 months ago
return _db.CopyNew().Queryable(joinExpression).Select(selectExpression).ToList();
1 year ago
}
10 months ago
return _db.CopyNew().Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToList();
1 year ago
}
/// <summary
/// 两表联合查询-分页
/// </summary
/// <typeparam name="T"实体1</typeparam
/// <typeparam name="T2"实体1</typeparam
/// <typeparam name="TResult"返回对象</typeparam
/// <param name="joinExpression"关联表达式</param
/// <param name="selectExpression"返回表达式</param
/// <param name="whereExpression"查询表达式</param
/// <param name="intPageIndex"页码</param
/// <param name="intPageSize"页大小</param
/// <param name="strOrderByFileds"排序字段</param
/// <returns</returns
public PageModel<TResult> QueryTabsPage<T, T2, TResult>(
Expression<Func<T, T2, object[]>> joinExpression,
Expression<Func<T, T2, TResult>> selectExpression,
Expression<Func<TResult, bool>> whereExpression,
int intPageIndex = 1,
int intPageSize = 20,
string strOrderByFileds = null)
{
int totalCount = 0;
10 months ago
var list = _db.CopyNew().Queryable<T, T2>(joinExpression)
1 year ago
.Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageList(intPageIndex, intPageSize, ref totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).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"实体1</typeparam
/// <typeparam name="TResult"返回对象</typeparam
/// <param name="joinExpression"关联表达式</param
/// <param name="selectExpression"返回表达式</param
/// <param name="whereExpression"查询表达式</param
/// <param name="intPageIndex"页码</param
/// <param name="intPageSize"页大小</param
/// <param name="strOrderByFileds"排序字段</param
/// <returns</returns
public PageModel<TResult> QueryTabsPage<T, T2, TResult>(
Expression<Func<T, T2, object[]>> joinExpression,
Expression<Func<T, T2, TResult>> selectExpression,
Expression<Func<TResult, bool>> whereExpression,
Expression<Func<T, object>> groupExpression,
int intPageIndex = 1,
int intPageSize = 20,
string strOrderByFileds = null)
{
int totalCount = 0;
10 months ago
var list = _db.CopyNew().Queryable<T, T2>(joinExpression).GroupBy(groupExpression)
1 year ago
.Select(selectExpression)
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression)
.ToPageList(intPageIndex, intPageSize,ref totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list };
}
#endregion
}
}