using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediII.Common;
using System.ComponentModel.Composition;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using IL.Common;
using MediII.Adapter.ReceiveToScanModel;

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

        //数据库连接
        protected Database ctx;
        protected Database HealthCarectx;

        static IBizBehavior BizBehavior { get; set; }

        public BaseBizComponent()
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();
            ctx = factory.CreateDefault();
            HealthCarectx = factory.Create("HealthCare");
        }

        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(TaskMessage message, string msgType);

        /// <summary>
        ///实现处理的接口
        /// </summary>
        /// <param name="message">传入的消息字符</param>
        /// <returns></returns>
        public string Process(TaskMessage message,string msgType)
        {
            //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, message.No.ToString(), message.TaskID, "Restful");
            string ack = DoProcess(message,msgType);
            OnProcessed(guid, "", ack, 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 = MySqlDataAccess.GetSqlStringCommand(ctx, "SELECT  SYSDATE() sysdate  ");
            object obj = MySqlDataAccess.ExecuteScalar(ctx, command);
            if (obj != null)
                return Convert.ToDateTime(obj.ToString());
            else
                return Convert.ToDateTime(null);
        }
        #endregion


    }
}
