diff --git a/Tiobon.Core.Api/Controllers/Base/BaseController.cs b/Tiobon.Core.Api/Controllers/Base/BaseController.cs index f425ad58..6138bf97 100644 --- a/Tiobon.Core.Api/Controllers/Base/BaseController.cs +++ b/Tiobon.Core.Api/Controllers/Base/BaseController.cs @@ -30,12 +30,12 @@ public class BaseController /// 根据条件查询数据 /// - /// 条件 + /// 条件 /// [HttpPost, Route("Query")] - public virtual async Task> QueryByFilter([FromFilter] QueryFilter filter) + public virtual async Task> QueryByFilter([FromBody] QueryBody body) { - var data = (await InvokeServiceAsync("QueryFilterPage", [filter])) as ServicePageResult; + var data = (await InvokeServiceAsync("QueryFilterPage", [body])) as ServicePageResult; return data; } diff --git a/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs b/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs index df07e413..048508fc 100644 --- a/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs +++ b/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs @@ -16,36 +16,6 @@ public class Ghre_QuestionController : BaseController - /// 根据条件查询数据 - /// - /// 条件 - /// - [HttpPost, Route("Query")] - public override async Task> QueryByFilter([FromFilter] QueryFilter filter) - { - var data = await _service.QueryFilterPage(filter); - return data; - } - - /// - /// 根据Id查询数据 - /// - /// 主键ID - /// - [HttpPost("Query/{Id}")] - public override async Task> QueryById(long Id) - { - var entity = await _service.QueryById(Id); - if (entity == null) - return Failed("获取失败"); - else - return Success(entity, "获取成功"); - } - #endregion - #region 新增 /// /// 新增数据 diff --git a/Tiobon.Core.Api/Tiobon.Core.xml b/Tiobon.Core.Api/Tiobon.Core.xml index bdf3be3c..dd91cc29 100644 --- a/Tiobon.Core.Api/Tiobon.Core.xml +++ b/Tiobon.Core.Api/Tiobon.Core.xml @@ -79,11 +79,11 @@ 初始化 (注入) - + 根据条件查询数据 - 条件 + 条件 @@ -525,20 +525,6 @@ 题目(Controller) - - - 根据条件查询数据 - - 条件 - - - - - 根据Id查询数据 - - 主键ID - - 新增数据 diff --git a/Tiobon.Core.Common/Attribute/QueryFilter.cs b/Tiobon.Core.Common/Attribute/QueryFilter.cs index b5f0b8d3..605f685e 100644 --- a/Tiobon.Core.Common/Attribute/QueryFilter.cs +++ b/Tiobon.Core.Common/Attribute/QueryFilter.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using Newtonsoft.Json.Linq; namespace Tiobon.Core.Common; @@ -58,4 +59,65 @@ public class QueryFilter Sorting = string.Empty, Conditions = string.Empty }; +} + +/// +/// 动态查询条件 +/// +public class QueryBody +{ + private int _pageNum; + /// + /// 起始位置(e.g. 0) + /// + [Required] + public int pageNum + { + get { return _pageNum; } + set + { + //前端默认从分页显示默认1开始,所以后端需要-1 + if (value >= 1) + value -= 1; + _pageNum = value; + } + } + /// + /// 每页数量(e.g. 10) + /// + [Required] + public int pageSize { get; set; } + private JObject _jsonParam; + /// + /// 查询条件( 例如:id = 1 and name = 小明) + /// + public JObject jsonParam + { + get { return _jsonParam; } + set + { + _jsonParam = value; + } + } + + /// + /// 排序条件表达式(e.g. LoginName ASC,Name DESC) + /// + public string Sorting { get; set; } + /// + /// 缺省值 + /// + public static QueryBody Default => new QueryBody + { + pageNum = 1, + pageSize = 100000, + Sorting = string.Empty, + jsonParam = default + }; +} +public class JsonParam +{ + public string columnValue { get; set; } + public string operationKey { get; set; } + } \ No newline at end of file diff --git a/Tiobon.Core.IServices/BASE/IBaseServices.cs b/Tiobon.Core.IServices/BASE/IBaseServices.cs index 7ed410d0..c440fc4e 100644 --- a/Tiobon.Core.IServices/BASE/IBaseServices.cs +++ b/Tiobon.Core.IServices/BASE/IBaseServices.cs @@ -1,5 +1,6 @@ using System.Data; using System.Linq.Expressions; +using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Model; @@ -139,7 +140,7 @@ namespace Tiobon.Core.IServices.BASE Task> QueryPage(Expression> whereExpression, int pageIndex = 1, int pageSize = 20, string orderByFields = null); - Task> QueryFilterPage([FromFilter] QueryFilter filter); + Task> QueryFilterPage([FromBody] QueryBody body); Task> QueryMuch( Expression> joinExpression, diff --git a/Tiobon.Core.Repository/BASE/BaseRepository.cs b/Tiobon.Core.Repository/BASE/BaseRepository.cs index 4b13ef64..45494bd5 100644 --- a/Tiobon.Core.Repository/BASE/BaseRepository.cs +++ b/Tiobon.Core.Repository/BASE/BaseRepository.cs @@ -1,6 +1,8 @@ using System.Data; using System.Linq.Expressions; using System.Reflection; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Common.DB; @@ -465,19 +467,42 @@ namespace Tiobon.Core.Repository.Base /// 页大小 /// 排序字段,如name asc,age desc /// - public async Task> QueryFilterPage([FromFilter] QueryFilter filter) + public async Task> QueryFilterPage(QueryBody filter) { RefAsync totalCount = 0; var query = _db.Queryable(); - if (!filter.Conditions.IsNullOrEmpty()) - query = query.Where(filter.Conditions); + string conditions = "1=1"; + + + foreach (JProperty jProperty in filter.jsonParam.Properties()) + { + var name = jProperty.Name; + var value = jProperty.Value.ToString(); + if (!string.IsNullOrWhiteSpace(value)) + { + var jsonParam = JsonConvert.DeserializeObject(value); + + switch (jsonParam.operationKey) + { + case "Include": + conditions += $" AND {name} ='{jsonParam.columnValue}'"; + break; + case "NotInclude": + conditions += $" AND {name} !='{jsonParam.columnValue}'"; + break; + default: + break; + } + } + } + query = query.Where(conditions); var list = await query .OrderByIF(!string.IsNullOrEmpty(filter.Sorting), filter.Sorting) - .ToPageListAsync(filter.PageIndex, filter.PageSize, totalCount); + .ToPageListAsync(filter.pageNum, filter.pageSize, totalCount); - return new ServicePageResult(filter.PageIndex, totalCount, filter.PageSize, list); + return new ServicePageResult(filter.pageNum, totalCount, filter.pageSize, list); } diff --git a/Tiobon.Core.Repository/BASE/IBaseRepository.cs b/Tiobon.Core.Repository/BASE/IBaseRepository.cs index 04bf9ef9..6850697e 100644 --- a/Tiobon.Core.Repository/BASE/IBaseRepository.cs +++ b/Tiobon.Core.Repository/BASE/IBaseRepository.cs @@ -183,7 +183,7 @@ namespace Tiobon.Core.IRepository.Base /// /// /// - Task> QueryFilterPage([FromFilter] QueryFilter filter); + Task> QueryFilterPage(QueryBody filter); /// /// 三表联查 diff --git a/Tiobon.Core.Services/BASE/BaseServices.cs b/Tiobon.Core.Services/BASE/BaseServices.cs index 23c8be48..7a19b03d 100644 --- a/Tiobon.Core.Services/BASE/BaseServices.cs +++ b/Tiobon.Core.Services/BASE/BaseServices.cs @@ -3,6 +3,7 @@ using System.Linq.Expressions; using System.Reflection; using AgileObjects.AgileMapper; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Common.DB.Dapper; @@ -781,11 +782,11 @@ public class BaseServices : IBaseServ return await BaseDal.QueryPage(whereExpression, pageIndex, pageSize, orderByFileds); } - public async Task> QueryFilterPage([FromFilter] QueryFilter filter) + public async Task> QueryFilterPage(QueryBody body) { - var data = await BaseDal.QueryFilterPage(filter); + var data = await BaseDal.QueryFilterPage(body); - return new ServicePageResult(filter.PageIndex, data.result.DT_TablePageInfoT1.TotalCount, filter.PageSize, Mapper.Map(data.result.DT_TableDataT1).ToANew>()); + return new ServicePageResult(body.pageNum, data.result.DT_TablePageInfoT1.TotalCount, body.pageSize, Mapper.Map(data.result.DT_TableDataT1).ToANew>()); } diff --git a/Tiobon.Core/Tiobon.Core.xml b/Tiobon.Core/Tiobon.Core.xml index bdf3be3c..dd91cc29 100644 --- a/Tiobon.Core/Tiobon.Core.xml +++ b/Tiobon.Core/Tiobon.Core.xml @@ -79,11 +79,11 @@ 初始化 (注入) - + 根据条件查询数据 - 条件 + 条件 @@ -525,20 +525,6 @@ 题目(Controller) - - - 根据条件查询数据 - - 条件 - - - - - 根据Id查询数据 - - 主键ID - - 新增数据