using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHapi.Base.Model;
using NHapi.Base.Parser;
using NHapi.Model.V24.Segment;
using NHapi.Model.V24.Message;
using MediII.Common;
using NHapi.Base.Util;
using System.ComponentModel.Composition;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using IL.Common;

namespace MediII.Adapter.BizComponent.Base
{
    [Export]
    public abstract class BaseBizComponent : IBizComponent
    {


        protected PipeParser parser = new PipeParser();
        //数据库连接
        protected Database ctx;

        static IBizBehavior BizBehavior { get; set; }

        public BaseBizComponent()
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();
            ctx = factory.CreateDefault();
        }

        static BaseBizComponent()
        {
            string bizBehavior = System.Configuration.ConfigurationManager.AppSettings["BizBehavior"];
            if (string.IsNullOrEmpty(bizBehavior))
            {
                BizBehavior = new DefaultBizBehavior();
            }
            else
            {
                string[] ar = bizBehavior.Split(',');
                string assemblyName = ar[0];
                string typeName = ar[1];
                try
                {
                    BizBehavior = Activator.CreateInstance(assemblyName, typeName).Unwrap() as IBizBehavior;
                }
                catch
                {
                    BizBehavior = new DefaultBizBehavior();
                }
            }
        }


        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="message">解析后消息的抽象类型</param>
        public abstract string DoProcess(string message,string msgType,string ID);

        /// <summary>
        ///实现处理的接口
        /// </summary>
        /// <param name="message">传入的消息字符</param>
        /// <returns></returns>
        public string Process(string message, string msgType,string ID)
        {
            //IMessage m = parser.Parse(message);
            //ISegment msh = (ISegment)m.GetStructure("MSH");
            //string messageID = Terser.Get(msh, 10, 0, 1, 1);
            OperateXmlUtil xmlHelper = new OperateXmlUtil();
            string messageID = xmlHelper.XpathRead(message, "//*[@root='2.16.156.10011.2.5.1.1']", "extension");
            string guid = MediII.Common.GUIDHelper.NewGUID();
            OnProcessing(guid, messageID, message,"HL7v3");
            string ack = DoProcess(message, msgType, ID);
            OnProcessed(guid, "", "", true);
            return ack;
        }

        /// <summary>
        /// 保存回复
        /// </summary>
        /// <param name="ack"></param>
        private void OnProcessed(string id, string messageID, string ack, bool successed)
        {
            BizBehavior.AfterProcess(id, messageID, ack, successed);
        }

        /// <summary>
        /// 保存接受信息
        /// </summary>
        /// <param name="message"></param>
        private void OnProcessing(string id, string messageID, string message, string Version)
        {
            BizBehavior.BeforeProcess(id, messageID, message, Version);
        }


        #region 取DB服务器当前时间
        public static DateTime GetNow(Database ctx)
        {
            DbCommand command = SqlDataAccess.GetSqlStringCommand(ctx, "SELECT  GETDATE() sysdate  ");
            object obj = SqlDataAccess.ExecuteScalar(ctx, command);
            if (obj != null)
                return Convert.ToDateTime(obj.ToString());
            else
                return Convert.ToDateTime(null);
        }
        #endregion


    }
}
