using DeviceRepair.Models;
using DeviceRepair.Models.Device;
using DeviceRepair.Models.SFC;
using DeviceRepair.Utils;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using TsSFCDevice.Client.Biz.Base.Service;
using TsSFCDevice.Client.Biz.Base.Utils;
namespace TsSFCDevice.Client.Biz.Impl
{
    public class DevRepository
    {
        private readonly Logger log;
        private static DevRepository manager;

        private IDictionary<string, string> m_ApiParameters;
        /// <summary>
        /// API输入参数
        /// </summary>
        public IDictionary<string, string> ApiParameters
        {
            get
            {
                return m_ApiParameters;
            }
            set
            {
                m_ApiParameters = value;
            }
        }

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

        public DevRepository()
        {
            log = LogManager.GetCurrentClassLogger();
            m_ApiParameters = new Dictionary<string, string>();
        }

        internal string GetParameters(string cOperator = "", bool bFlag = true)
        {

            return ParametersObject.GetInstance(cOperator).GetJsonSerialized(m_ApiParameters);
        }

        public IList<DeviceInformationInfo> GetDatas(List<string> Auths)
        {
            try
            {
                IList<DeviceInformationInfo> Data = new List<DeviceInformationInfo>();
                if (Auths == null || Auths.Count == 0)
                    return Data;

                byte[] btResults = null;
                ApiParameters?.Clear();
                ApiParameters.Add("Auths", string.Join(",", Auths));

                var Rtn = Utility.SfcBizService.CurrentSvc.GetDatas(DeviceSvc.SysModelType.DEVICE, GetParameters(), out btResults);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                DataSet dsResults = CompressionHelper.ExactDataSet(btResults);

                return DTOHelper<DeviceInformationInfo>.DataTableToList(dsResults.Tables[0]);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 获取设备树形结构数据
        /// </summary>
        /// <param name="Auths"></param>
        /// <returns></returns>
        public IList<DeviceInformationInfoTree> GetTreeDatas(List<string> Auths,string FilterValue = "")
        {
            try
            {
                IList<DeviceInformationInfoTree> Data = new List<DeviceInformationInfoTree>();
                if (Auths == null || Auths.Count == 0)
                    return Data;

                byte[] btResults = null;
                ApiParameters?.Clear();
                ApiParameters.Add("Auths", string.Join(",", Auths));
                if (!FilterValue.IsNull())
                    ApiParameters.Add("FilterValue", FilterValue);

                var Rtn = Utility.SfcBizService.CurrentSvc.Get_DEVICE_TreeDatas(GetParameters(), out btResults);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                DataSet dsResults = CompressionHelper.ExactDataSet(btResults);

                return DTOHelper<DeviceInformationInfoTree>.DataTableToList(dsResults.Tables[0]);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 获取设备分组数据
        /// </summary>
        /// <param name="Auths"></param>
        /// <returns></returns>
        public IList<DeviceRouteInfo> Get_DEVICE_Route(List<string> Auths)
        {
            try
            {
                IList<DeviceRouteInfo> Data = new List<DeviceRouteInfo>();
                if (Auths == null || Auths.Count == 0)
                    return Data;

                byte[] btResults = null;
                ApiParameters?.Clear();
                ApiParameters.Add("Auths", string.Join(",", Auths));

                var Rtn = Utility.SfcBizService.CurrentSvc.Get_DEVICE_Route(GetParameters(), out btResults);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                DataSet dsResults = CompressionHelper.ExactDataSet(btResults);
                return DTOHelper<DeviceRouteInfo>.DataTableToList(dsResults.Tables[0]);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 判断设备编号是否存在
        /// </summary>
        /// <param name="EquipmentID"></param>
        /// <returns></returns>
        public bool EquipmentID_EXISTS(string EquipmentID)
        {
            try
            {
                ApiParameters?.Clear();
                bool EXISTS = true;

                var Rtn = Utility.SfcBizService.CurrentSvc.Get_DEVICE_EXISTS(GetParameters(), EquipmentID, out EXISTS); ;
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }

                return EXISTS;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 设备新增
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public APIResponseData Insert(DeviceInformationInfo Data, out DeviceInformationInfo Item)
        {
            Item = null;
            try
            {
                ApiParameters?.Clear();

                byte[] btResults = null;
                var Rtn = Utility.SfcBizService.CurrentSvc.Insert_DEVICE_Data(GetParameters(), new List<DeviceInformationInfo> { Data }.ToDataTable(), out btResults);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }

                DataSet dsResults = CompressionHelper.ExactDataSet(btResults);
                IList<DeviceInformationInfo> ResultLst = DTOHelper<DeviceInformationInfo>.DataTableToList(dsResults.Tables[0]);
                if (ResultLst == null)
                {
                    throw new Exception("实例化当前导出数据时发生异常错误！");
                }
                Item = ResultLst.FirstOrDefault();
                return new APIResponseData { Code = 1, Message = Rtn.Message };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 设备信息修改
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="OperationTime"></param>
        /// <returns></returns>
        public APIResponseData Update(DeviceInformationInfo Data, out DateTime OperationTime)
        {
            OperationTime = DateTime.MinValue;
            try
            {
                ApiParameters?.Clear();

                var Rtn = Utility.SfcBizService.CurrentSvc.Updata_DEVICE_Update(GetParameters(), new List<DeviceInformationInfo> { Data }.ToDataTable(), out OperationTime);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                return new APIResponseData { Code = 1, Message = Rtn.Message };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 修改设备状态
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="OperationTime"></param>
        /// <returns></returns>
        public APIResponseData ChangeStatus(int AutoID, out DateTime OperationTime)
        {
            OperationTime = DateTime.MinValue;
            try
            {
                ApiParameters?.Clear();

                var Rtn = Utility.SfcBizService.CurrentSvc.Update_DEVICE_Status(GetParameters(), AutoID, out OperationTime);
                if (Rtn.Code != 1)
                {
                    throw new Exception(Rtn.Message);
                }
                return new APIResponseData { Code = 1, Message = Rtn.Message };
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                throw ex;
            }
        }
    }
}
