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_A04B02_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("getOutCostDetail", cert);
            //接口访问
            rtnMsg = server.TestInterface(msgHead, msgBody);
            //存储访问记录
            MessageHelper.SaveSendMsg(Scanctx, ID, "ADT^A04[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 (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {

                var delSql = @"delete from dbo.original_data_menzhen where mz_id=@mz_id";
                var cmd = ctx.DBGetSqlStringCommand(delSql);
                ctx.AddInParameter(cmd, "mz_id", System.Data.DbType.String, visitNo);
                ctx.DBExecuteNonQuery(cmd);//
                //门诊收费明细

                var osql = @"insert into dbo.original_data_menzhen(mz_id,patient_id,fee_date,order_unit_id,order_doctor_id,exec_unit_id,exec_doctor_id,fee_type_id,fee_type_name,item_id,item_name,item_quantity,price,fee) VALUES(
@mz_id,@patient_id,@fee_date,@order_unit_id,@order_doctor_id,@exec_unit_id,@exec_doctor_id,@fee_type_id,@fee_type_name,@item_id,@item_name,@item_quantity,@price,@fee);";
                foreach (XmlElement xn in list)
                {
                    var ocmd = ctx.DBGetSqlStringCommand(osql);
                    ctx.AddInParameter(ocmd, "mz_id", System.Data.DbType.String, xn.SelectSingleNode("mz_id").InnerText);
                    ctx.AddInParameter(ocmd, "patient_id", System.Data.DbType.String, xn.SelectSingleNode("patient_id").InnerText);
                    ctx.AddInParameter(ocmd, "fee_date", System.Data.DbType.DateTime, xn.SelectSingleNode("fee_date").InnerText);
                    ctx.AddInParameter(ocmd, "order_unit_id", System.Data.DbType.String, xn.SelectSingleNode("order_unit_id").InnerText);
                    ctx.AddInParameter(ocmd, "order_doctor_id", System.Data.DbType.String, xn.SelectSingleNode("order_doctor_id").InnerText);
                    ctx.AddInParameter(ocmd, "exec_unit_id", System.Data.DbType.String, xn.SelectSingleNode("exec_unit_id").InnerText);
                    ctx.AddInParameter(ocmd, "exec_doctor_id", System.Data.DbType.String, xn.SelectSingleNode("exec_doctor_id").InnerText);
                    ctx.AddInParameter(ocmd, "fee_type_id", System.Data.DbType.String, xn.SelectSingleNode("fee_type_id").InnerText);
                    ctx.AddInParameter(ocmd, "fee_type_name", System.Data.DbType.String, xn.SelectSingleNode("fee_type_name").InnerText);
                    ctx.AddInParameter(ocmd, "item_id", System.Data.DbType.String, xn.SelectSingleNode("item_id").InnerText);
                    ctx.AddInParameter(ocmd, "item_name", System.Data.DbType.String, xn.SelectSingleNode("item_name").InnerText);
                    ctx.AddInParameter(ocmd, "item_quantity", System.Data.DbType.String, xn.SelectSingleNode("item_quantity").InnerText);
                    ctx.AddInParameter(ocmd, "price", System.Data.DbType.String, xn.SelectSingleNode("price").InnerText);
                    ctx.AddInParameter(ocmd, "fee", System.Data.DbType.String, xn.SelectSingleNode("fee").InnerText);
                    ctx.DBExecuteNonQuery(ocmd);//
                }
                
                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;

        }
    }


}
