优化多查
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Enums;
|
||||
|
||||
namespace Yi.Framework.Core.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
|
||||
public class QueryParameterAttribute:Attribute
|
||||
{
|
||||
public QueryParameterAttribute()
|
||||
{
|
||||
}
|
||||
public QueryParameterAttribute(QueryOperatorEnum queryOperatorEnum)
|
||||
{
|
||||
QueryOperator = queryOperatorEnum;
|
||||
}
|
||||
|
||||
public QueryOperatorEnum QueryOperator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 验证值为0时是否作为查询条件
|
||||
/// true-作为查询条件 false-不作为查询条件
|
||||
/// </summary>
|
||||
public bool VerifyIsZero { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string ColumnName { get; set; }
|
||||
|
||||
public ColumnTypeEnum ColumnType { get; set; }
|
||||
}
|
||||
|
||||
public enum ColumnTypeEnum
|
||||
{
|
||||
datetime,
|
||||
@bool
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Core.Enums
|
||||
{
|
||||
public enum QueryOperatorEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// 相等
|
||||
/// </summary>
|
||||
Equal,
|
||||
/// <summary>
|
||||
/// 匹配
|
||||
/// </summary>
|
||||
Like,
|
||||
/// <summary>
|
||||
/// 大于
|
||||
/// </summary>
|
||||
GreaterThan,
|
||||
/// <summary>
|
||||
/// 大于或等于
|
||||
/// </summary>
|
||||
GreaterThanOrEqual,
|
||||
/// <summary>
|
||||
/// 小于
|
||||
/// </summary>
|
||||
LessThan,
|
||||
/// <summary>
|
||||
/// 小于或等于
|
||||
/// </summary>
|
||||
LessThanOrEqual,
|
||||
/// <summary>
|
||||
/// 等于集合
|
||||
/// </summary>
|
||||
In,
|
||||
/// <summary>
|
||||
/// 不等于集合
|
||||
/// </summary>
|
||||
NotIn,
|
||||
/// <summary>
|
||||
/// 左边匹配
|
||||
/// </summary>
|
||||
LikeLeft,
|
||||
/// <summary>
|
||||
/// 右边匹配
|
||||
/// </summary>
|
||||
LikeRight,
|
||||
/// <summary>
|
||||
/// 不相等
|
||||
/// </summary>
|
||||
NoEqual,
|
||||
/// <summary>
|
||||
/// 为空或空
|
||||
/// </summary>
|
||||
IsNullOrEmpty,
|
||||
/// <summary>
|
||||
/// 不为空
|
||||
/// </summary>
|
||||
IsNot,
|
||||
/// <summary>
|
||||
/// 不匹配
|
||||
/// </summary>
|
||||
NoLike,
|
||||
/// <summary>
|
||||
/// 时间段 值用 "|" 隔开
|
||||
/// </summary>
|
||||
DateRange
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
using Yi.Framework.Core.Helper;
|
||||
using Yi.Framework.Core.Model;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
@@ -101,64 +102,59 @@ where TEntityDto : IEntityDto<TKey>
|
||||
|
||||
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 )
|
||||
var dependsOnbuild = sortInput.GetType().GetCustomAttributes(typeof(QueryParameterAttribute), false).FirstOrDefault() as QueryParameterAttribute;
|
||||
if (dependsOnbuild is null)
|
||||
{
|
||||
foreach (System.Reflection.PropertyInfo item in _properties1)
|
||||
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)
|
||||
{
|
||||
if (vs.Contains(item.Name) || !vs1.Contains(item.PropertyType.Name))
|
||||
continue;
|
||||
object value = item.GetValue(sortInput, null);
|
||||
if (value is null)
|
||||
var query = item.GetCustomAttributes(typeof(QueryParameterAttribute), false).FirstOrDefault() as QueryParameterAttribute;
|
||||
if (query is not null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.PropertyType.Name.StartsWith("Nullable`1"))
|
||||
object value = item.GetValue(sortInput, null);
|
||||
if (value is not null)
|
||||
{
|
||||
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))
|
||||
if (value.ToString() == "0")
|
||||
{
|
||||
sortInput.Conditions.Add(new ConditionalModel { FieldValue = value.ToString(), FieldName = item.Name, ConditionalType = ConditionalType.Like });
|
||||
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:
|
||||
|
||||
}
|
||||
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 });
|
||||
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);
|
||||
}
|
||||
else {
|
||||
entities = await _repository.GetPageListAsync(_=>true, sortInput, sortInput.SortBy, sortInput.SortType);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
|
||||
@@ -3,16 +3,28 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Enums;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
|
||||
namespace Yi.RBAC.Application.Contracts.Identity.Dtos
|
||||
{
|
||||
[QueryParameter]
|
||||
public class DeptGetListInputVo : PagedAllResultRequestDto
|
||||
{
|
||||
[QueryParameter(QueryOperatorEnum.Equal)]
|
||||
public long Id { get; set; }
|
||||
[QueryParameter(QueryOperatorEnum.Equal, ColumnType = ColumnTypeEnum.@bool)]
|
||||
public bool? State { get; set; }
|
||||
[QueryParameter(QueryOperatorEnum.Like)]
|
||||
public string? DeptName { get; set; }
|
||||
public string? DeptCode { get; set; }
|
||||
[QueryParameter(QueryOperatorEnum.Equal)]
|
||||
public string? DeptCode { get; set; }
|
||||
[QueryParameter(QueryOperatorEnum.Equal)]
|
||||
public string? Leader { get; set; }
|
||||
|
||||
[QueryParameter(QueryOperatorEnum.GreaterThanOrEqual, ColumnName = "CreationTime",ColumnType =ColumnTypeEnum.datetime)]
|
||||
public DateTime? StartTime { get; set; }
|
||||
[QueryParameter(QueryOperatorEnum.LessThanOrEqual, ColumnName = "CreationTime", ColumnType = ColumnTypeEnum.datetime)]
|
||||
public DateTime? EndTime { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user