using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Transactions;
using MediII.Adapter.ReceiveToScanModel;
using IL.Common;
using System.Data.Common;
using System.Data;

namespace MediII.Adapter.BizComponent.ADT
{
    public class ADT_A02_Handler : ADTHandler
    {
        public override void Handler(string m)
        {
            #region 门诊就诊更新
            //初始化
            var rtnMsg = string.Empty;
            var msgHead = string.Empty;
            var msgBody = string.Empty;
            var resultCode = string.Empty;
            var result = string.Empty;
            WebService server = new WebService();
            OperateXmlUtil xmlHelper = new OperateXmlUtil();
            //读取配置的凭证号
            var cert = System.Configuration.ConfigurationManager.AppSettings["cert"];
            //读取就诊流水号
            string visitNo = xmlHelper.XpathRead(m, "//*[@root='2.16.156.10011.2.5.1.9']", "extension");
            var tmp = xmlHelper.XpathRead(m, "//*[name()='effectiveTime']");
            var visitDate = tmp[0].LastChild.Attributes["value"].Value;
            DateTime date = DateTime.ParseExact(visitDate, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
           {
               #region 门诊就诊更新 

               var isql = @"INSERT INTO HL7_Platform.dbo.HL7_Scan  (ID ,ChartNo,VisitNo,OtherNo1,OtherNo2,OtherNo3,OtherName ,  TaskType ,  TaskStatus ,  UpdateUser ,  UpdateTime,Reserve1) VALUES (@ID ,@ChartNo ,@VisitNo ,@OtherNo1 ,  @OtherNo2 ,@OtherNo3 ,@OtherName ,@TaskType ,@TaskStatus , @UpdateUser , GETDATE(),@Reserve1)";
               //门诊处方单
               var dcmd = ctx.DBGetSqlStringCommand(isql);
               ctx.AddInParameter(dcmd, "id", System.Data.DbType.String, Guid.NewGuid().ToString("N"));
               ctx.AddInParameter(dcmd, "ChartNo", System.Data.DbType.String, visitNo);
               ctx.AddInParameter(dcmd, "VisitNo", System.Data.DbType.String, visitNo);
               ctx.AddInParameter(dcmd, "OtherNo1", System.Data.DbType.String, date.ToString("yyyy-MM-dd HH:mm:ss"));
               ctx.AddInParameter(dcmd, "OtherNo2", System.Data.DbType.String, "");
               ctx.AddInParameter(dcmd, "OtherNo3", System.Data.DbType.String, "");
               ctx.AddInParameter(dcmd, "OtherName", System.Data.DbType.String, "门诊处方单");
               ctx.AddInParameter(dcmd, "TaskType", System.Data.DbType.String, "ADT^A02[B01]");
               ctx.AddInParameter(dcmd, "TaskStatus", System.Data.DbType.Int32, 1);
               ctx.AddInParameter(dcmd, "UpdateUser", System.Data.DbType.String, "9999");
               ctx.AddInParameter(dcmd, "Reserve1", System.Data.DbType.String, "");
               ctx.DBExecuteNonQuery(dcmd);
        
               #endregion
               //门诊收费明细
               var lcmd = ctx.DBGetSqlStringCommand(isql);
               ctx.AddInParameter(lcmd, "id", System.Data.DbType.String, Guid.NewGuid().ToString("N"));
               ctx.AddInParameter(lcmd, "ChartNo", System.Data.DbType.String, visitNo);
               ctx.AddInParameter(lcmd, "VisitNo", System.Data.DbType.String, visitNo);
               ctx.AddInParameter(lcmd, "OtherNo1", System.Data.DbType.String, date.ToString("yyyy-MM-dd HH:mm:ss"));
               ctx.AddInParameter(lcmd, "OtherNo2", System.Data.DbType.String, "");
               ctx.AddInParameter(lcmd, "OtherNo3", System.Data.DbType.String, "");
               ctx.AddInParameter(lcmd, "OtherName", System.Data.DbType.String, "门诊收费明细");
               ctx.AddInParameter(lcmd, "TaskType", System.Data.DbType.String, "ADT^A02[B02]");
               ctx.AddInParameter(lcmd, "TaskStatus", System.Data.DbType.Int32, 1);
               ctx.AddInParameter(lcmd, "UpdateUser", System.Data.DbType.String, "9999");
               ctx.AddInParameter(lcmd, "Reserve1", System.Data.DbType.String, "");
               ctx.DBExecuteNonQuery(lcmd);
    
               scope.Complete();
            }
            #endregion
            

        }

        /// <summary>
        /// 获取急诊病人床位
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="visitNo"></param>
        /// <returns></returns>
        private string GetDepartNo(Database ctx, string roomBed)
        {

            var sql = string.Format("SELECT top 1 DepartNo FROM dbo.Depart WHERE DepartCode='{0}' AND DepartType='99' AND IsDel=0", roomBed);
            var dr = ctx.DBExecuteScalar(CommandType.Text, sql);
            return StrHelepr.Obj2StrTrim(dr);
        }



    }
}