

namespace Tiobon.Core.Api.Controllers;

/// <summary>
/// Ghre_CourseClass(Controller)
/// </summary>
[Route("api/[controller]")]
[ApiController, GlobalActionFilter]
[Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghre)]
public class Ghre_CourseClassController : BaseController<IGhre_CourseClassServices, Ghre_CourseClass, Ghre_CourseClassDto, InsertGhre_CourseClassInput, EditGhre_CourseClassInput>
{
    public Ghre_CourseClassController(IGhre_CourseClassServices service) : base(service)
    {
    }
}
//using SqlSugar;
//using Mapper = AgileObjects.AgileMapper.Mapper;

//namespace Tiobon.Core.Api.Controllers;

///// <summary>
///// 课程分类(Controller)
///// </summary>
//[Route("api/[controller]")]
//[ApiController, GlobalActionFilter]
//[Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghre)]
//public class Ghre_CourseClassController : BaseApiController
//{
//    protected IGhre_CourseClassServices _service;

//    public Ghre_CourseClassController(IGhre_CourseClassServices service)
//    {
//        _service = service;
//    }

//    #region 基础接口

//    #region 查询
//    /// <summary>
//    /// 根据条件查询数据
//    /// </summary>
//    /// <param name="filter">条件</param>
//    /// <returns></returns>
//    [HttpGet]
//    public async Task<ServicePageResult1<Ghre_CourseClassDto>> QueryByFilter([FromFilter] QueryFilter filter)
//    {
//        //var data = await _service.QueryFilterPage();
//        RefAsync<int> totalCount = 0;
//        var query = _service.Db.Queryable<Ghre_CourseClass>();
//        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<Ghre_CourseClassDto>(filter.PageIndex, totalCount, filter.PageSize, Mapper.Map(list).ToANew<List<Ghre_CourseClassDto>>());

//    }

//    public static T ConvertTo<T>(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}.");
//        }
//    }

//    /// <summary>
//    ///  根据Id查询数据
//    /// </summary>
//    /// <param name="Id">主键ID</param>
//    /// <returns></returns>
//    [HttpGet("{Id}")]
//    public virtual async Task<ServiceResult<Ghre_CourseClassDto>> QueryById(long Id)
//    {
//        var entity1 = await _service.QueryById(Id);
//        var entity = ConvertTo<Ghre_CourseClassDto>(entity1);
//        if (entity is null)
//            return new ServiceResult<Ghre_CourseClassDto>() { Success = false, Status = 500, Message = "获取失败", Data = default };
//        else
//            return new ServiceResult<Ghre_CourseClassDto>() { Success = false, Message = "获取成功", Data = entity };

//    }
//    #endregion

//    #region 新增
//    /// <summary>
//    /// 新增数据
//    /// </summary>
//    /// <param name="insertModel"></param>
//    /// <returns></returns>
//    [HttpPost]
//    public virtual async Task<ServiceResult<string>> Insert([FromBody] InsertGhre_CourseClassInput insertModel)
//    {
//        var data = new ServiceResult<string>() { 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<string>() { Success = false, Status = 500, Message = "新增失败", Data = default };
//        return data;
//    }

//    /// <summary>
//    /// 批量新增数据
//    /// </summary>
//    /// <param name="insertModels"></param>
//    [HttpPost, Route("BulkInsert")]
//    public virtual async Task<ServiceResult<List<long>>> BulkInsert([FromBody] List<InsertGhre_CourseClassInput> insertModels)
//    {
//        var data = new ServiceResult<List<long>>() { 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<List<long>>() { Success = false, Status = 500, Message = "新增失败", Data = default };

//        return data;
//    }

//    #endregion

//    #region 更新
//    /// <summary>
//    /// 更新数据
//    /// </summary>
//    /// <param name="Id">主键ID</param>
//    /// <param name="editModel"></param>
//    /// <returns></returns>
//    [HttpPut("{Id}")]
//    public virtual async Task<ServiceResult> 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;
//    }
//    /// <summary>
//    ///  批量更新数据
//    /// </summary>
//    /// <param name="editModels"></param>
//    [HttpPut, Route("BulkUpdate")]
//    public virtual async Task<ServiceResult> BulkUpdate([FromBody] Dictionary<long, EditGhre_CourseClassInput> 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 删除
//    /// <summary>
//    /// 删除数据
//    /// </summary>
//    /// <param name="Id">主键ID</param>
//    /// <returns></returns>
//    [HttpDelete("{Id}")]
//    public virtual async Task<ServiceResult> 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;
//    }

//    /// <summary>
//    /// 批量删除数据
//    /// </summary>
//    /// <param name="Ids">主键IDs</param>
//    /// <returns></returns>
//    [HttpDelete, Route("BulkDelete")]
//    public virtual async Task<ServiceResult> 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
//}


///// <summary>
///// 服务层分页响应实体(泛型)
///// </summary>
//public class ServicePageResult1<T>
//{
//    /// <summary>
//    /// 状态码
//    /// </summary>
//    public int Status { get; set; } = 200;
//    /// <summary>
//    /// 操作是否成功
//    /// </summary>
//    public bool Success { get; set; } = false;
//    /// <summary>
//    /// 返回信息
//    /// </summary>
//    public string Message { get; set; } = null;
//    /// <summary>
//    /// 当前页标
//    /// </summary>
//    public int Page { get; set; } = 1;
//    /// <summary>
//    /// 总页数
//    /// </summary>
//    public int PageCount => (int)Math.Ceiling((decimal)TotalCount / PageSize);
//    /// <summary>
//    /// 数据总数
//    /// </summary>
//    public int TotalCount { get; set; } = 0;
//    /// <summary>
//    /// 每页大小
//    /// </summary>
//    public int PageSize { set; get; } = 20;
//    /// <summary>
//    /// 返回数据
//    /// </summary>
//    public List<T> Data { get; set; }

//    public ServicePageResult1() { }

//    public ServicePageResult1(int page, int totalCount, int pageSize, List<T> data)
//    {
//        Success = true;
//        this.Page = page;
//        this.TotalCount = totalCount;
//        PageSize = pageSize;
//        this.Data = data;
//    }
//}
