using DeviceRepair.Api.Common;
using DeviceRepair.Api.CustomAttribute;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using System;
using System.Web.Http;

namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Role")]
    public class RoleController : CFController
    {
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        [HttpAuthorize]
        public APIResponseData GetAllRole()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };

            try
            {
                apiResponseData = RoleAccess.Instance.GetAllRole();
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取角色下的用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRoleUsers")]
        [HttpAuthorize]
        public APIResponseData GetRoleUsers(int RoleID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };

            try
            {
                apiResponseData = RoleAccess.Instance.GetRoleUsers(RoleID);
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 根据角色ID,权限状态 获取权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetRoleAuths")]
        [HttpAuthorize]
        public APIResponseData GetRoleAuths(int RoleID, bool Status)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };
            try
            {
                apiResponseData = RoleAccess.Instance.GetRoleAuths(RoleID, Status);
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("EditRole")]
        [HttpAuthorize]
        public APIResponseData EditRole(RoleModel Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            if (Item == null)
                return apiResponseData;
            try
            {
                if (string.IsNullOrWhiteSpace(Item.RoleCode))
                {
                    apiResponseData.Message = "角色编码不能为空！";
                    return apiResponseData;
                }

                if (string.IsNullOrWhiteSpace(Item.RoleName))
                {
                    apiResponseData.Message = "角色名称不能为空！";
                    return apiResponseData;
                }

                base.GetParams();
                apiResponseData = RoleAccess.Instance.Update(Item, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取全部权限数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAuthList")]
        [HttpAuthorize]
        public APIResponseData GetAuthList()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };

            try
            {
                apiResponseData = RoleAccess.Instance.GetAuthList();
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取全部权限数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("CheckRoleCodeExists")]
        [HttpAuthorize]
        public APIResponseData CheckRoleCodeExists(string RoleCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };

            try
            {
                apiResponseData = RoleAccess.Instance.CheckRoleCodeExists(RoleCode);
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 根据主键编号是插入/更新 角色权限数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertOrEditRoleAndAuth")]
        [HttpAuthorize]
        public APIResponseData InsertOrEditRoleAndAuth(RoleEditSubmitModel entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                base.GetParams();
                apiResponseData = RoleAccess.Instance.InsertOrEdit(entity, base.OperationInfo);
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 清空角色下的用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ClearRoleUsers")]
        [HttpAuthorize]
        public APIResponseData ClearRoleUsers(int RoleAutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };
            try
            {
                base.GetParams();
                apiResponseData = RoleAccess.Instance.ClearRoleUsers(RoleAutoID, base.OperationInfo);
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 清空角色下的权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ClearRoleAuths")]
        [HttpAuthorize]
        public APIResponseData ClearRoleAuths(int RoleAutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "数据查询失败！" };
            try
            {
                base.GetParams();
                apiResponseData = RoleAccess.Instance.ClearRoleAuths(RoleAutoID, base.OperationInfo);
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }
    }
}
