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;
using TsSFCDevice.Client.Biz.DeviceSvc;

namespace TsSFCDevice.Client.Biz.Impl
{
    public class HisRepository<TParams> where TParams : class, new()
    {
        private readonly Logger log;
        private static HisRepository<TParams> manager;

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


        public static HisRepository<TParams> Instance
        {
            get
            {
                if (manager == null)
                    manager = new HisRepository<TParams>();
                return manager;
            }
        }

        public HisRepository()
        {
            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<TParams> HistoryGet(IDictionary<string, string> vParams)
        {
            DataSet dsResults = new DataSet();
            try
            {
                byte[] btResults = null;
                var vRtn = Utility.SfcBizService.CurrentSvc.HistoryGet(HisType, GetParameters(), out btResults);

                if (vRtn.Code != 1)
                {
                    throw new Exception(vRtn.Message);
                }

                dsResults = CompressionHelper.ExactDataSet(btResults);
                if (dsResults == null || dsResults.Tables.Count == 0 || dsResults.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                return DTOHelper<TParams>.DataTableToList(dsResults.Tables[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<TParams> HistoryGet()
        {
            return HistoryGet(ApiParameters);
        }

    }
}
