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

namespace MediII.Adapter.BizComponent.ADT
{
    public class ADT_A04B03_Handler : ADTHandler
    {
        public override void Handler(string m, string ID)
        {
            //门诊就诊，挂号
            //初始化
            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 = m;
            //拼接接口访问参数
            msgBody = xmlHelper.GetMsgBody(visitNo);
            msgHead = xmlHelper.GetMsgHead("getRegisterDetail", cert);
            //接口访问
            rtnMsg = server.TestInterface(msgHead, msgBody);
            //存储访问记录
            MessageHelper.SaveSendMsg(Scanctx, ID, "ADT^A04[B03]", 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 (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                var delSql = @"delete from dbo.v_zhencha where VISITNO=@VISITNO";
                var cmd = ctx.DBGetSqlStringCommand(delSql);
                ctx.AddInParameter(cmd, "VISITNO", System.Data.DbType.String, visitNo);
                ctx.DBExecuteNonQuery(cmd);//
                //挂号

                var fsql = @"insert into dbo.v_zhencha(VISITNO,fee_date,unit_id,unit_name,doct_code,doct_name,item_id,item_name,item_quantity,fee,fee_kind) VALUES(
@VISITNO,@fee_date,@unit_id,@unit_name,@doct_code,@doct_name,@item_id,@item_name,@item_quantity,@fee,@fee_kind)";
                foreach (XmlElement xn in list)
                {
                    var gcmd = ctx.DBGetSqlStringCommand(fsql);
                    ctx.AddInParameter(gcmd, "VISITNO", System.Data.DbType.String, visitNo);
                    ctx.AddInParameter(gcmd, "fee_date", System.Data.DbType.DateTime, xn.SelectSingleNode("fee_date").InnerText);
                    ctx.AddInParameter(gcmd, "unit_id", System.Data.DbType.String, xn.SelectSingleNode("unit_id").InnerText);
                    ctx.AddInParameter(gcmd, "unit_name", System.Data.DbType.String, xn.SelectSingleNode("unit_name").InnerText);
                    ctx.AddInParameter(gcmd, "doct_code", System.Data.DbType.String, xn.SelectSingleNode("doct_code").InnerText);
                    ctx.AddInParameter(gcmd, "doct_name", System.Data.DbType.String, xn.SelectSingleNode("doct_name").InnerText);
                    ctx.AddInParameter(gcmd, "item_id", System.Data.DbType.String, xn.SelectSingleNode("item_id").InnerText);
                    ctx.AddInParameter(gcmd, "item_name", System.Data.DbType.String, xn.SelectSingleNode("item_name").InnerText);
                    ctx.AddInParameter(gcmd, "item_quantity", System.Data.DbType.String, xn.SelectSingleNode("item_quantity").InnerText);
                    ctx.AddInParameter(gcmd, "fee", System.Data.DbType.String, xn.SelectSingleNode("fee").InnerText);
                    ctx.AddInParameter(gcmd, "fee_kind", System.Data.DbType.String, xn.SelectSingleNode("fee_kind").InnerText);
                    ctx.DBExecuteNonQuery(gcmd);//
                }
                scope.Complete();
                //DiagFlag = 20; 
            }

        }


        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;

        }
    }


}
