using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.Permission;
using OpenAuth.App.Permission.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.BaseControllers
{
    /// <summary>
    /// 数据权限控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class BaseDataAuthController : ControllerBase
    {
        private readonly BaseDataAuthorizeApp _baseDataAuthorizeApp;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="dataAuthorizeIBLL">数据权限操作接口</param>
        public BaseDataAuthController(BaseDataAuthorizeApp baseDataAuthorizeApp)
        {
            _baseDataAuthorizeApp = baseDataAuthorizeApp;
        }

        #region 获取数据

        /// <summary>
        /// 获取数据权限对应关系数据列表
        /// </summary>
        /// <param name="objectId">用户或角色主键</param>
        /// <param name="type">1.普通权限 2.自定义表单权限</param>
        /// <returns></returns>
        [HttpGet("permission/data/page")]
        public async Task<Response<PageInfo<List<BaseDataAuth>>>> GetPageList([FromQuery] BaseDataAuthListReq req)
        {
            return await _baseDataAuthorizeApp.GetPageList(req);
        }

        /// <summary>
        /// 获取数据权限数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet("permission/data/{id}")]
        public async Task<Response<BaseDataAuth>> GetEntity(string id)
        {
            var data = await _baseDataAuthorizeApp.GetEntity(id);
            return new Response<BaseDataAuth>()
            {
                Result = data,
                Message = "success"
            };
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 新增数据权限
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        [HttpPost("permission/data")]
        public async Task<Response<string>> AddForm(BaseDataAuthReq entity)
        {
            // todo 保存的时候，序列化公式
            await _baseDataAuthorizeApp.SaveEntity(string.Empty, entity);
            return new Response<string>()
            {
                Result = "新增成功！",
                Message = "success"
            };
        }

        /// <summary>
        /// 更新数据权限
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        [HttpPost("permission/data/{id}")]
        public async Task<Response<string>> UpdateForm(string id, BaseDataAuthReq entity)
        {
            await _baseDataAuthorizeApp.SaveEntity(id, entity);
            return new Response<string>()
            {
                Result = "更新成功！",
                Message = "success"
            };
        }

        /// <summary>
        /// 删除数据权限
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost("permission/data/{id}")]
        public async Task<Response<string>> DeleteForm(string id)
        {
            await _baseDataAuthorizeApp.DeleteEntity(id);
            return new Response<string>()
            {
                Result = "删除成功！",
                Message = "success"
            };
        }


        [HttpGet]
        public async Task<string> GetWhereSql(string code)
        {
            return await _baseDataAuthorizeApp.GetWhereSql(code);
        }

        #endregion
    }
}