From 201c569f605f050f1807756be37d9f761150aa8d Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Tue, 16 Apr 2024 13:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=B1=82=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E5=AE=9E=E4=BD=93(=E6=B3=9B=E5=9E=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tiobon.Core.Api/Controllers/BaseController.cs | 24 +++--- Tiobon.Core.Api/Tiobon.Core.Model.xml | 45 ++++++++++ Tiobon.Core.Model/PageModel.cs | 2 - Tiobon.Core.Model/ServiceResult.cs | 86 ++++++++++++++++++- 4 files changed, 141 insertions(+), 16 deletions(-) diff --git a/Tiobon.Core.Api/Controllers/BaseController.cs b/Tiobon.Core.Api/Controllers/BaseController.cs index 38edef0f..d29771af 100644 --- a/Tiobon.Core.Api/Controllers/BaseController.cs +++ b/Tiobon.Core.Api/Controllers/BaseController.cs @@ -29,7 +29,6 @@ namespace Tiobon.Core.Controllers [HttpGet] public async Task>> QueryByFilter([FromFilter] QueryFilter filter) { - //var response = await _service.QueryFilterPage(filter); var response = (await InvokeServiceAsync("QueryFilterPage", [filter])) as PageModel; return new ServiceResult>() { msg = "获取成功", success = true, response = response }; } @@ -43,13 +42,10 @@ namespace Tiobon.Core.Controllers public async Task> QueryById(long Id) { var entity = (await InvokeServiceAsync("QueryById", [Id])); - - //var entity = ObjectConverter.ConvertToEntity(obj); - //var entity = await _service.QueryById(Id); if (entity == null) - return ServiceResult.OprateFailed("获取失败"); + return Failed("获取失败"); else - return new ServiceResult() { msg = "获取成功", success = true, response = entity }; + return Success(entity, "获取成功"); } #endregion @@ -62,11 +58,13 @@ namespace Tiobon.Core.Controllers [HttpPost] public async Task> Post([FromBody] TEditDto insertModel) { - var data = ServiceResult.OprateSuccess("获取成功", null); + var data = Success(null, "新增成功"); var id = Convert.ToInt64(await InvokeServiceAsync("Add", [insertModel])); data.success = id > 0; if (data.success) data.response = id.ObjToString(); + else + data.msg = "新增失败"; return data; } @@ -82,8 +80,8 @@ namespace Tiobon.Core.Controllers [HttpPut("{Id}")] public async Task Put(long Id, [FromBody] TEditDto editModel) { - var data = ServiceResult.OprateSuccess("更新成功"); - var id = Convert.ToInt64(await InvokeServiceAsync("Update", [editModel])); + var data = Success("更新成功"); + var id = Convert.ToInt64(await InvokeServiceAsync("Update", [Id, editModel])); if (!data.success) data.msg = "更新失败"; @@ -100,10 +98,10 @@ namespace Tiobon.Core.Controllers [HttpDelete("{Id}")] public async Task Delete(long Id) { - var data = ServiceResult.OprateSuccess("删除成功"); + var data = Success("删除成功"); var entity = await QueryById(Id); if (entity == null) - return ServiceResult.OprateFailed("删除失败"); + return Failed("删除失败"); //entity.IsEnable = 0; data.success = Convert.ToBoolean(await InvokeServiceAsync("Update", [entity])); @@ -166,9 +164,9 @@ namespace Tiobon.Core.Controllers } [NonAction] - public ServiceResult Failed(string msg = "失败", int status = 500) + public ServiceResult Failed(string msg = "失败", int status = 500) { - return new ServiceResult() { success = false, status = status, msg = msg, response = null, }; + return new ServiceResult() { success = false, status = status, msg = msg, response = null, }; } [NonAction] diff --git a/Tiobon.Core.Api/Tiobon.Core.Model.xml b/Tiobon.Core.Api/Tiobon.Core.Model.xml index 4753b07f..f2915d11 100644 --- a/Tiobon.Core.Api/Tiobon.Core.Model.xml +++ b/Tiobon.Core.Api/Tiobon.Core.Model.xml @@ -2232,6 +2232,51 @@ + + + 通用分页信息类 + + + + + 状态码 + + + + + 操作是否成功 + + + + + 返回信息 + + + + + 当前页标 + + + + + 总页数 + + + + + 数据总数 + + + + + 每页大小 + + + + + 返回数据 + + 数据库读取类型 diff --git a/Tiobon.Core.Model/PageModel.cs b/Tiobon.Core.Model/PageModel.cs index 86e1faaf..10b8c5e8 100644 --- a/Tiobon.Core.Model/PageModel.cs +++ b/Tiobon.Core.Model/PageModel.cs @@ -1,6 +1,4 @@ using AutoMapper; -using System; -using System.Collections.Generic; namespace Tiobon.Core.Model { diff --git a/Tiobon.Core.Model/ServiceResult.cs b/Tiobon.Core.Model/ServiceResult.cs index e044700a..bf87cd79 100644 --- a/Tiobon.Core.Model/ServiceResult.cs +++ b/Tiobon.Core.Model/ServiceResult.cs @@ -1,4 +1,6 @@ -namespace Tiobon.Core.Model; +using AutoMapper; + +namespace Tiobon.Core.Model; /// /// 服务层响应实体(泛型) @@ -130,4 +132,86 @@ public class ServiceResult response = null }; } +} + + +/// +/// 服务层分页响应实体(泛型) +/// +public class ServicPageeResult +{ + /// + /// 状态码 + /// + public int Status { get; set; } = 200; + /// + /// 操作是否成功 + /// + public bool Success { get; set; } = false; + /// + /// 返回信息 + /// + public string Message { get; set; } = null; + /// + /// 当前页标 + /// + public int Page { get; set; } = 1; + /// + /// 总页数 + /// + public int PageCount => (int)Math.Ceiling((decimal)TotalCount / PageSize); + /// + /// 数据总数 + /// + public int TotalCount { get; set; } = 0; + /// + /// 每页大小 + /// + public int PageSize { set; get; } = 20; + /// + /// 返回数据 + /// + public List Data { get; set; } + + public ServicPageeResult() { } + + public ServicPageeResult(int page, int totalCount, int pageSize, List data) + { + this.Page = page; + this.TotalCount = totalCount; + PageSize = pageSize; + this.Data = data; + } + + public PageModel ConvertTo() + { + return new PageModel(Page, TotalCount, PageSize, default); + } + + + public PageModel ConvertTo(IMapper mapper) + { + var model = ConvertTo(); + + if (Data != null) + { + model.data = mapper.Map>(Data); + } + + return model; + } + + + public PageModel ConvertTo(IMapper mapper, Action options) + { + var model = ConvertTo(); + if (Data != null) + { + model.data = mapper.Map>(Data, options); + } + + return model; + + } + } \ No newline at end of file