using System;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.WebApi.Model.CustomAttribute;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 用户操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[ApiExplorerSettings(GroupName = "用户管理_Users")]
    public class UsersController : ControllerBase
    {
        private readonly UserManagerApp _app;
        public UsersController(UserManagerApp app)
        {
            _app = app;
        }

        #region 查询

        #region 实体
        /// <summary>
        /// 实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<UserView> Get(long id)
        {
            var result = new Response<UserView>();
            try
            {
                result.Result = _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 用户列表
        /// <summary>
        /// 用户列表
        /// 根据选中部门及其下子部门过滤Account和Name包含key值的用户列表
        /// orgId 为空，查全部 User
        /// </summary>
        [HttpGet]
        [AllData("Users", "LoadAll")]
        public async Task<Response<PageInfo<List<dynamic>>>> Load([FromQuery] QueryUserListReq request)
        {
            return await _app.LoadUsers(request);
        }

        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = true)]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadAll([FromQuery] QueryUserListReq request)
        {
            return await _app.LoadAll(request);
        }
        #endregion

        #region 加载指定角色的用户
        /// <summary>
        /// 加载指定角色的用户
        /// </summary>
        [HttpGet]
        public async Task<Response<PageInfo<List<UserView>>>> LoadByRole([FromQuery] QueryUserListByRoleReq request)
        {
            return await _app.LoadByRole(request);
        }
        #endregion

        #region 加载指定部门的用户
        /// <summary>
        /// 加载指定部门的用户
        /// 不包含下级部门的用户
        /// </summary>
        [HttpGet]
        public async Task<TableData> LoadByOrg([FromQuery] QueryUserListByOrgReq request)
        {
            return await _app.LoadByOrg(request);
        }
        #endregion

        #endregion

        #region 修改用户资料
        /// <summary>
        /// 修改用户资料
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response ChangeProfile(ChangeProfileReq request)
        {
            var result = new Response();

            try
            {
                _app.ChangeProfile(request);
                result.Message = "修改成功，重新登录生效";
            }
            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 ChangePassword(ChangePasswordReq request)
        {
            var result = new Response();
            try
            {
                _app.ChangePassword(request);
            }
            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> AddOrUpdate(UpdateUserReq 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;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> Delete([FromBody] long[] ids)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.Delete(ids);

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

            return result;
        }
        #endregion

        #region
        /// <summary>
        /// 人员信息导入
        /// </summary>
        /// <param name="formFiles"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> UserInfoUpload(IFormFileCollection formFiles,long roleId)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return _app.UserInfoUpload(formFiles,roleId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
        #region 为用户分配角色
        /// <summary>
        /// 为用户分配角色
        /// </summary>
        /// <param name="userRoles"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UserRoles(AssignUserRoles userRoles)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.UserRoles(userRoles);

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

        #region 为用户分配部门
        /// <summary>
        /// 为用户分配部门
        /// </summary>
        /// <param name="userRoles"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UserOrgs(AssignUserOrgs userOrgs)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.UserOrgs(userOrgs);

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

        #region 未启用/后台使用
        /// <summary>
        /// 更新数据库实体类
        /// </summary>
        [HttpPost]
        [AllowAnonymous]
        //[ApiExplorerSettings(IgnoreApi = true)]
        public Response AddEntity()
        {
            var result = new Response();
            try
            {
                result.Message = _app.AddEntity();

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

            return result;
        }
        #endregion

        #region user数据表导入
        /// <summary>
        /// 上传用户信息
        /// </summary>
        /// <param name="formFiles">文件</param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> ImportUserInfo(IFormFileCollection formFiles)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response =  _app.ImportUserInfo(formFiles);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

    }
}