using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediII.Adapter.Scan.Interface;
using NHapi.Base.Model;
using System.Collections.Concurrent;

using NHapi.Base.Parser;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Threading.Tasks;
using System.Data;
using MediII.Adapter.ReceiveToKCModel;
using IL.Common;

namespace MediII.Adapter.Scanner
{
    public abstract class DBScaner : BaseScan
    {
        PipeParser parser = new PipeParser();

        DatabaseProviderFactory factory = new DatabaseProviderFactory();
        //数据库连接
        protected Database ctx;

        public DBScaner()
        {
            ctx = factory.CreateDefault();
        }

        protected override void OnStart()
        {
            string typeName = this.GetType().Name.Trim();
            MediII.Common.LogHelper.LogInfo(string.Format("Scaner:{0} Started...", typeName), Common.LogCatagories.MQAccess);

            var entity = GetSaoMiao(typeName);
            if (entity == null)
            {
                LastTime = GetNow().AddDays(-1);
            }
            else
            {
                if (entity.IsDel == 1)
                {
                    this.Stop();
                    return;
                }

                MediII.Common.LogHelper.LogInfo(string.Format("Scaner:{0} Started...", typeName), Common.LogCatagories.MQAccess);
                int minus = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["AddMinues"]);
                if (AddMinusDay)
                    LastTime = entity.ExecuteTime.AddDays(-1);
                else
                    LastTime = entity.ExecuteTime;

                //设置扫描间隔
                var jianGe = entity.IntervalTime;
                if (jianGe != null)
                {
                    this.Interval = (int)jianGe;
                    MediII.Common.LogHelper.LogInfo(string.Format("Scaner:{0} jianGe:{1}", typeName, this.Interval), Common.LogCatagories.MQAccess);
                }
            }
        }

        private HL7_MessageSetEntity GetSaoMiao(string typeName)
        {
            var sql = string.Format("select * from HL7_MessageSet where MessageType=2 and  MessageId = '{0}'", typeName);
            var entity = SqlDataAccess.ExecuteSqlStringAccessor<HL7_MessageSetEntity>(ctx, sql).FirstOrDefault();
            return entity;
        }

        protected override void OnStop()
        {
            SetNewExecTime(LastTime);
        }

        protected override void OnElapsed(DateTime elapsedTime)
        {
            SetNewExecTime(elapsedTime);
        }

        /// <summary>
        /// 最后执行时间
        /// </summary>
        /// <param name="elapsedTime"></param>
        private void SetNewExecTime(DateTime elapsedTime)
        {
            try
            {
                var typeName = this.GetType().Name.Trim();
                var sql = "update HL7_MessageSet set ExecuteTime = @ExecuteTime where MessageId=@MessageId";
                var dbCommand = SqlDataAccess.GetSqlStringCommand(ctx, sql);
                SqlDataAccess.AddInParameter(ctx, dbCommand, "ExecuteTime", System.Data.DbType.DateTime, elapsedTime);
                SqlDataAccess.AddInParameter(ctx, dbCommand, "MessageId", System.Data.DbType.AnsiString, typeName);
                SqlDataAccess.ExecuteNonQuery(ctx, dbCommand);
            }
            catch (Exception ex)
            {
                MediII.Common.LogHelper.LogError(ex, Common.LogCatagories.MQAccess);
            }
        }

         
        public override long GetMaxSCN()
        {
            return 0;
        }

        protected override DateTime GetNow()
        {
            try
            {
                return (DateTime)SqlDataAccess.ExecuteScalar(ctx, CommandType.Text, "select GETDATE()");
            }
            catch (Exception ex)
            {
                MediII.Common.LogHelper.LogError(ex, Common.LogCatagories.MQAccess);
                return DateTime.Now;
            }
        }
         

       
    }
}
