using DeviceRepair.Models;
using DeviceRepair.Models.Plan;
using DeviceRepair.Utils;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using TsSFCDevice.Client.Biz.Base.Service;
using TsSFCDevice.Client.Biz.Base.Utils;

namespace TsSFCDevice.Client.Biz.Impl
{
    public class EquipmentJumpPlanCheckRepository
    {
        private readonly Logger log;
        private static EquipmentJumpPlanCheckRepository 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 EquipmentJumpPlanCheckRepository Instance
        {
            get
            {
                if (manager == null)
                    manager = new EquipmentJumpPlanCheckRepository();
                return manager;
            }
        }

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

        #region 公共

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

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

        #endregion


        public IList<EquipmentJumpPlanCheckInfo> GetDatas(string EquipmentID = "", DateTime? CheckDate = null)
        {
            try
            {
                var Data = new List<EquipmentJumpPlanCheckInfo>();

                byte[] btResults = null;
                ApiParameters?.Clear();

                if (!EquipmentID.IsNull())
                    ApiParameters.Add("EquipmentID", EquipmentID);
                if (CheckDate.HasValue)
                    ApiParameters.Add("CheckDate", CheckDate.Value.ToString("yyyy-MM-dd"));

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

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

        public APIResponseData DataNews(string EquipmentID, DateTime CheckDate)
        {
            try
            {
                ApiParameters?.Clear();

                ApiParameters.Add("EquipmentID", EquipmentID);
                ApiParameters.Add("CheckDate", CheckDate.ToString("yyyy-MM-dd"));

                var Rtn = Utility.SfcBizService.CurrentSvc.News_JumpCheckData(GetParameters());
                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;
            }
        }

        public APIResponseData DataRemove(int AutoID)
        {
            try
            {
                ApiParameters?.Clear();
                ApiParameters.Add("AutoID", AutoID + "");

                var Rtn = Utility.SfcBizService.CurrentSvc.Remove_JumpCheckData(GetParameters());
                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;
            }
        }
    }
}
