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

namespace DeviceRepairAndOptimization.Data
{
    /// <summary>
    /// 用户信息数据交互对象
    /// </summary>
    public class UserInfoDataMaintenance
    {
        private static UserInfoDataMaintenance manager;
        public static UserInfoDataMaintenance Instance
        {
            get
            {
                if (manager == null)
                    manager = new UserInfoDataMaintenance();
                return manager;
            }
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public List<UserInfoModel> GetAll()
        {
            List<UserInfoModel> lst = null;

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

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

                        if (result.Code == 1)
                        {
                            lst = JsonConvert.DeserializeObject<List<UserInfoModel>>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

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

                        lst = UserInfoAccess.Instance().GetAll();

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

            return lst;
        }

        /// <summary>
        /// 通过用户名查询用户信息
        /// </summary>
        /// <param name="LoginCode"></param>
        /// <returns></returns>
        public UserInfoModel GetDataByLoginCode(string LoginCode)
        {
            UserInfoModel entity = null;
            try
            {
                if (string.IsNullOrWhiteSpace(LoginCode))
                    return entity;

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

                        #region api

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

                        if (result.Code == 1)
                        {
                            entity = JsonConvert.DeserializeObject<UserInfoModel>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion

                        break;
                    case "sql":

                        #region sql

                        entity = UserInfoAccess.Instance().GetDataByLoginCode(LoginCode);

                        break;
                    #endregion

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

            return entity;
        }


        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData GetDataByCodeAndPwd(UserInfoModel model)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "登录失败！" };
            UserInfoModel us = null;
            try
            {
                if (!model.IsValid())
                    return new APIResponseData { Code = -1, Message = "用户名或密码不允许为空！" };

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

                        #region api

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

                        us = JsonConvert.DeserializeObject<UserInfoModel>(result.Data + "");

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        UserInfoModel m = UserInfoAccess.Instance().GetDataByCodeAndPwd(model.LoginCode, model.PassWord);

                        #endregion
                        break;
                    default:
                        break;
                }

                if (us != null)
                {
                    if (!us.Status)
                    {
                        result.Code = -1;
                        result.Message = $"用户被锁定,无法登录！";
                        return result;
                    }
                    //GlobalInfo.CurrentUser = us;
                }
                if (!string.IsNullOrEmpty(result.Token))
                    GlobalInfo.token = result.Token;
                return new APIResponseData { Code = 1 };
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 验证账户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData ValidePassWord(UserInfoModel model)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "账户或密码不正确！" };
            try
            {
                if (!model.IsValid())
                    return new APIResponseData { Code = -1, Message = "用户名或密码不允许为空！" };

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

                        #region api

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

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = UserInfoAccess.Instance().ValidePassWord(model);

                        #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 UpdateUserStatus(UserInfoModel model)
        {
            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.UpdateUserStatus,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(model.ToArrayAndCompress())
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = UserInfoAccess.Instance().UpdateUserStatus(model);

                        #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 UpdateUserPassword(UserInfoModel model)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                if (!model.IsValid())
                    return new APIResponseData { Code = -1, Message = "用户名或密码不允许为空！" };

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

                        #region api

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

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = UserInfoAccess.Instance().UpdateUserPassword(model);

                        #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 InsertOrEdit(UserInfoModel model)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                //if (!model.IsValid())
                //    return new APIResponseData { Code = -1, Message = "用户名或密码不允许为空！" };

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

                        #region api

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

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        result = UserInfoAccess.Instance().InsertOrEdit(model);

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

        /// <summary>
        /// 根据角色编号查询用户
        /// </summary>
        /// <param name="LoginCode"></param>
        /// <returns></returns>
        public APIResponseData GetUserByRoleID(int RoleID)
        {
            APIResponseData result = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

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

                        if (result.Code == 1)
                        {
                            result.Data = JsonConvert.DeserializeObject<List<UserInfoModel>>(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion

                        break;
                    case "sql":

                        #region sql

                        result = UserInfoAccess.Instance().GetUserByRoleID(RoleID);

                        break;
                    #endregion

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

            return result;
        }
    }
}