using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.Repository.Core;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi.Model.CustomAttribute;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 机构操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[ApiExplorerSettings(GroupName = "组织机构_Orgs")]
    public class OrgsController : ControllerBase
    {
        private readonly OrgManagerApp _app;
        public OrgsController(OrgManagerApp app)
        {
            _app = app;
        }

        #region 查询

        #region 分页
        /// <summary>
        /// 查询部门列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        [AllData("Orgs", "LoadAllPage")]
        public async Task<Response<PageInfo<List<SysOrg>>>> LoadPage([FromQuery] QueryOrgListReq req)
        {
            return await _app.LoadPage(req);
        }
        #endregion

        #region 详情
        /// <summary>
        /// 获取机构详情
        /// </summary>
        [HttpGet]
        public Response<SysOrg> Get(string id)
        {
            var result = new Response<SysOrg>();
            try
            {
                result.Result = _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 部门树       
        /// <summary>
        /// 根据name查询组织树形结构
        /// name为空查全部
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        [AllData("Orgs", "OrgsAllTree")]
        public async Task<Response<List<TreeItemLong>>> OrgsTree(string name)
        {
            return await _app.OrgsTree(name);
        }
        #endregion

        #region 部门树【包含职级】
        /// <summary>
        /// 部门树【包含职级】
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<TreeItemLong>>> OrgPositonTree()
        {
            var result = new Response<List<TreeItemLong>>();
            try
            {
                result = await _app.OrgPositonTree();

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 子节点
        [HttpGet]
        public async Task<Response<List<SysOrg>>> LoadChildren(long? parentId)
        {
            var response = new Response<List<SysOrg>>();
            try
            {
                response.Result = await _app.LoadChildren(parentId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }
        #endregion

        #region 父节点
        /// <summary>
        /// 查询节点所有父级
        /// </summary>
        /// <param name="childId">节点id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<SysOrg>>> LoadParents(long childId)
        {
            var response = new Response<List<SysOrg>>();
            try
            {
                response.Result = await _app.LoadParents(childId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }
        #endregion

        #region 用户关联的部门和职级
        [HttpGet]
        public async Task<Response<List<SysUserOrg>>> UserOrgs(long userId)
        {
            var response = new Response<List<SysUserOrg>>();
            try
            {
                response.Result = await _app.UserOrgs(userId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }
        #endregion


        #endregion

        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> Add(OrgForm org)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.Add(org);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> Update(SysOrg obj)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.Update(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        #endregion

        #region 删除选中的部门及所有的子部门
        /// <summary>
        /// 删除选中的部门及所有的子部门
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> Delete([FromBody] string[] ids)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.DelOrgCascade(ids);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 为部门分配职级组
        /// <summary>
        /// 为部门分配职级组
        /// </summary>
        /// <param name="assignOrgPos"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> OrgPosGroup(AssignOrgPosGroup assignOrgPos)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.OrgPosGroup(assignOrgPos);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 未启用/后台使用

        #region 部门树【全部数据】
        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = true)]
        public async Task<Response<List<TreeItemLong>>> OrgsAllTree(string name)
        {
            return await _app.OrgsAllTree(name);
        }
        #endregion

        #region 分页
        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = true)]
        public async Task<Response<PageInfo<List<SysOrg>>>> LoadAllPage([FromQuery] QueryOrgListReq req)
        {
            return await _app.LoadAllPage(req);
        }
        #endregion

        #endregion
    }
}