¶Ô±ÈÐÂÎļþ |
| | |
| | | using SqlSugar; |
| | | using System.Data; |
| | | using System.Linq.Expressions; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using System.Reflection; |
| | | using WIDESEA_Core.Helper; |
| | | using Microsoft.Data.SqlClient; |
| | | using System.Drawing.Printing; |
| | | using WIDESEA_Core.Tenants; |
| | | using WIDESEA_Core.Seed; |
| | | using WIDESEA_Core.DB; |
| | | using WIDESEA_Core.Const; |
| | | using WIDESEA_Core.AOP; |
| | | using OfficeOpenXml.FormulaParsing.ExpressionGraph; |
| | | using WIDESEA_Core.Enums; |
| | | using WIDESEA_Core.Utilities; |
| | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | using NetTaste; |
| | | using WIDESEA_Core.DB.Models; |
| | | |
| | | namespace WIDESEA_Core.BaseRepository |
| | | { |
| | | public class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class, new() |
| | | { |
| | | private readonly IUnitOfWorkManage _unitOfWorkManage; |
| | | private readonly SqlSugarClient _dbBase; |
| | | |
| | | private ISqlSugarClient _db |
| | | { |
| | | get |
| | | { |
| | | ISqlSugarClient db = _dbBase; |
| | | |
| | | //å¤ç§æ· |
| | | //var mta = typeof(TEntity).GetCustomAttribute<MultiTenantAttribute>(); |
| | | //if (mta is { TenantType: TenantTypeEnum.Db }) |
| | | //{ |
| | | // //è·åç§æ·ä¿¡æ¯ ç§æ·ä¿¡æ¯å¯ä»¥æåç¼å䏿¥ |
| | | // if (App.User is { TenantId: > 0 }) |
| | | // { |
| | | // dynamic tenant = db.Queryable(MainDb.TenantTableName, "x").Where(MainDb.TenantId, "=", App.User.TenantId).First(); |
| | | // if (tenant != null) |
| | | // { |
| | | // var iTenant = db.AsTenant(); |
| | | // if (!iTenant.IsAnyConnection(tenant.TenantId)) |
| | | // { |
| | | // string conStr = tenant.ConnectionString; |
| | | // ConnectionConfig connectionConfig = new ConnectionConfig() |
| | | // { |
| | | // ConfigId = tenant.TenantId, |
| | | // ConnectionString = conStr.DecryptDES(AppSecret.DB), |
| | | // DbType = (SqlSugar.DbType)tenant.DbType, |
| | | // IsAutoCloseConnection = true, |
| | | // AopEvents = new AopEvents() |
| | | // { |
| | | // DataExecuting = SqlSugarAop.DataExecuting, |
| | | |
| | | // } |
| | | // }; |
| | | // iTenant.AddConnection(connectionConfig); |
| | | // } |
| | | |
| | | // return iTenant.GetConnection(tenant.TenantId); |
| | | // } |
| | | // } |
| | | //} |
| | | |
| | | return db; |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// åå»ºæ°æ®åºè¿æ¥å¯¹è±¡ |
| | | /// </summary> |
| | | public ISqlSugarClient Db => _db; |
| | | |
| | | public RepositoryBase(IUnitOfWorkManage unitOfWorkManage) |
| | | { |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | | _dbBase = unitOfWorkManage.GetDbClient(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿ä¸»é®æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="id">主é®</param> |
| | | /// <returns>æ¥è¯¢ç»æ</returns> |
| | | public virtual TEntity QureyDataById(object id) |
| | | { |
| | | return _db.Queryable<TEntity>().In(id).Single(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿ä¸»é®æ°ç»æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="lstIds">䏻鮿°ç»</param> |
| | | /// <returns>æ¥è¯¢ç»æéå</returns> |
| | | public virtual List<TEntity> QureyDataByIds(object[] lstIds) |
| | | { |
| | | return _db.Queryable<TEntity>().In(lstIds).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿ä¸»é®éåæ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="lstIds">主é®éå</param> |
| | | /// <returns>æ¥è¯¢ç»æéå</returns> |
| | | public virtual List<TEntity> QureyDataByIds(List<object> lstIds) |
| | | { |
| | | return _db.Queryable<TEntity>().In(lstIds).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ·»å åæ¡æ°æ® |
| | | /// </summary> |
| | | /// <param name="entity"></param> |
| | | /// <returns>å½±åè¡æ°</returns> |
| | | public virtual int AddData(TEntity entity) |
| | | { |
| | | IInsertable<TEntity> insert = _db.Insertable(entity); |
| | | return insert.ExecuteReturnIdentity(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ·»å 夿¡æ°æ® |
| | | /// </summary> |
| | | /// <param name="listEntity"></param> |
| | | /// <returns>å½±åè¡æ°</returns> |
| | | public virtual int AddData(List<TEntity> listEntity) |
| | | { |
| | | IInsertable<TEntity> insert = _db.Insertable(listEntity); |
| | | return insert.ExecuteCommand(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿ä¸»é®å 餿°æ® |
| | | /// </summary> |
| | | /// <param name="id">主é®</param> |
| | | /// <returns>å é¤ç»æ</returns> |
| | | public virtual bool DeleteDataById(object id) |
| | | { |
| | | return _db.Deleteable<TEntity>().In(id).ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿ä¸»é®æ°æ®å é¤å¤æ¡æ°æ® |
| | | /// </summary> |
| | | /// <param name="ids">䏻鮿°ç»</param> |
| | | /// <returns>å é¤ç»æ</returns> |
| | | public virtual bool DeleteDataByIds(object[] ids) |
| | | { |
| | | return _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿å®ä½æ°æ®å 餿°æ® |
| | | /// </summary> |
| | | /// <param name="ids">䏻鮿°ç»</param> |
| | | /// <returns>å é¤ç»æ</returns> |
| | | public virtual bool DeleteData(TEntity entity) |
| | | { |
| | | return _db.Deleteable(entity).ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éè¿å®ä½éåæ°æ®å 餿°æ® |
| | | /// </summary> |
| | | /// <param name="ids">䏻鮿°ç»</param> |
| | | /// <returns>å é¤ç»æ</returns> |
| | | public virtual bool DeleteData(List<TEntity> listEntity) |
| | | { |
| | | return _db.Deleteable(listEntity).ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ´æ°åæ¡æ°æ® |
| | | /// </summary> |
| | | /// <param name="entity"></param> |
| | | /// <returns></returns> |
| | | public virtual bool UpdateData(TEntity entity) |
| | | { |
| | | return _db.Updateable(entity).ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ´æ°å¤æ¡æ°æ® |
| | | /// </summary> |
| | | /// <param name="listEntity"></param> |
| | | /// <returns></returns> |
| | | public virtual bool UpdateData(List<TEntity> listEntity) |
| | | { |
| | | return _db.Updateable(listEntity).ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æå®åæ´æ°æ°æ® |
| | | /// </summary> |
| | | /// <param name="entity"></param> |
| | | /// <param name="lstColumns"></param> |
| | | /// <param name="lstIgnoreColumns"></param> |
| | | /// <returns></returns> |
| | | public virtual bool UpdateData(TEntity entity, List<string> lstColumns, List<string>? lstIgnoreColumns = null) |
| | | { |
| | | IUpdateable<TEntity> update = _db.Updateable(entity); |
| | | |
| | | if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0) |
| | | { |
| | | update = update.IgnoreColumns(lstIgnoreColumns.ToArray()); |
| | | } |
| | | |
| | | if (lstColumns != null && lstColumns.Count > 0) |
| | | { |
| | | update = update.UpdateColumns(lstColumns.ToArray()); |
| | | } |
| | | |
| | | return update.ExecuteCommandHasChange(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¥è¯¢æææ°æ® |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData() |
| | | { |
| | | return _db.Queryable<TEntity>().ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="where"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(string where) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList(); |
| | | } |
| | | |
| | | public virtual TEntity QueryFirst(Expression<Func<TEntity, bool>> whereExpression) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).First(); |
| | | } |
| | | |
| | | public virtual TResult QueryFirst<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Select(expression).First(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ®å¹¶æåº |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ®å¹¶æåº |
| | | /// </summary> |
| | | /// <param name="where"></param> |
| | | /// <param name="orderBy"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(string where, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¥è¯¢æå®æ°æ®å¯¹è±¡ |
| | | /// </summary> |
| | | /// <typeparam name="TResult"></typeparam> |
| | | /// <param name="expression"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TResult> QueryData<TResult>(Expression<Func<TEntity, TResult>> expression) |
| | | { |
| | | return _db.Queryable<TEntity>().Select(expression).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æå®æ°æ®å¯¹è±¡ |
| | | /// </summary> |
| | | /// <typeparam name="TResult"></typeparam> |
| | | /// <param name="expression"></param> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TResult> QueryData<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields = "") |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ®å¹¶æåº |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="orderByExpression"></param> |
| | | /// <param name="isAsc"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ®å¹¶æåº |
| | | /// </summary> |
| | | /// <param name="where"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(string where, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åçSqlè¯å¥æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryDataBySql(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQuery<TEntity>(sql, parameters); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åçSqlè¯å¥æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual List<dynamic> QueryDynamicDataBySql(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQuery<dynamic>(sql, parameters); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åçSqlè¯å¥æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual List<object> QueryObjectDataBySql(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQuery<object>(sql, parameters); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åçSqlè¯å¥æ§è¡æä½ |
| | | /// </summary> |
| | | /// <param name="sql"></param> |
| | | /// <param name="sqlParameters"></param> |
| | | /// <returns></returns> |
| | | public virtual int ExecuteSqlCommand(string sql, params SqlParameter[] sqlParameters) |
| | | { |
| | | return _db.Ado.ExecuteCommand(sql, sqlParameters); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åçSqlè¯å¥æ¥è¯¢æ°æ® |
| | | /// </summary> |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual DataTable QueryTable(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.GetDataTable(sql, parameters); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æ°æ®æå®æ°éçè¡ |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="top"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Take(top).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// æ¡ä»¶æ¥è¯¢æå®æ°éçè¡ |
| | | /// </summary> |
| | | /// <param name="where"></param> |
| | | /// <param name="top"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(string where, int top, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).Take(top).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å页æ¥è¯¢ |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(whereExpression != null, whereExpression).ToPageList(pageIndex, pageSize); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å页æ¥è¯¢ |
| | | /// </summary> |
| | | /// <param name="where"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(string where, int pageIndex, int pageSize, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(!string.IsNullOrEmpty(where), where).ToPageList(pageIndex, pageSize); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å页æ¥è¯¢ |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual PageGridData<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string? orderByFields = null) |
| | | { |
| | | int totalCount = 0; |
| | | var list = _db.Queryable<TEntity>() |
| | | .OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(whereExpression != null, whereExpression) |
| | | .ToPageList(pageIndex, pageSize, ref totalCount); |
| | | |
| | | return new PageGridData<TEntity> { Rows = list, Total = totalCount }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å页æ¥è¯¢ |
| | | /// </summary> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderBy"></param> |
| | | /// <returns></returns> |
| | | public virtual PageGridData<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | int totalCount = 0; |
| | | List<TEntity> list = _db.Queryable<TEntity>() |
| | | .OrderBy(orderByModels) |
| | | .WhereIF(whereExpression != null, whereExpression) |
| | | .ToPageList(pageIndex, pageSize, ref totalCount); |
| | | |
| | | return new PageGridData<TEntity>(totalCount, list); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å页æ¥è¯¢ |
| | | /// </summary> |
| | | /// <param name="where"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual PageGridData<TEntity> QueryPage(string where, int pageIndex, int pageSize, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key.FirstLetterToUpper(), |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | int totalCount = 0; |
| | | List<TEntity> list = _db.Queryable<TEntity>() |
| | | .WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToPageList(pageIndex, pageSize, ref totalCount); |
| | | |
| | | return new PageGridData<TEntity>(totalCount, list); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// ä¸¤è¡¨èæ¥ |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <typeparam name="T2"></typeparam> |
| | | /// <typeparam name="TResult"></typeparam> |
| | | /// <param name="joinExpression"></param> |
| | | /// <param name="selectExpression"></param> |
| | | /// <param name="whereExpression"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TResult> QueryTabs<T, T2, TResult>( |
| | | Expression<Func<T, T2, object[]>> joinExpression, |
| | | Expression<Func<T, T2, TResult>> selectExpression, |
| | | Expression<Func<T, T2, bool>> whereExpressionT1, |
| | | Expression<Func<TResult, bool>> whereExpression) |
| | | { |
| | | List<TResult> list = _db.Queryable(joinExpression).WhereIF(whereExpressionT1 != null, whereExpressionT1) |
| | | .Select(selectExpression) |
| | | .WhereIF(whereExpression != null, whereExpression).ToList(); |
| | | return list; |
| | | } |
| | | |
| | | public virtual List<TResult> QueryTabs<T, T2, TResult>( |
| | | Expression<Func<T, T2, bool>> joinExpression, |
| | | Expression<Func<T, T2, TResult>> selectExpression, |
| | | Expression<Func<T, bool>> whereExpressionT1, |
| | | Expression<Func<T2, bool>> whereExpressionT2, |
| | | Expression<Func<TResult, bool>> whereExpression) |
| | | { |
| | | List<TResult> list = _db.Queryable<T>().WhereIF(whereExpressionT1 != null, whereExpressionT1).InnerJoin(_db.Queryable<T2>().WhereIF(whereExpressionT2 != null, whereExpressionT2), joinExpression).Select(selectExpression) |
| | | .WhereIF(whereExpression != null, whereExpression).ToList(); |
| | | return list; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// ä¸¤è¡¨èæ¥-å页 |
| | | /// </summary> |
| | | /// <typeparam name="T1"></typeparam> |
| | | /// <typeparam name="T2"></typeparam> |
| | | /// <typeparam name="TResult"></typeparam> |
| | | /// <param name="joinExpression"></param> |
| | | /// <param name="selectExpression"></param> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderByFields"></param> |
| | | public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, int pageIndex, int pageSize, string? orderByFields = null) |
| | | { |
| | | int totalCount = 0; |
| | | List<TResult> list = _db.Queryable(joinExpression) |
| | | .Select(selectExpression) |
| | | .OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(whereExpression != null, whereExpression) |
| | | .ToPageList(pageIndex, pageSize, ref totalCount); |
| | | return new PageGridData<TResult>(totalCount, list); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 两表èåæ¥è¯¢-å页-åç» |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <typeparam name="T2"></typeparam> |
| | | /// <typeparam name="TResult"></typeparam> |
| | | /// <param name="joinExpression"></param> |
| | | /// <param name="selectExpression"></param> |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="groupExpression"></param> |
| | | /// <param name="pageIndex"></param> |
| | | /// <param name="pageSize"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, Expression<Func<T1, object>> groupExpression, int pageIndex, int pageSize, string? orderByFields = null) |
| | | { |
| | | int totalCount = 0; |
| | | List<TResult> list = _db.Queryable(joinExpression).GroupBy(groupExpression) |
| | | .Select(selectExpression) |
| | | .OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(whereExpression != null, whereExpression) |
| | | .ToPageList(pageIndex, pageSize, ref totalCount); |
| | | return new PageGridData<TResult>(totalCount, list); |
| | | } |
| | | |
| | | public Task<TEntity> QureyDataByIdAsync(object id) |
| | | { |
| | | return _db.Queryable<TEntity>().In(id).SingleAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QureyDataByIdsAsync(object[] lstIds) |
| | | { |
| | | return _db.Queryable<TEntity>().In(lstIds).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QureyDataByIdsAsync(List<object> lstIds) |
| | | { |
| | | return _db.Queryable<TEntity>().In(lstIds).ToListAsync(); |
| | | } |
| | | |
| | | public Task<int> AddDataAsync(TEntity entity) |
| | | { |
| | | IInsertable<TEntity> insert = _db.Insertable(entity); |
| | | return insert.ExecuteReturnIdentityAsync(); |
| | | } |
| | | |
| | | public Task<int> AddDataAsync(List<TEntity> listEntity) |
| | | { |
| | | IInsertable<TEntity> insert = _db.Insertable(listEntity); |
| | | return insert.ExecuteReturnIdentityAsync(); |
| | | } |
| | | |
| | | public Task<bool> DeleteDataByIdAsync(object id) |
| | | { |
| | | return _db.Deleteable<TEntity>().In(id).ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<bool> DeleteDataByIdsAsync(object[] ids) |
| | | { |
| | | return _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<bool> DeleteDataAsync(TEntity entity) |
| | | { |
| | | return _db.Deleteable(entity).ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<bool> DeleteDataAsync(List<TEntity> listEntity) |
| | | { |
| | | return _db.Deleteable(listEntity).ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<bool> UpdateDataAsync(TEntity entity) |
| | | { |
| | | return _db.Updateable(entity).ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<bool> UpdateDataAsync(List<TEntity> listEntity) |
| | | { |
| | | return _db.Updateable(listEntity).ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<bool> UpdateDataAsync(TEntity entity, List<string> lstColumns, List<string>? lstIgnoreColumns = null) |
| | | { |
| | | IUpdateable<TEntity> update = _db.Updateable(entity); |
| | | |
| | | if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0) |
| | | { |
| | | update = update.IgnoreColumns(lstIgnoreColumns.ToArray()); |
| | | } |
| | | |
| | | if (lstColumns != null && lstColumns.Count > 0) |
| | | { |
| | | update = update.UpdateColumns(lstColumns.ToArray()); |
| | | } |
| | | |
| | | return update.ExecuteCommandHasChangeAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync() |
| | | { |
| | | return _db.Queryable<TEntity>().ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(string where) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync(); |
| | | } |
| | | |
| | | public Task<TEntity> QueryFirstAsync(Expression<Func<TEntity, bool>> whereExpression) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).FirstAsync(); |
| | | } |
| | | |
| | | public Task<TResult> QueryFirstAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Select(expression).FirstAsync(); |
| | | } |
| | | |
| | | public TResult QueryFirst<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).Select(expression).First(); |
| | | } |
| | | |
| | | public Task<TResult> QueryFirstAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TResult>> expression, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).Select(expression).FirstAsync(); |
| | | } |
| | | |
| | | public TEntity QueryFirst(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).First(); |
| | | } |
| | | |
| | | public Task<TEntity> QueryFirstAsync(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).FirstAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(string where, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | } |
| | | return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TResult>> QueryDataAsync<TResult>(Expression<Func<TEntity, TResult>> expression) |
| | | { |
| | | return _db.Queryable<TEntity>().Select(expression).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TResult>> QueryDataAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(string where, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataBySqlAsync(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQueryAsync<TEntity>(sql, parameters); |
| | | } |
| | | |
| | | public Task<List<dynamic>> QueryDynamicDataBySqlAsync(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQueryAsync<dynamic>(sql, parameters); |
| | | } |
| | | |
| | | public Task<List<object>> QueryObjectDataBySqlAsync(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQueryAsync<object>(sql, parameters); |
| | | } |
| | | |
| | | public Task<int> ExecuteSqlCommandAsync(string sql, params SqlParameter[] sqlParameters) |
| | | { |
| | | return _db.Ado.ExecuteCommandAsync(sql, sqlParameters); |
| | | } |
| | | |
| | | public Task<DataTable> QueryTableAsync(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.GetDataTableAsync(sql, parameters); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, int top, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Take(top).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(string where, int top, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(!string.IsNullOrEmpty(where), where).Take(top).ToListAsync(); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(whereExpression != null, whereExpression).ToPageListAsync(pageIndex, pageSize); |
| | | } |
| | | |
| | | public Task<List<TEntity>> QueryDataAsync(string where, int pageIndex, int pageSize, string orderByFields) |
| | | { |
| | | return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields) |
| | | .WhereIF(!string.IsNullOrEmpty(where), where).ToPageListAsync(pageIndex, pageSize); |
| | | } |
| | | |
| | | public Task<List<TResult>> QueryTabsAsync<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression) |
| | | { |
| | | return _db.Queryable(joinExpression) |
| | | .Select(selectExpression) |
| | | .WhereIF(whereExpression != null, whereExpression).ToListAsync(); |
| | | } |
| | | |
| | | public bool DeleteAndMoveIntoHty(TEntity entity, OperateType operateType) |
| | | { |
| | | Type type = entity.GetType(); |
| | | Assembly assembly = type.Assembly; |
| | | Type? htyType = assembly.GetType(type.FullName + "_Hty"); |
| | | if (htyType != null) |
| | | { |
| | | object? obj = Activator.CreateInstance(htyType); |
| | | PropertyInfo keyPro = typeof(TEntity).GetKeyProperty(); |
| | | PropertyInfo? operateTypePro = htyType.GetProperty(nameof(OperateType)); |
| | | PropertyInfo? sourceIdPro = htyType.GetProperty("SourceId"); |
| | | if (obj != null && keyPro != null && operateTypePro != null && sourceIdPro != null) |
| | | { |
| | | operateTypePro.SetValue(obj, operateType.ToString()); |
| | | sourceIdPro.SetValue(obj, keyPro.GetValue(entity)); |
| | | |
| | | List<PropertyInfo> propertyInfos = htyType.GetProperties().Where(x => x.Name != operateTypePro.Name && x.Name != sourceIdPro.Name && x.Name != keyPro.Name).ToList(); |
| | | |
| | | for (int i = 0; i < propertyInfos.Count; i++) |
| | | { |
| | | PropertyInfo propertyInfo = propertyInfos[i]; |
| | | PropertyInfo? property = type.GetProperty(propertyInfo.Name); |
| | | |
| | | if (property != null) |
| | | { |
| | | if (propertyInfo.Name == nameof(BaseEntity.Modifier)) |
| | | { |
| | | propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : App.User.UserId.ToString()); |
| | | } |
| | | else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate)) |
| | | { |
| | | propertyInfo.SetValue(obj, DateTime.Now); |
| | | } |
| | | else |
| | | { |
| | | propertyInfo.SetValue(obj, property.GetValue(entity)); |
| | | } |
| | | } |
| | | } |
| | | _db.InsertableByObject(obj).AS(type.Name + "_Hty").ExecuteCommand(); |
| | | } |
| | | } |
| | | return DeleteData(entity); |
| | | } |
| | | |
| | | public bool DeleteAndMoveIntoHty(List<TEntity> entities, OperateType operateType) |
| | | { |
| | | Type type = typeof(TEntity); |
| | | Assembly assembly = type.Assembly; |
| | | Type? htyType = assembly.GetType(type.FullName + "_Hty"); |
| | | if (htyType != null) |
| | | { |
| | | object? obj2 = Activator.CreateInstance(htyType); |
| | | PropertyInfo keyPro = typeof(TEntity).GetKeyProperty(); |
| | | PropertyInfo? operateTypePro = htyType.GetProperty(nameof(OperateType)); |
| | | PropertyInfo? sourceIdPro = htyType.GetProperty("SourceId"); |
| | | if (obj2 != null && keyPro != null && operateTypePro != null && sourceIdPro != null) |
| | | { |
| | | List<PropertyInfo> propertyInfos = htyType.GetProperties().Where(x => x.Name != operateTypePro.Name && x.Name != sourceIdPro.Name && x.Name != keyPro.Name).ToList(); |
| | | List<object> list = new List<object>(); |
| | | foreach (var item in entities) |
| | | { |
| | | object? obj = Activator.CreateInstance(htyType); |
| | | if (obj != null) |
| | | { |
| | | operateTypePro.SetValue(obj, operateType.ToString()); |
| | | sourceIdPro.SetValue(obj, keyPro.GetValue(item)); |
| | | for (int i = 0; i < propertyInfos.Count; i++) |
| | | { |
| | | PropertyInfo propertyInfo = propertyInfos[i]; |
| | | PropertyInfo? property = type.GetProperty(propertyInfo.Name); |
| | | |
| | | if (property != null) |
| | | { |
| | | if (propertyInfo.Name == nameof(BaseEntity.Modifier)) |
| | | { |
| | | propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : App.User.UserId.ToString()); |
| | | } |
| | | else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate)) |
| | | { |
| | | propertyInfo.SetValue(obj, DateTime.Now); |
| | | } |
| | | else |
| | | { |
| | | propertyInfo.SetValue(obj, property.GetValue(item)); |
| | | } |
| | | } |
| | | } |
| | | list.Add(obj); |
| | | } |
| | | } |
| | | _db.InsertableByObject(list).AS(type.Name + "_Hty").ExecuteCommand(); |
| | | |
| | | } |
| | | } |
| | | return DeleteData(entities); |
| | | } |
| | | //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(){throw new NotImplementedException();} |
| | | //Task<PageModel<TEntity>> QueryPage(PaginationModel pagination){throw new NotImplementedException();} |
| | | } |
| | | } |