using DeviceRepairAndOptimization.Data.User;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace DeviceRepairAndOptimization.Data
{
    public class SettingMaintenance
    {
        private static SettingMaintenance manager;

        public static SettingMaintenance Instance
        {
            get
            {
                if (manager == null)
                    manager = new SettingMaintenance();
                return manager;
            }
        }

        /// <summary>
        /// 获取用户设置
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public UserSettingModel GetUserSettingModel(int UserId)
        {
            UserSettingModel entity = null;
            try
            {
                if (UserId == 0)
                    return entity;

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

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = "api/setting/GetUserSettingModel",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",//返回类型    可选项有默认值
                            Postdata = JsonConvert.SerializeObject(new { userid = UserId })
                        });

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

                        break;
                    case "sql":

                        entity = UserSettingAccess.Instance().GetUserSettingModel(UserId);

                        break;

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

            return entity;
        }

        /// <summary>
        /// 插入数据并返回自增长主键编号
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        public int SaveUserSettingModel(UserSettingModel us)
        {
            int Count = 0;
            try
            {
                if (us == null)
                    return Count;

                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        string pdata = JObject.FromObject(us).ToString();
                        APIResponseData result = ApiHelper.Instance.PostWebRequest("api/setting/SaveUserSettingModel", pdata, GlobalInfo.token);

                        if (result.Code == 1)
                        {
                            Count = int.Parse(result.Data + "");
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        break;
                    case "sql":

                        Count = UserSettingAccess.Instance().SaveUserSettingModel(us);

                        break;

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

            return Count;
        }

        /// <summary>
        /// 异步修改/新增
        /// </summary>
        /// <param name="us"></param>
        public void SaveUserSettingModelAsync(UserSettingModel us)
        {
            try
            {
                if (us == null)
                    return;

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

                        ApiHelper.Instance.SendMessageAsync(new CsharpHttpHelper.HttpItem
                        {
                            URL = "api/us/SaveUserSettingModel",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",//返回类型    可选项有默认值
                            Postdata = JsonConvert.SerializeObject(new { usersettingmodel = us }),
                            Header =
                            {
                                {"auth",GlobalInfo.token }
                            }
                        }, null);

                        break;
                    case "sql":

                        UserSettingAccess.Instance().SaveUserSettingModelAsync(us);

                        break;

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