using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.SysDatabaseLink;
using OpenAuth.App.SysDatabaseLink.Response;
using OpenAuth.Repository.Core;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 数据库连接管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysDatabaseLinkController:ControllerBase
    {
        private readonly SysDatabaseLinkApp _app;
        public SysDatabaseLinkController(SysDatabaseLinkApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 获取数据库树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<TreeModel>>> LoadDataBaseLinkTree()
        {
            return await _app.LoadDataBaseLinkTree();
        }

        /// <summary>
        /// 分页获取列表数据
        /// </summary>
        /// <param name="keyword">筛选条件</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<SysDatabaseLink>>>> LoadDataBaseInfo(string keyword, int page = 1, int limit = 15)
        {
            return await _app.LoadDataBaseInfo(keyword,page,limit);
        }

        /// <summary>
        /// 根据主键获取实体数据(编辑时使用)
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<SysDatabaseLink>> GetDataBaseForm(string id)
        {
            return await _app.GetDataBaseForm(id);
        }

        /// <summary>
        ///新增数据库连接
        /// </summary>
        /// <param name="baseLink">数据库实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddBaseLinkForm(Repository.Domain.SysDatabaseLink baseLink)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.SaveBaseLinkEntity(String.Empty, baseLink);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        ///更新数据库连接
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="baseLink">数据库实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateBaseLink(string keyValue, Repository.Domain.SysDatabaseLink baseLink)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.SaveBaseLinkEntity(keyValue, baseLink);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除数据库连接
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteBaseLink(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteBaseLink(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 测试连接串是否正确
        /// </summary>
        /// <param name="connection">连接串</param>
        /// <param name="dbType">数据库类型</param>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<bool>> TestConnection(string connection, string dbType, string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.TestDataBaseLink(connection, dbType, id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
