优化(多查)查询功能

This commit is contained in:
小希
2023-02-14 19:01:38 +08:00
parent 158cab9f9b
commit 97bf39f031
7 changed files with 105 additions and 55 deletions

View File

@@ -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)
{

View File

@@ -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; }
}
}

View File

@@ -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; }
}
}

View File

@@ -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);

View File

@@ -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)
{