using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Models;
using NetTaste;
using OpenAuth.App;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.BaseApp.DmModuleManager;
using OpenAuth.App.BaseApp.DmModuleManager.Request;
using OpenAuth.App.BaseApp.DmModuleManager.Response;
using OpenAuth.App.Request;

namespace OpenAuth.WebApi.Controllers.BaseControllers
{
    /// <summary>
    /// 用户操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DmModuleController : ControllerBase
    {
        private readonly DmModuleApp _app;
        public DmModuleController(DmModuleApp app)
        {
            _app = app;
        }

        #region 数据查询
        /// <summary>
        /// 分页查询模板信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DmModule>>>> LoadModulePageInfo([FromQuery] PageReq request)
        {
            var result = new Response<PageInfo<List<DmModule>>>();
            try
            {
                result =await _app.LoadModulePageInfo(request);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 查询所有模板信息(不分页)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<DmModule>>> LoadAll()
        {
            var result = new Response<List<DmModule>>();
            try
            {
                result = await _app.LoadAll();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 根据模板id获取字段列表
        /// </summary>
        /// <param name="id">模板id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<DmModulecolumn>>> LoadCloumsById(string id)
        {
            var result = new Response<List<DmModulecolumn>>();
            try
            {
                result = await _app.LoadCloumsById(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 根据模板详情信息查询
        /// </summary>
        /// <param name="id">模板id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<DmDetailRes>> LoadModuleDetailInfo(string id)
        {
            var result = new Response<DmDetailRes>();
            try
            {
                result = await _app.LoadModuleDetailInfo(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        #endregion

        #region 增删改
        /// <summary>
        /// 添加或者修改数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddOrUpdate(AddorUpdateModuleReq obj)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.AddOrUpdate(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 根据选中id删除模板
        /// </summary>
        /// <param name="ids"></param>
        [HttpPost]
        public Response<bool> DeleteModule(string[] ids)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.DeleteModule(ids);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion
    }
}
