using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceRepairAndOptimization.Biz
{
    public class RoleManger
    {
        private static RoleManger manager;
        public static RoleManger Instance
        {
            get
            {
                if (manager == null)
                    manager = new RoleManger();
                return manager;
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAllRole()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetAllRoles,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = RoleAccess.Instance.GetAllRole();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取全部权限数据
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAuthList()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetAuthList,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = RoleAccess.Instance.GetAuthList();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取角色下全部用户
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetRoleUsers(int RoleID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetRoleUsers}?RoleID={RoleID}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = RoleAccess.Instance.GetRoleUsers(RoleID);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 清空角色下的用户
        /// </summary>
        /// <param name="RoleAutoID"></param>
        /// <returns></returns>
        public APIResponseData ClearRoleUsers(int RoleAutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.ClearRoleUsers}?RoleAutoID={RoleAutoID}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    default:
                        apiResponseData = RoleAccess.Instance.ClearRoleUsers(RoleAutoID, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 清空角色下的权限
        /// </summary>
        /// <param name="RoleAutoID"></param>
        /// <returns></returns>
        public APIResponseData ClearRoleAuths(int RoleAutoID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.ClearRoleAuths}?RoleAutoID={RoleAutoID}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    default:
                        apiResponseData = RoleAccess.Instance.ClearRoleAuths(RoleAutoID, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 判断当前角色编码是否存在
        /// </summary>
        /// <param name="RoleCode"></param>
        /// <returns></returns>
        public APIResponseData CheckRoleCodeExists(string RoleCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.CheckRoleCodeExists}?RoleCode={RoleCode}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    default:
                        apiResponseData = RoleAccess.Instance.CheckRoleCodeExists(RoleCode);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 根据角色ID,权限状态 获取权限列表
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetRoleAuths(int RoleID, bool Status)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetRoleAuths}?RoleID={RoleID}&Status={Status}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = RoleAccess.Instance.GetRoleAuths(RoleID, Status);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData Update(RoleModel Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                Item.ModifyBy = GlobalInfo.CurrentUser.AutoID;

                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.EditRole,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Item)
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = RoleAccess.Instance.Update(Item, GlobalInfo.OperationInfo);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 根据主键编号是插入/更新 角色权限数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public APIResponseData InsertOrEdit(RoleEditSubmitModel entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.InsertOrEditRoleAndAuth,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(entity)
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = RoleAccess.Instance.InsertOrEdit(entity, GlobalInfo.OperationInfo);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
