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

namespace MediII.Adapter.BizComponent.ADT
{
    public class ADT_A01_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 (var 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 dcmd = ctx.DBGetSqlStringCommand(isql);
                ctx.DBAddInParameter(dcmd, "id", System.Data.DbType.String, Guid.NewGuid().ToString("N"));
                ctx.DBAddInParameter(dcmd, "ChartNo", System.Data.DbType.String, visitNo);
                ctx.DBAddInParameter(dcmd, "VisitNo", System.Data.DbType.String, visitNo);
                ctx.DBAddInParameter(dcmd, "OtherNo1", System.Data.DbType.String, date.ToString("yyyy-MM-dd HH:mm:ss"));
                ctx.DBAddInParameter(dcmd, "OtherNo2", System.Data.DbType.String, "");
                ctx.DBAddInParameter(dcmd, "OtherNo3", System.Data.DbType.String, "");
                ctx.DBAddInParameter(dcmd, "OtherName", System.Data.DbType.String, "门诊处方单");
                ctx.DBAddInParameter(dcmd, "TaskType", System.Data.DbType.String, "ADT^A01[B01]");
                ctx.DBAddInParameter(dcmd, "TaskStatus", System.Data.DbType.Int32, 1);
                ctx.DBAddInParameter(dcmd, "UpdateUser", System.Data.DbType.String, "9999");
                ctx.DBAddInParameter(dcmd, "Reserve1", System.Data.DbType.String, "");
                ctx.DBExecuteNonQuery(dcmd);

                //门诊收费明细
                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^A01[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();
            }
        }


        public string GetDiagNo(string icdCode)
        {
            try
            {
                var sql = string.Format("SELECT DiagNo FROM HealthCare.dbo.Diag WHERE IcdCode='{0}'", icdCode);
                var obj = ctx.DBExecuteScalar(CommandType.Text, sql);
                var val = StrHelepr.Obj2StrTrim(obj);
                if (string.IsNullOrWhiteSpace(val))
                {
                    val = icdCode;
                }

                return val;
            }
            catch (Exception ex)
            {
                IL.Common.LogHelper.LogError(string.Format("获取DiagNo错误:{0}  {1}\r\n{2}", icdCode, ex.Message, ex.StackTrace), LogCatagories.MQAccess);
                return string.Empty;
            }
        }

        public string GetBedNo(Database ctx, string roombed)
        {
            var sql = "SELECT Top 1 BedNo FROM HealthCare.dbo.Bed WHERE RoomBed=@RoomBed";
            var cmd = ctx.DBGetSqlStringCommand(sql);
            ctx.AddInParameter(cmd, "RoomBed", DbType.String, roombed);
            var obj = StrHelepr.Obj2StrTrim(ctx.DBExecuteScalar(cmd));
            if (string.IsNullOrWhiteSpace(obj))
            {
                obj = roombed;
            }
            return obj;

        }
    }


}
