using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.CodeTable;
using OpenAuth.App.CodeTable.Request;
using OpenAuth.App.CodeTable.Response;
using OpenAuth.App.FormScheme;
using OpenAuth.App.FormScheme.Request;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>  
    /// 日 期： 2024-03-02
    /// 描 述： 数据对象模型管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CodeTableController : ControllerBase
    {
        private readonly CodeTableApp _app;
        private readonly CodeColumnsApp _ccapp;
        public CodeTableController(CodeTableApp app, CodeColumnsApp ccapp)
        {
            _app = app;
            _ccapp = ccapp;
        }
        /// <summary>
        /// 获取CodeTable的分页数据
        /// </summary>
        /// <param name="keyWord">查询关键字</param>
        /// <param name="dbCode">数据库编码</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<DbCodeTable>>>> LoadCodeTablePage(string keyWord, string dbCode, int page, int limit)
        {
            return await _app.LoadCodeTablePage(keyWord, dbCode, page, limit);
        }
        /// <summary>
        /// 获取字典分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<DbCodeTable>>> GetEntitys(string dbCode, string tableNames)
        {
            var result = new Response<List<DbCodeTable>>();
            try
            {
                result.Result = await _app.GetEntitys(dbCode, tableNames.Split(',').ToList());
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 获取表的对象集合数据
        /// </summary>
        /// <param name="dbCode">数据库编码</param>
        /// <param name="tableNames">数据表名集合</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<CodeTableColumnsResp>>> GetForms(string dbCode, string tableNames)
        {
            var result = new Response<List<CodeTableColumnsResp>>();
            try
            {
                var list = await _app.GetEntitys(dbCode, tableNames.Split(',').ToList());
                List<CodeTableColumnsResp> res = new List<CodeTableColumnsResp>();
                foreach (var item in list)
                {
                    var data = new CodeTableColumnsResp();
                    data.db_codetable = item;
                    data.db_codecolumnsList = await _ccapp.GetList(new DbCodeColumns { CodeTableId = item.Id });
                    res.Add(data);
                }
                result.Result = res;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 导入表数据
        /// </summary>
        /// <param name="dbCode">数据库编码</param>
        /// <param name="req">表数据</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> ImportTable(string dbCode, CodeTableReq req)
        {
            var result = new Response<bool>();
            try
            {
                await _app.ImportTable(dbCode,req.TableList);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 获取数据库表及视图字段
        /// </summary>
        /// <param name="dbCode"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Response<List<dynamic>> GetTableAndViewColumnList(string dbCode, string tableName)
        {
            var result = new Response<List<dynamic>>();
            try
            {
                result.Result = _app.GetTableAndViewColumnList(dbCode, tableName);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
