| | |
| | | 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 |
| | | { |
| | |
| | | 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); |
| | | } |
| | | //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, |
| | | |
| | | return iTenant.GetConnection(tenant.TenantId); |
| | | } |
| | | } |
| | | } |
| | | // } |
| | | // }; |
| | | // iTenant.AddConnection(connectionConfig); |
| | | // } |
| | | |
| | | // return iTenant.GetConnection(tenant.TenantId); |
| | | // } |
| | | // } |
| | | //} |
| | | |
| | | return db; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åå»ºæ°æ®åºè¿æ¥å¯¹è±¡ |
| | | /// </summary> |
| | | public ISqlSugarClient Db => _db; |
| | | |
| | | public RepositoryBase(IUnitOfWorkManage unitOfWorkManage) |
| | |
| | | public virtual int AddData(List<TEntity> listEntity) |
| | | { |
| | | IInsertable<TEntity> insert = _db.Insertable(listEntity); |
| | | return insert.ExecuteReturnIdentity(); |
| | | return insert.ExecuteCommand(); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="lstColumns"></param> |
| | | /// <param name="lstIgnoreColumns"></param> |
| | | /// <returns></returns> |
| | | public virtual bool UpdateData(TEntity entity, List<string> lstColumns, List<string> lstIgnoreColumns = null) |
| | | public virtual bool UpdateData(TEntity entity, List<string> lstColumns, List<string>? lstIgnoreColumns = null) |
| | | { |
| | | IUpdateable<TEntity> update = _db.Updateable(entity); |
| | | |
| | |
| | | /// <param name="whereExpression"></param> |
| | | /// <param name="orderByFields"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, string orderByFields) |
| | | public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy) |
| | | { |
| | | return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).ToList(); |
| | | 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> |
| | |
| | | /// <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) |
| | | 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(); |
| | | } |
| | |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual List<TEntity> QueryDataBySql(string sql, SugarParameter[] parameters = null) |
| | | public virtual List<TEntity> QueryDataBySql(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQuery<TEntity>(sql, parameters); |
| | | } |
| | |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual List<dynamic> QueryDynamicDataBySql(string sql, SugarParameter[] parameters = null) |
| | | public virtual List<dynamic> QueryDynamicDataBySql(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQuery<dynamic>(sql, parameters); |
| | | } |
| | |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual List<object> QueryObjectDataBySql(string sql, SugarParameter[] parameters = null) |
| | | public virtual List<object> QueryObjectDataBySql(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.SqlQuery<object>(sql, parameters); |
| | | } |
| | |
| | | /// <param name="sql"></param> |
| | | /// <param name="parameters"></param> |
| | | /// <returns></returns> |
| | | public virtual DataTable QueryTable(string sql, SugarParameter[] parameters = null) |
| | | public virtual DataTable QueryTable(string sql, SugarParameter[]? parameters = null) |
| | | { |
| | | return _db.Ado.GetDataTable(sql, parameters); |
| | | } |
| | |
| | | /// <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) |
| | | 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>() |
| | |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | { |
| | | FieldName = item.Key, |
| | | FieldName = item.Key.FirstLetterToUpper(), |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | |
| | | /// <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) |
| | | 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; |
| | | } |
| | |
| | | /// <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) |
| | | 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) |
| | |
| | | /// <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) |
| | | 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) |
| | |
| | | .ToPageList(pageIndex, pageSize, ref totalCount); |
| | | return new PageGridData<TResult>(totalCount, list); |
| | | } |
| | | |
| | | //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();} |
| | | |
| | | |
| | | public Task<TEntity> QureyDataByIdAsync(object id) |
| | | { |
| | |
| | | .WhereIF(whereExpression != null, whereExpression).ToListAsync(); |
| | | } |
| | | |
| | | /// <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) |
| | | public bool DeleteAndMoveIntoHty(TEntity entity, OperateType operateType) |
| | | { |
| | | List<OrderByModel> orderByModels = new List<OrderByModel>(); |
| | | foreach (var item in orderBy) |
| | | Type type = entity.GetType(); |
| | | Assembly assembly = type.Assembly; |
| | | Type? htyType = assembly.GetType(type.FullName + "_Hty"); |
| | | if (htyType != null) |
| | | { |
| | | OrderByModel orderByModel = new OrderByModel() |
| | | 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) |
| | | { |
| | | FieldName = item.Key, |
| | | OrderByType = item.Value |
| | | }; |
| | | orderByModels.Add(orderByModel); |
| | | 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 _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).ToList(); |
| | | 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, |