优化(多查)查询功能
This commit is contained in:
@@ -43,7 +43,11 @@ 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)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -14,5 +15,7 @@ namespace Yi.Framework.Ddd.Dtos
|
||||
string? SortBy { get; set; }
|
||||
|
||||
OrderByEnum SortType { get; set; }
|
||||
List<IConditionalModel> Conditions { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -13,5 +14,6 @@ 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; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,8 @@ 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);
|
||||
|
||||
@@ -90,36 +90,83 @@ where TEntityDto : IEntityDto<TKey>
|
||||
var entityDtos = new List<TGetListOutputDto>();
|
||||
|
||||
bool isPageList = true;
|
||||
|
||||
//if (totalCount > 0)
|
||||
//{
|
||||
|
||||
//这里还可以追加如果是审计日志,继续拼接条件即可
|
||||
if (input is IPageTimeResultRequestDto timeInput)
|
||||
//这里还可以追加如果是审计日志,继续拼接条件即可
|
||||
if (input is IPageTimeResultRequestDto timeInput)
|
||||
{
|
||||
if (timeInput.StartTime is not null)
|
||||
{
|
||||
if (timeInput.StartTime is not null)
|
||||
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
if (input is IPagedAndSortedResultRequestDto sortInput)
|
||||
{
|
||||
sortInput.Conditions = new List<IConditionalModel>();
|
||||
System.Reflection.PropertyInfo[] properties = sortInput.GetType().GetProperties();
|
||||
string[] vs = new string[] { "PageNum", "PageSize", "SortBy", "SortType", "Conditions" };
|
||||
string[] vs1 = new string[] { "Int32", "Int64", "Double", "Decimal", "String", "Nullable`1" };
|
||||
var diffproperties = properties.Where(p => !vs.Select(v => v).Contains(p.Name)).ToArray();
|
||||
var _properties1 = properties.Where(p => vs1.Select(v => v).Contains(p.PropertyType.Name)).ToArray();
|
||||
if (_properties1.Count() > 0 && diffproperties.Count() > 0 )
|
||||
{
|
||||
foreach (System.Reflection.PropertyInfo item in _properties1)
|
||||
{
|
||||
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
|
||||
if (vs.Contains(item.Name) || !vs1.Contains(item.PropertyType.Name))
|
||||
continue;
|
||||
object value = item.GetValue(sortInput, null);
|
||||
if (value is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.PropertyType.Name.StartsWith("Nullable`1"))
|
||||
{
|
||||
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = ConditionalType.Equal });
|
||||
}
|
||||
if (item.PropertyType.Name.StartsWith("Int64"))
|
||||
{
|
||||
|
||||
if ((long)value == (long)0)
|
||||
continue;
|
||||
else
|
||||
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = ConditionalType.Equal });
|
||||
}
|
||||
if (item.PropertyType.Name.StartsWith("String"))
|
||||
{
|
||||
if (!string.IsNullOrEmpty((string)value))
|
||||
{
|
||||
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = ConditionalType.Like });
|
||||
}
|
||||
|
||||
}
|
||||
if (item.PropertyType.Name.StartsWith("DateTime"))
|
||||
{
|
||||
if (item.Name == "StartTime")
|
||||
{
|
||||
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = ConditionalType.GreaterThanOrEqual });
|
||||
}
|
||||
else if (item.Name == "EndTime")
|
||||
{
|
||||
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = ConditionalType.LessThanOrEqual });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
entities = await _repository.GetPageListAsync(sortInput.Conditions, sortInput, sortInput.SortBy, sortInput.SortType);
|
||||
}
|
||||
else {
|
||||
entities = await _repository.GetPageListAsync(_=>true, sortInput, sortInput.SortBy, sortInput.SortType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (input is IPagedAndSortedResultRequestDto sortInput)
|
||||
{
|
||||
entities = await _repository.GetPageListAsync(_ => true, sortInput,sortInput.SortBy, sortInput.SortType);
|
||||
}
|
||||
|
||||
|
||||
else
|
||||
{
|
||||
isPageList = false;
|
||||
entities = await _repository.GetListAsync();
|
||||
}
|
||||
entityDtos = await MapToGetListOutputDtosAsync(entities);
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
isPageList = false;
|
||||
entities = await _repository.GetListAsync();
|
||||
}
|
||||
entityDtos = await MapToGetListOutputDtosAsync(entities);
|
||||
//如果是分页查询,还需要统计数量
|
||||
if (isPageList)
|
||||
{
|
||||
|
||||
@@ -84,13 +84,6 @@
|
||||
</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服务实现
|
||||
|
||||
@@ -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
|
||||
// };
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user