diff --git a/Tiobon.Core.Api/Controllers/BaseController.cs b/Tiobon.Core.Api/Controllers/BaseController.cs index 51e9f711..38edef0f 100644 --- a/Tiobon.Core.Api/Controllers/BaseController.cs +++ b/Tiobon.Core.Api/Controllers/BaseController.cs @@ -1,4 +1,6 @@ -using Tiobon.Core.Model; +using System.Collections.Generic; +using System.Reflection; +using Tiobon.Core.Model; namespace Tiobon.Core.Controllers { @@ -15,6 +17,132 @@ namespace Tiobon.Core.Controllers } #endregion + + #region 基础接口 + + #region 查询 + /// + /// Ghra_Grade -- 根据条件查询数据 + /// + /// 条件 + /// + [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 }; + } + + /// + /// Ghra_Grade -- 根据Id查询数据 + /// + /// 主键ID + /// + [HttpGet("{Id}")] + 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("获取失败"); + else + return new ServiceResult() { msg = "获取成功", success = true, response = entity }; + } + #endregion + + #region 新增 + /// + /// Ghra_Grade -- 新增数据 + /// + /// + /// + [HttpPost] + public async Task> Post([FromBody] TEditDto insertModel) + { + var data = ServiceResult.OprateSuccess("获取成功", null); + var id = Convert.ToInt64(await InvokeServiceAsync("Add", [insertModel])); + data.success = id > 0; + if (data.success) + data.response = id.ObjToString(); + + return data; + } + #endregion + + #region 更新 + /// + /// Ghra_Grade -- 更新数据 + /// + /// + /// + /// + [HttpPut("{Id}")] + public async Task Put(long Id, [FromBody] TEditDto editModel) + { + var data = ServiceResult.OprateSuccess("更新成功"); + var id = Convert.ToInt64(await InvokeServiceAsync("Update", [editModel])); + if (!data.success) + data.msg = "更新失败"; + + return data; + } + #endregion + + #region 删除 + /// + /// Ghra_Grade -- 删除数据 + /// + /// + /// + [HttpDelete("{Id}")] + public async Task Delete(long Id) + { + var data = ServiceResult.OprateSuccess("删除成功"); + var entity = await QueryById(Id); + if (entity == null) + return ServiceResult.OprateFailed("删除失败"); + + //entity.IsEnable = 0; + data.success = Convert.ToBoolean(await InvokeServiceAsync("Update", [entity])); + if (!data.success) + data.msg = "删除失败"; + return data; + } + #endregion + + #endregion + + /// + /// 反射调用service方法 + /// + /// + /// + /// + private object InvokeService(string methodName, object[] parameters) + { + return _service.GetType().GetMethod(methodName).Invoke(_service, parameters); + } + /// + /// 反射调用service方法 + /// + /// + /// 为要调用重载的方法参数类型:new Type[] { typeof(SaveDataModel) + /// + /// + private object InvokeService(string methodName, Type[] types, object[] parameters) => _service.GetType().GetMethod(methodName, types).Invoke(_service, parameters); + + + private async Task InvokeServiceAsync(string methodName, object[] parameters) + { + var task = _service.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, _service, parameters) as Task; + if (task != null) await task; + var result = task?.GetType().GetProperty("Result")?.GetValue(task); + return result; + } + [NonAction] public ServiceResult Success(T data, string msg = "成功") { diff --git a/Tiobon.Core.Api/Controllers/BaseControllerHelpers.cs b/Tiobon.Core.Api/Controllers/BaseControllerHelpers.cs new file mode 100644 index 00000000..d98db8f2 --- /dev/null +++ b/Tiobon.Core.Api/Controllers/BaseControllerHelpers.cs @@ -0,0 +1,16 @@ +internal static class BaseControllerHelpers +{ + public static TEditDto ConvertToEntity(object obj) where TEditDto : new() + { + TEditDto entity = new TEditDto(); + foreach (var property in typeof(TEditDto).GetProperties()) + { + var value = obj.GetType().GetProperty(property.Name)?.GetValue(obj, null); + if (value != null) + { + property.SetValue(entity, Convert.ChangeType(value, property.PropertyType), null); + } + } + return entity; + } +} \ No newline at end of file diff --git a/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs b/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs index f7e9bff9..f5f7ce58 100644 --- a/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs +++ b/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs @@ -1,109 +1,14 @@ -namespace Tiobon.Core.Api.Controllers +namespace Tiobon.Core.Api.Controllers; + +/// +/// Ghra_Grade +/// +[Route("api/[controller]")] +[ApiController, GlobalActionFilter] +[Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghra)] +public class Ghra_GradeController : BaseController { - /// - /// Ghra_Grade - /// - [Route("api/[controller]")] - [ApiController, GlobalActionFilter] - [Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghra)] - public class Ghra_GradeController : BaseController + public Ghra_GradeController(IGhra_GradeServices service) : base(service) { - public Ghra_GradeController(IGhra_GradeServices service) : base(service) - { - } - - #region 基础接口 - - #region 查询 - /// - /// Ghra_Grade -- 根据条件查询数据 - /// - /// 条件 - /// - [HttpGet] - public async Task>> Get([FromFilter] QueryFilter filter) - { - var response = await _service.QueryFilterPage(filter); - return new ServiceResult>() { msg = "获取成功", success = true, response = response }; - } - - /// - /// Ghra_Grade -- 根据Id查询数据 - /// - /// 主键ID - /// - [HttpGet("{Id}")] - public async Task> Get(string Id) - { - var entity = await _service.QueryById(Id); - if (entity == null) - return ServiceResult.OprateFailed("获取失败"); - else - return new ServiceResult() { msg = "获取成功", success = true, response = entity }; - } - #endregion - - #region 新增 - /// - /// Ghra_Grade -- 新增数据 - /// - /// - /// - [HttpPost] - public async Task> Post([FromBody] InsertGhra_GradeInput insertModel) - { - var data = ServiceResult.OprateSuccess("获取成功", null); - - var id = await _service.Add(insertModel); - data.success = id > 0; - if (data.success) - data.response = id.ObjToString(); - - return data; - } - #endregion - - #region 更新 - /// - /// Ghra_Grade -- 更新数据 - /// - /// - /// - /// - [HttpPut("{Id}")] - public async Task Put(long Id, [FromBody] EditGhra_GradeInput editModel) - { - var data = ServiceResult.OprateSuccess("更新成功"); - data.success = await _service.Update(Id, editModel); - if (!data.success) - data.msg = "更新失败"; - - return data; - } - #endregion - - #region 删除 - /// - /// Ghra_Grade -- 删除数据 - /// - /// - /// - [HttpDelete("{Id}")] - public async Task Delete(long Id) - { - var data = ServiceResult.OprateSuccess("删除成功"); - var entity = await _service.QueryById(Id); - if (entity == null) - return ServiceResult.OprateFailed("删除失败"); - - entity.IsEnable = 0; - data.success = await _service.Update(entity); - if (!data.success) - data.msg = "删除失败"; - return data; - } - #endregion - - #endregion } } \ No newline at end of file diff --git a/Tiobon.Core.Api/Tiobon.Core.xml b/Tiobon.Core.Api/Tiobon.Core.xml index 93434ad1..cb460096 100644 --- a/Tiobon.Core.Api/Tiobon.Core.xml +++ b/Tiobon.Core.Api/Tiobon.Core.xml @@ -9,6 +9,59 @@ + + + Ghra_Grade -- 根据条件查询数据 + + 条件 + + + + + Ghra_Grade -- 根据Id查询数据 + + 主键ID + + + + + Ghra_Grade -- 新增数据 + + + + + + + Ghra_Grade -- 更新数据 + + + + + + + + Ghra_Grade -- 删除数据 + + + + + + + 反射调用service方法 + + + + + + + + 反射调用service方法 + + + 为要调用重载的方法参数类型:new Type[] { typeof(SaveDataModel) + + + 博客管理 @@ -1226,42 +1279,6 @@ Ghra_Grade - - - Ghra_Grade -- 根据条件查询数据 - - 条件 - - - - - Ghra_Grade -- 根据Id查询数据 - - 主键ID - - - - - Ghra_Grade -- 新增数据 - - - - - - - Ghra_Grade -- 更新数据 - - - - - - - - Ghra_Grade -- 删除数据 - - - - 服务管理