From 9274d88c76d890150767764ff5e33fd7319d9d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=B8=8C?= <小希2899451377@qq.com> Date: Wed, 15 Feb 2023 15:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=9A=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Attributes/QueryParameterAttribute.cs | 42 +++++++++ .../Enums/QueryOperatorEnum.cs | 72 ++++++++++++++++ .../Services/ReadOnlyAppService.cs | 86 +++++++++---------- .../Identity/Dtos/Dept/DeptGetListInputVo.cs | 14 ++- 4 files changed, 168 insertions(+), 46 deletions(-) create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/QueryParameterAttribute.cs create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Core/Enums/QueryOperatorEnum.cs diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/QueryParameterAttribute.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/QueryParameterAttribute.cs new file mode 100644 index 00000000..20c1740e --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Attributes/QueryParameterAttribute.cs @@ -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; } + + /// + /// 验证值为0时是否作为查询条件 + /// true-作为查询条件 false-不作为查询条件 + /// + public bool VerifyIsZero { get; set; } = false; + + /// + /// + /// + public string ColumnName { get; set; } + + public ColumnTypeEnum ColumnType { get; set; } + } + + public enum ColumnTypeEnum + { + datetime, + @bool + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Enums/QueryOperatorEnum.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Enums/QueryOperatorEnum.cs new file mode 100644 index 00000000..13439675 --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core/Enums/QueryOperatorEnum.cs @@ -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 + { + /// + /// 相等 + /// + Equal, + /// + /// 匹配 + /// + Like, + /// + /// 大于 + /// + GreaterThan, + /// + /// 大于或等于 + /// + GreaterThanOrEqual, + /// + /// 小于 + /// + LessThan, + /// + /// 小于或等于 + /// + LessThanOrEqual, + /// + /// 等于集合 + /// + In, + /// + /// 不等于集合 + /// + NotIn, + /// + /// 左边匹配 + /// + LikeLeft, + /// + /// 右边匹配 + /// + LikeRight, + /// + /// 不相等 + /// + NoEqual, + /// + /// 为空或空 + /// + IsNullOrEmpty, + /// + /// 不为空 + /// + IsNot, + /// + /// 不匹配 + /// + NoLike, + /// + /// 时间段 值用 "|" 隔开 + /// + DateRange + } +} diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/ReadOnlyAppService.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/ReadOnlyAppService.cs index 8a2bd75b..49f5743c 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/ReadOnlyAppService.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Ddd/Services/ReadOnlyAppService.cs @@ -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 if (input is IPagedAndSortedResultRequestDto sortInput) { - sortInput.Conditions = new List(); - 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(); + 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 diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Dept/DeptGetListInputVo.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Dept/DeptGetListInputVo.cs index 2a65631f..c414ccdb 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Dept/DeptGetListInputVo.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application.Contracts/Identity/Dtos/Dept/DeptGetListInputVo.cs @@ -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; } } }