refactor: 基础设施

This commit is contained in:
橙子
2023-02-19 11:34:15 +08:00
parent 961634981a
commit 8eda2cd814
9 changed files with 75 additions and 111 deletions

View File

@@ -12,8 +12,9 @@ using Yi.Framework.Core.Exceptions;
namespace Microsoft.AspNetCore.Builder
{
internal class ExceptionModle
internal class ExceptionModle
{
public int Code { get; set; }
public string? Message { get; set; }
public string? Details { get; set; }
}
@@ -36,10 +37,11 @@ namespace Microsoft.AspNetCore.Builder
catch (BusinessException businessEx)
{
context.Response.ContentType = "application/json;charset=utf-8";
context.Response.StatusCode = businessEx.Code.GetHashCode();
//context.Response.StatusCode = businessEx.Code.GetHashCode();
var result = new ExceptionModle
{
Code = businessEx.Code.GetHashCode(),
Message = businessEx.Message,
Details = businessEx.Details,
};
@@ -57,10 +59,11 @@ namespace Microsoft.AspNetCore.Builder
//系统错误,记录日志
_logger.LogError(ex, $"授权失败:{ex.Message}");
//await _errorHandle.Invoer(context, ex);
context.Response.StatusCode =(int)ex.Code;
context.Response.StatusCode = (int)ex.Code;
//系统错误,需要记录
var result = new ExceptionModle
{
Code = ex.Code.GetHashCode(),
Message = ex.Message,
Details = "授权失败",
};
@@ -81,6 +84,7 @@ namespace Microsoft.AspNetCore.Builder
//系统错误,需要记录
var result = new ExceptionModle
{
Code = 500,
Message = ex.Message,
Details = "系统错误",
};

View File

@@ -43,11 +43,7 @@ namespace Yi.Framework.Core.Sqlsugar.Repositories
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageNum, page.PageSize);
}
public async Task<List<T>> GetPageListAsync(List<IConditionalModel> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageNum, page.PageSize);
}
public async Task<bool> UpdateIgnoreNullAsync(T updateObj)
{

View File

@@ -1,5 +1,4 @@
using SqlSugar;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -15,7 +14,5 @@ namespace Yi.Framework.Ddd.Dtos
string? SortBy { get; set; }
OrderByEnum SortType { get; set; }
List<IConditionalModel> Conditions { get; set; }
}
}

View File

@@ -1,5 +1,4 @@
using SqlSugar;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -14,6 +13,5 @@ namespace Yi.Framework.Ddd.Dtos
public int PageSize { get; set; } = int.MaxValue;
public string? SortBy { get; set; }
public OrderByEnum SortType { get; set; } = OrderByEnum.Desc;
public List<IConditionalModel> Conditions { get; set; }
}
}

View File

@@ -32,8 +32,7 @@ namespace Yi.Framework.Ddd.Repositories
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(List<IConditionalModel> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
//插入
Task<bool> InsertAsync(T insertObj);

View File

@@ -91,86 +91,36 @@ where TEntityDto : IEntityDto<TKey>
var entityDtos = new List<TGetListOutputDto>();
bool isPageList = true;
//这里还可以追加如果是审计日志,继续拼接条件即可
if (input is IPageTimeResultRequestDto timeInput)
{
if (timeInput.StartTime is not null)
{
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
}
}
if (input is IPagedAndSortedResultRequestDto sortInput)
{
var dependsOnbuild = sortInput.GetType().GetCustomAttributes(typeof(QueryParameterAttribute), false).FirstOrDefault() as QueryParameterAttribute;
if (dependsOnbuild is null)
//if (totalCount > 0)
//{
//这里还可以追加如果是审计日志,继续拼接条件即可
if (input is IPageTimeResultRequestDto timeInput)
{
entities = await _repository.GetPageListAsync(_ => true, sortInput, sortInput.SortBy, sortInput.SortType);
if (timeInput.StartTime is not null)
{
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
}
}
if (input is IPagedAndSortedResultRequestDto sortInput)
{
entities = await _repository.GetPageListAsync(_ => true, sortInput,sortInput.SortBy, sortInput.SortType);
}
else
{
sortInput.Conditions = new List<IConditionalModel>();
System.Reflection.PropertyInfo[] properties = sortInput.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
var query = item.GetCustomAttributes(typeof(QueryParameterAttribute), false).FirstOrDefault() as QueryParameterAttribute;
if (query is not null)
{
object value = item.GetValue(sortInput, null);
if (value is not null)
{
if (value.ToString() == "0")
{
if (query.VerifyIsZero)
{
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
}
}
else {
switch (query.ColumnType)
{
case ColumnTypeEnum.datetime:
if (!string.IsNullOrEmpty(query.ColumnName))
{
DateTime dt = DateTime.Now;
DateTime.TryParse(value.ToString(), out dt);
sortInput.Conditions.Add(new ConditionalModel { FieldValue = dt.ToString("yyyy-MM-dd HH:mm:ss"), FieldName = query.ColumnName, ConditionalType = (ConditionalType)(int)query.QueryOperator });
}
else
{
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
}
break;
case ColumnTypeEnum.@bool:
string _Value = "";
if ((bool)value)
{
_Value = "1";
}
else {
_Value = "0";
}
sortInput.Conditions.Add(new ConditionalModel { FieldValue = _Value, FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
break;
default:
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = (ConditionalType)(int)query.QueryOperator });
break;
}
}
}
}
}
entities = await _repository.GetPageListAsync(sortInput.Conditions, sortInput, sortInput.SortBy, sortInput.SortType);
isPageList = false;
entities = await _repository.GetListAsync();
}
}
else
{
isPageList = false;
entities = await _repository.GetListAsync();
}
entityDtos = await MapToGetListOutputDtosAsync(entities);
entityDtos = await MapToGetListOutputDtosAsync(entities);
//}
//如果是分页查询,还需要统计数量
if (isPageList)
{

View File

@@ -84,6 +84,13 @@
</summary>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.DeptService.GetListAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.DeptGetListInputVo)">
<summary>
多查
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="T:Yi.RBAC.Application.Identity.MenuService">
<summary>
Menu服务实现

View File

@@ -28,24 +28,24 @@ namespace Yi.RBAC.Application.Identity
return await MapToGetListOutputDtosAsync(entities);
}
///// <summary>
///// 多查
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
//{
// RefAsync<int> total = 0;
// var entities = await _DbQueryable
// .WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
// .WhereIF(input.State is not null, u => u.State == input.State)
// .OrderBy(u => u.OrderNum, OrderByType.Asc)
// .ToPageListAsync(input.PageNum, input.PageSize, total);
// return new PagedResultDto<DeptGetListOutputDto>
// {
// Items = await MapToGetListOutputDtosAsync(entities),
// Total = total
// };
//}
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
{
RefAsync<int> total = 0;
var entities = await _DbQueryable
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
.WhereIF(input.State is not null, u => u.State == input.State)
.OrderBy(u => u.OrderNum, OrderByType.Asc)
.ToPageListAsync(input.PageNum, input.PageSize, total);
return new PagedResultDto<DeptGetListOutputDto>
{
Items = await MapToGetListOutputDtosAsync(entities),
Total = total
};
}
}
}

View File

@@ -104,6 +104,14 @@ namespace Yi.RBAC.Domain.Identity
return claims;
}
/// <summary>
/// 更新密码
/// </summary>
/// <param name="userId"></param>
/// <param name="newPassword"></param>
/// <param name="oldPassword"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task UpdatePasswordAsync(long userId, string newPassword, string oldPassword)
{
var user = await _repository.GetByIdAsync(userId);
@@ -117,7 +125,12 @@ namespace Yi.RBAC.Domain.Identity
await _repository.UpdateAsync(user);
}
/// <summary>
/// 重置密码
/// </summary>
/// <param name="userId"></param>
/// <param name="password"></param>
/// <returns></returns>
public async Task<bool> RestPasswordAsync(long userId, string password)
{
var user = await _repository.GetByIdAsync(userId);