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_A04B01_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("getOutPresc", cert);
            //接口访问
            rtnMsg = server.TestInterface(msgHead, msgBody);
            //存储访问记录
            MessageHelper.SaveSendMsg(Scanctx, ID, "ADT^A04[B01]", 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.menzhen_medical_order where id=@id";
                var cmd = ctx.DBGetSqlStringCommand(delSql);
                ctx.AddInParameter(cmd, "id", System.Data.DbType.String, visitNo);
                ctx.DBExecuteNonQuery(cmd);//

                var dsql = @"INSERT INTO dbo.menzhen_medical_order
(id,medical_order_id,fee_date,yfdm,yfmc,cflx,cfje,sycfbz,yjts,kdksdm,kdys) 
VALUES(@id,@medical_order_id,@fee_date,@yfdm,@yfmc,@cflx,@cfje,@sycfbz,@yjts,@kdksdm,@kdys)";

                foreach (XmlElement xn in list)
                {
                    var dcmd = ctx.DBGetSqlStringCommand(dsql);
                    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, "sycfbz", System.Data.DbType.String, xn.SelectSingleNode("sycfbz").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();
                //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;

        }
    }


}
