using CsharpHttpHelper;
using DeviceRepairAndOptimization.Data.Auth;
using DeviceRepairAndOptimization.Data.Role;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

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

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAll()
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败" };

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.GetRoleDatas,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });

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

                        result = RoleAccess.Instance.GetAll();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public APIResponseData Add(RolesModel entity)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                entity.CreateBy = GlobalInfo.CurrentUser.AutoID;

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

                        #region api

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

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = RoleAccess.Instance.Add(entity);

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

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

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

                        #region api

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

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = RoleAccess.Instance.Update(entity);

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

        /// <summary>
        /// 获取所有权限列表
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAuthList()
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败" };

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

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

                        if (result.Code == 1)
                        {
                            string json = JsonConvert.SerializeObject(result.Data);
                            result.Data = JsonConvert.DeserializeObject<List<AuthsModel>>(json);
                        }

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

                        result = AuthAccess.Instance.GetList();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        /// <summary>
        /// 根据角色ID,权限状态 获取权限列表
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetRoleAuths(int RoleID, bool Status)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败" };

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.GetRoleAuths,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { RoleID = RoleID, Status = Status })
                        });

                        if (result.Code == 1)
                        {
                            string json = JsonConvert.SerializeObject(result.Data);
                            result.Data = JsonConvert.DeserializeObject<List<AuthsModel>>(json);
                        }

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

                        result = AuthAccess.Instance.GetRoleAuths(RoleID, Status);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        /// <summary>
        /// 获取角色下的用户
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public APIResponseData GetRoleUsers(int RoleID)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败" };

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.GetRoleUsers,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { RoleID = RoleID })
                        });

                        if (result.Code == 1)
                        {
                            string json = JsonConvert.SerializeObject(result.Data);
                            result.Data = JsonConvert.DeserializeObject<List<UserInfoModel>>(json);
                        }

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

                        result = AuthAccess.Instance.GetRoleUsers(RoleID);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

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

                        #region api

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

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = RoleAccess.Instance.InsertOrEdit(entity);

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

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

                        #region api

                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.ClearRoleUsers,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { RoleID = RoleAutoID, OperationUser = GlobalInfo.CurrentUser.AutoID })
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = RoleAccess.Instance.ClearRoleUsers(RoleAutoID, GlobalInfo.CurrentUser.AutoID);

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

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

                        #region api

                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.ClearRoleAuths,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { RoleID = RoleAutoID })
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = RoleAccess.Instance.ClearRoleAuths(RoleAutoID);

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

        public APIResponseData CheckRoleCodeExists(string RoleCode)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败" };

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.CheckRoleCodeExists,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { RoleCode = RoleCode })
                        });

                        //if (result.Code == 1)
                        //{
                        //    string json = JsonConvert.SerializeObject(result.Data);
                        //    result.Data = JsonConvert.DeserializeObject<List<RoleAuthsModel>>(json);
                        //}

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

                        result = AuthAccess.Instance.CheckRoleCodeExists(RoleCode);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }
    }
}
