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

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

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

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

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

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

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

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

                        lst = Drives.DrivesAccess.Instance().GetAll();

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

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="FilterValue"></param>
        /// <returns></returns>
        public DriveInfomationModel GetDataByAutoID(int AutoID)
        {
            DriveInfomationModel t = null;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = "api/drive/GetDataByAutoID",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = $"{{\"filtervalue\":{AutoID}}}"
                        });

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

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

                        t = Drives.DrivesAccess.Instance().GetDataByAutoID(AutoID);

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

        /// <summary>
        /// 设定设备的点检表
        /// </summary>
        /// <param name="MaintenanceFormVersionId">点检表编号</param>
        /// <param name="DriveIds">设备编号数组</param>
        /// <returns></returns>
        public bool SetFormVersion(int MaintenanceFormVersionId, int[] DriveIds)
        {
            bool isSuccess = false;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        string postData = JsonConvert.SerializeObject(new
                        {
                            maintenanceformversionid = MaintenanceFormVersionId,
                            driveids = DriveIds
                        });
                        APIResponseData result = ApiHelper.Instance.PostWebRequest(ServiceRoute.BindFormVersion, postData, GlobalInfo.token);

                        if (result.Code == 1)
                        {
                            bool.TryParse(result.Data + "", out isSuccess);
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        break;
                    case "sql":

                        isSuccess = Drives.DrivesAccess.Instance().SetFormVersion(MaintenanceFormVersionId, DriveIds);

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

            return isSuccess;
        }

        /// <summary>
        /// 通过主键编号查询设备信息
        /// </summary>
        /// <param name="EquipmentID"></param>
        /// <returns></returns>
        public DriveInfomationModel GetModelByEquipmentID(int KeyId)
        {
            DriveInfomationModel entity = null;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetCurrentMonthPlans,
                            Method = "post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { equipmentid = KeyId })
                        });

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

                        #endregion

                        break;
                    case "sql":

                        #region sql

                        entity = Drives.DrivesAccess.Instance().GetModelByEquipmentID(KeyId);

                        break;
                    #endregion

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

            return entity;
        }

        /// <summary>
        /// 修改设备信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool UpdateDriveInformation(Dictionary<string, object> data)
        {
            byte[] byteArray = data.toByteArray();
            bool isSuccess = false;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.UpdateDeviceInfo,
                            Method = "post",
                            ContentType = "application/json",
                            Postdata = JsonConvert.SerializeObject(byteArray)
                        });

                        if (result.Code == 1)
                        {
                            isSuccess = true;
                        }
                        else
                        {
                            throw new Exception(result.Message);
                        }

                        #endregion

                        break;
                    case "sql":

                        #region sql

                        isSuccess = Drives.DrivesAccess.Instance().UpdateDriveInformation(data);

                        break;
                    #endregion

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

        /// <summary>
        /// 通过设备编号查询设备信息
        /// </summary>
        /// <param name="EquipmentID"></param>
        /// <returns></returns>
        public DriveInfomationModel GetModelByEquipmentID(string EquipmentID)
        {
            DriveInfomationModel entity = null;
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        APIResponseData result = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetDeviceByEquipmentID,
                            Method = "post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { equipmentid = EquipmentID })
                        });

                        if (result.Code == 1)
                        {
                            entity = JsonConvert.DeserializeObject<DriveInfomationModel>(result.Data + "");
                        }
                        else
                        {
                            return null;
                        }

                        break;
                    case "sql":

                        entity = Drives.DrivesAccess.Instance().GetModelByEquipmentID(EquipmentID);

                        break;
                    default:
                        break;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return entity;
        }
    }
}