using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Data;
using System.Data.Common;
using IL.Common;

namespace MediII.Adapter.BizComponent.ADT
{
    /// <summary>
    /// 住院就诊记录
    /// </summary>
    public class ADT_A04_Handler : ADTHandler
    {
        public override void Handler(string m)
        {
            //初始化
            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))
            {
                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 cmd = ctx.DBGetSqlStringCommand(isql);
                SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.AnsiString, Guid.NewGuid().ToString("N"));
                SqlDataAccess.AddInParameter(ctx, cmd, "ChartNo", DbType.AnsiString, visitNo);
                SqlDataAccess.AddInParameter(ctx, cmd, "VisitNo", DbType.AnsiString, visitNo);
                SqlDataAccess.AddInParameter(ctx, cmd, "OtherNo1", DbType.AnsiString, date.ToString("yyyy-MM-dd HH:mm:ss"));
                SqlDataAccess.AddInParameter(ctx, cmd, "OtherNo2", DbType.AnsiString, "");
                SqlDataAccess.AddInParameter(ctx, cmd, "OtherNo3", DbType.AnsiString, "");
                SqlDataAccess.AddInParameter(ctx, cmd, "OtherName", DbType.AnsiString, "住院就诊信息登记");
                SqlDataAccess.AddInParameter(ctx, cmd, "TaskType", DbType.AnsiString, "ADT^A04[B01]");
                SqlDataAccess.AddInParameter(ctx, cmd, "TaskStatus", DbType.Int32, 1);
                SqlDataAccess.AddInParameter(ctx, cmd, "UpdateUser", DbType.AnsiString, "9999");
                SqlDataAccess.AddInParameter(ctx, cmd, "Reserve1", DbType.AnsiString, "");
                SqlDataAccess.ExecuteNonQuery(ctx, cmd);

                //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^A01[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(cmd);
                
               
                scope.Complete(); 
            }


        }

        /// <summary>
        /// 获取门诊就诊次数
        /// </summary> 
        /// <returns></returns>
        private int GetOutNumByChartNo(string chartNo)
        {

            string OutPatientSql = @"select count(1)	 
                                             from HealthCare.dbo.OutPatient a, HealthCare.dbo.VisitRecord b	 
                                             where a.VisitNo = b.VisitNo  AND b.ChartNo= @ChartNo  
                                             AND a.VisitStatus<>'D'  ";
            var cmd = SqlDataAccess.GetSqlStringCommand(ctx, OutPatientSql);
            SqlDataAccess.AddInParameter(ctx, cmd, "ChartNo", DbType.AnsiString, chartNo);
            return int.Parse(StrHelepr.Obj2StrTrim(SqlDataAccess.ExecuteScalar(ctx, cmd)));  //看诊次数 

        }

        /// <summary>
        /// 获取急诊就诊次数
        /// </summary> 
        /// <returns></returns>
        private int GetEmgNumByChartNo(string chartNo)
        {
            string EmgPatientSql = @"select count(1) from HealthCare.dbo.Emergency a, HealthCare.dbo.VisitRecord b	 
                                             where a.VisitNo = b.VisitNo  
                                             AND b.ChartNo= @ChartNo   AND a.VisitStatus<>'D'  ";
            var cmd = SqlDataAccess.GetSqlStringCommand(ctx, EmgPatientSql);
            SqlDataAccess.AddInParameter(ctx, cmd, "ChartNo", DbType.AnsiString, chartNo);
            return int.Parse(StrHelepr.Obj2StrTrim(SqlDataAccess.ExecuteScalar(ctx, cmd)));   //看诊次数 

        }

    }
}
