From 114af793a433a93b85133f9012cdf511eba30ecb Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Fri, 10 May 2024 18:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Ghre/Ghre_CourseClassController.cs | 246 +++++++++++++++++- Tiobon.Core.Api/Tiobon.Core.xml | 100 +++++++ Tiobon.Core.Services/CommonServices.cs | 32 ++- Tiobon.Core/Tiobon.Core.xml | 100 +++++++ 4 files changed, 461 insertions(+), 17 deletions(-) diff --git a/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseClassController.cs b/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseClassController.cs index bfdcdb2d..1d8c308e 100644 --- a/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseClassController.cs +++ b/Tiobon.Core.Api/Controllers/Ghre/Ghre_CourseClassController.cs @@ -1,4 +1,7 @@ -namespace Tiobon.Core.Api.Controllers; +using SqlSugar; +using Mapper = AgileObjects.AgileMapper.Mapper; + +namespace Tiobon.Core.Api.Controllers; /// /// 课程分类(Controller) @@ -6,9 +9,246 @@ [Route("api/[controller]")] [ApiController, GlobalActionFilter] [Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghre)] -public class Ghre_CourseClassController : BaseController +public class Ghre_CourseClassController : BaseApiController +//public class Ghre_CourseClassController : BaseController +{ + protected IGhre_CourseClassServices _service; + + public Ghre_CourseClassController(IGhre_CourseClassServices service) + { + _service = service; + } + + //public Ghre_CourseClassController(IGhre_CourseClassServices service) : base(service) { } + + #region 基础接口 + + #region 查询 + /// + /// 根据条件查询数据 + /// + /// 条件 + /// + [HttpGet] + public async Task> QueryByFilter([FromFilter] QueryFilter filter) + { + //var data = await _service.QueryFilterPage(); + RefAsync totalCount = 0; + var query = _service.Db.Queryable(); + if (!filter.Conditions.IsNullOrEmpty()) + query = query.Where(filter.Conditions); + var list = await query + .OrderByIF(!string.IsNullOrEmpty(filter.Sorting), filter.Sorting) + .ToPageListAsync(filter.PageIndex, filter.PageSize, totalCount); + + return new ServicePageResult1(filter.PageIndex, totalCount, filter.PageSize, Mapper.Map(list).ToANew>()); + + } + + public static T ConvertTo(object input) + { + if (input == null) + { + throw new ArgumentNullException(nameof(input)); + } + + // 当T是Nullable类型时,需要获取其基础类型 + Type targetType = typeof(T); + Type nullableType = Nullable.GetUnderlyingType(targetType); + targetType = nullableType ?? targetType; + + // 检查input是否已经是T类型 + if (input is T) + { + return (T)input; + } + + // 尝试转换 + try + { + return (T)Convert.ChangeType(input, targetType); + } + catch (InvalidCastException) + { + throw new InvalidOperationException($"Cannot convert an instance of type {input.GetType().Name} to type {typeof(T).Name}."); + } + } + + /// + /// 根据Id查询数据 + /// + /// 主键ID + /// + [HttpGet("{Id}")] + public virtual async Task> QueryById(long Id) + { + var entity1 = await _service.QueryById(Id); + var entity = ConvertTo(entity1); + if (entity is null) + return new ServiceResult() { Success = false, Status = 500, Message = "获取失败", Data = default }; + else + return new ServiceResult() { Success = false, Message = "获取成功", Data = entity }; + + } + #endregion + + #region 新增 + /// + /// 新增数据 + /// + /// + /// + [HttpPost] + public virtual async Task> Insert([FromBody] InsertGhre_CourseClassInput insertModel) + { + var data = new ServiceResult() { Success = true, Message = "新增成功", Data = null }; + + var id = await _service.Add(insertModel); + data.Success = id > 0; + if (data.Success) + data.Data = id.ObjToString(); + else + return new ServiceResult() { Success = false, Status = 500, Message = "新增失败", Data = default }; + return data; + } + + /// + /// 批量新增数据 + /// + /// + [HttpPost, Route("BulkInsert")] + public virtual async Task>> BulkInsert([FromBody] List insertModels) + { + var data = new ServiceResult>() { Success = true, Message = "新增成功", Data = null }; + var ids = await _service.Add(insertModels); + data.Success = ids.Any(); + if (data.Success) + data.Data = ids; + else + return new ServiceResult>() { Success = false, Status = 500, Message = "新增失败", Data = default }; + + return data; + } + + #endregion + + #region 更新 + /// + /// 更新数据 + /// + /// 主键ID + /// + /// + [HttpPut("{Id}")] + public virtual async Task Put(long Id, [FromBody] EditGhre_CourseClassInput editModel) + { + var data = new ServiceResult() { Success = true, Message = "更新成功", Data = null }; + var flag = await _service.Update(Id, editModel); + if (!flag) + return new ServiceResult() { Success = false, Status = 500, Message = "更新失败", Data = default }; + return data; + } + /// + /// 批量更新数据 + /// + /// + [HttpPut, Route("BulkUpdate")] + public virtual async Task BulkUpdate([FromBody] Dictionary editModels) + { + var data = new ServiceResult() { Success = true, Message = "更新成功", Data = null }; + var flag = await _service.Update(editModels); + if (!flag) + return new ServiceResult() { Success = false, Status = 500, Message = "更新失败", Data = default }; + + return data; + } + #endregion + + #region 删除 + /// + /// 删除数据 + /// + /// 主键ID + /// + [HttpDelete("{Id}")] + public virtual async Task Delete(long Id) + { + var data = new ServiceResult() { Success = true, Message = "删除成功", Data = null }; + var isExist = await _service.AnyAsync(Id); + if (!isExist) + return new ServiceResult() { Success = false, Status = 500, Message = "无效的数据ID", Data = default }; + data.Success = await _service.DeleteById1(Id); ; + if (!data.Success) + return new ServiceResult() { Success = false, Status = 500, Message = "删除失败", Data = default }; + return data; + } + + /// + /// 批量删除数据 + /// + /// 主键IDs + /// + [HttpDelete, Route("BulkDelete")] + public virtual async Task BulkDelete([FromBody] long[] Ids) + { + var data = new ServiceResult() { Success = true, Message = "删除成功", Data = null }; + data.Success = await _service.DeleteById1(Ids); + if (!data.Success) + return new ServiceResult() { Success = false, Status = 500, Message = "删除失败", Data = default }; + return data; + } + #endregion + + #endregion +} + + +/// +/// 服务层分页响应实体(泛型) +/// +public class ServicePageResult1 { - public Ghre_CourseClassController(IGhre_CourseClassServices service) : base(service) + /// + /// 状态码 + /// + 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 ServicePageResult1() { } + + public ServicePageResult1(int page, int totalCount, int pageSize, List data) { + Success = true; + this.Page = page; + this.TotalCount = totalCount; + PageSize = pageSize; + this.Data = data; } } \ No newline at end of file diff --git a/Tiobon.Core.Api/Tiobon.Core.xml b/Tiobon.Core.Api/Tiobon.Core.xml index d73b5de9..95ca044c 100644 --- a/Tiobon.Core.Api/Tiobon.Core.xml +++ b/Tiobon.Core.Api/Tiobon.Core.xml @@ -528,6 +528,106 @@ 课程分类(Controller) + + + 根据条件查询数据 + + 条件 + + + + + 根据Id查询数据 + + 主键ID + + + + + 新增数据 + + + + + + + 批量新增数据 + + + + + + 更新数据 + + 主键ID + + + + + + 批量更新数据 + + + + + + 删除数据 + + 主键ID + + + + + 批量删除数据 + + 主键IDs + + + + + 服务层分页响应实体(泛型) + + + + + 状态码 + + + + + 操作是否成功 + + + + + 返回信息 + + + + + 当前页标 + + + + + 总页数 + + + + + 数据总数 + + + + + 每页大小 + + + + + 返回数据 + + Ghre_Course(Controller) diff --git a/Tiobon.Core.Services/CommonServices.cs b/Tiobon.Core.Services/CommonServices.cs index e37a3084..8d57e722 100644 --- a/Tiobon.Core.Services/CommonServices.cs +++ b/Tiobon.Core.Services/CommonServices.cs @@ -103,6 +103,23 @@ public partial class CommonServices : BaseServices>, ICommon { var result = new ModuleReturn(); + //SELECT NULL fnKeySeq, + // 'SetDefault' fnKey, + // [dbo].[FLangKeyToValue]('GHR_Common_000000',{2},'恢复默认') fnTitle, + // 'table' fnType, + // 'left' position, + // 'ghr-icon-setDefault' icon + //UNION ALL + + + //UNION ALL + // SELECT NULL fnKeySeq, + // 'DataSort' fnKey, + // 'GHR_Common_000045' fnTitle, + // 'table', + // 'left', + // 'ghr-data-sort' + #region 定义页面的操作按钮 string sql = @"SELECT fnKey, fnKeyValue, @@ -238,26 +255,13 @@ public partial class CommonServices : BaseServices>, ICommon NULL position, NULL icon UNION ALL - SELECT NULL fnKeySeq, - 'SetDefault' fnKey, - [dbo].[FLangKeyToValue]('GHR_Common_000000',{2},'恢复默认') fnTitle, - 'table' fnType, - 'left' position, - 'ghr-icon-setDefault' icon - UNION ALL SELECT NULL fnKeySeq, 'GridView' fnKey, 'GHR_Common_000067' fnTitle, 'table' fnType, 'right' position, 'ant-design:setting-outlined' icon - UNION ALL - SELECT NULL fnKeySeq, - 'DataSort' fnKey, - 'GHR_Common_000045' fnTitle, - 'table', - 'left', - 'ghr-data-sort') A + ) A WHERE fnKey NOT IN (SELECT b.field FROM Ghrs_UserPageSettingQueryColumn a, Ghrs_PageSettingQuery b diff --git a/Tiobon.Core/Tiobon.Core.xml b/Tiobon.Core/Tiobon.Core.xml index d73b5de9..95ca044c 100644 --- a/Tiobon.Core/Tiobon.Core.xml +++ b/Tiobon.Core/Tiobon.Core.xml @@ -528,6 +528,106 @@ 课程分类(Controller) + + + 根据条件查询数据 + + 条件 + + + + + 根据Id查询数据 + + 主键ID + + + + + 新增数据 + + + + + + + 批量新增数据 + + + + + + 更新数据 + + 主键ID + + + + + + 批量更新数据 + + + + + + 删除数据 + + 主键ID + + + + + 批量删除数据 + + 主键IDs + + + + + 服务层分页响应实体(泛型) + + + + + 状态码 + + + + + 操作是否成功 + + + + + 返回信息 + + + + + 当前页标 + + + + + 总页数 + + + + + 数据总数 + + + + + 每页大小 + + + + + 返回数据 + + Ghre_Course(Controller)