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;
using System.Xml;

namespace MediII.Adapter.BizComponent.ADT
{
    /// <summary>
    /// 住院就诊记录
    /// </summary>
    public class ADT_A01B02_Handler : ADTHandler
    {
        public override void Handler(string m, string ID)
        {
            var isql = @"insert into dbo.zhuyuan_original(No,patient_zyh,patient_id,RY_DATE,RY_UNIT,RY_BQ,RY_BED,CY_DATE,CY_UNIT,CY_BQ,age,patient_name,Sex,address,JG_DOCT_GH,SZ_DOCT_GH,Nurse_grade,ICD_CODE_RY1,ICD_CODE_RY2,ICD_CODE_RY3,ICD_CODE_RY4,ICD_CODE_CY1,ICD_CODE_CY2,ICD_CODE_CY3,ICD_CODE_CY4,CY_Kind) Value();";

            
           
            //初始化
            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");
            string visitNo = m;
            //拼接接口访问参数
            msgBody = xmlHelper.GetMsgBody(visitNo);
            msgHead = xmlHelper.GetMsgHead("getInHospPresc", cert);
            //接口访问
            rtnMsg = server.TestInterface(msgHead, msgBody);
            //存储访问记录
            MessageHelper.SaveSendMsg(Scanctx, ID, "ADT^A01[B02]", msgHead, msgBody, rtnMsg);
            //消息判断是否成功
            if (!xmlHelper.isSuccess(rtnMsg, ref resultCode, ref result))
            {
                //存储访问记录

                throw new Exception(result);
            }

            //更新发送表，保存留档

            //返回数据解析
            XmlNode root = xmlHelper.GetContentRootNode(rtnMsg);
            XmlNodeList list = root.SelectNodes("//returnContents//returnContent");
            if (list.Count <= 0)
                return;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                var delSql = @"delete from dbo.zhuyuan_medical_order where id=@id";
                var cmd = ctx.DBGetSqlStringCommand(delSql);
                ctx.AddInParameter(cmd, "id", System.Data.DbType.String, visitNo);
                ctx.DBExecuteNonQuery(cmd);//

                var osql = @"insert into dbo.zhuyuan_medical_order(id,medical_order_id,fee_date,yfdm,yfmc,cflx,cfje,cfdzs,yjts,kdksdm,kdys) VALUES(
@id,@medical_order_id,@fee_date,@yfdm,@yfmc,@cflx,@cfje,@cfdzs,@yjts,@kdksdm,@kdys );";

                foreach (XmlElement xn in list)
                {
                    var dcmd = ctx.DBGetSqlStringCommand(osql);
                    ctx.AddInParameter(dcmd, "id", System.Data.DbType.String, xn.SelectSingleNode("id").InnerText);
                    ctx.AddInParameter(dcmd, "medical_order_id", System.Data.DbType.String, xn.SelectSingleNode("medical_order_id").InnerText);
                    ctx.AddInParameter(dcmd, "fee_date", System.Data.DbType.DateTime, xn.SelectSingleNode("fee_date").InnerText);
                    ctx.AddInParameter(dcmd, "yfdm", System.Data.DbType.String, xn.SelectSingleNode("yfdm").InnerText);
                    ctx.AddInParameter(dcmd, "yfmc", System.Data.DbType.String, xn.SelectSingleNode("yfmc").InnerText);
                    ctx.AddInParameter(dcmd, "cflx", System.Data.DbType.String, xn.SelectSingleNode("cflx").InnerText);
                    ctx.AddInParameter(dcmd, "cfje", System.Data.DbType.String, xn.SelectSingleNode("cfje").InnerText);
                    ctx.AddInParameter(dcmd, "cfdzs", System.Data.DbType.String, xn.SelectSingleNode("cfdzs").InnerText);
                    ctx.AddInParameter(dcmd, "yjts", System.Data.DbType.String, xn.SelectSingleNode("yjts").InnerText);
                    ctx.AddInParameter(dcmd, "kdksdm", System.Data.DbType.String, xn.SelectSingleNode("kdksdm").InnerText);
                    ctx.AddInParameter(dcmd, "kdys", System.Data.DbType.String, xn.SelectSingleNode("kdys").InnerText);
                    ctx.DBExecuteNonQuery(dcmd);//
                }

               
                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)));   //看诊次数 

        }

    }
}
