using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Transactions;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using IL.Common;
using System.Xml;
//using MediII.Adapter.Model.UE;


namespace MediII.Adapter.BizComponent.MFN
{
    /// <summary>
    /// 公用科室
    /// zengfan 2012-09-04
    /// </summary>
    /// <remarks></remarks>
    public class MFN_Z2B_GY_KESHI : MFNBase
    {
        //数据库连接
        //HealthCareContainer HealthCareContainer = new HealthCareContainer();
        private Database ctx = null;
        public MFN_Z2B_GY_KESHI(Database dbCtx)
        {
            ctx = dbCtx;
        }
        /// <summary>
        /// 消息处理，入库
        /// </summary>
        /// <param name="message">转化过的强类型消息</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public override int Process(string message)
        {
            string strMsgType;
            using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                string sqlSel = "select count(*) from dbo.dic_department where id=@id";
                OperateXmlUtil helper = new OperateXmlUtil();
                string pkid = helper.XpathRead(message, "//*[@root='2.16.156.10011.1.26']", "extension");
                DbCommand cmd=ctx.DBGetSqlStringCommand(sqlSel);
                ctx.AddInParameter(cmd, "id", System.Data.DbType.String, pkid);
                int counts = Convert.ToInt32(ctx.ExecuteScalar(cmd));

                if (counts == 0)
                {
                    string sql = @"insert into dbo.dic_department(id,
                                                                name,
									                            status,
									                            search_code)
							                            VALUES(@id,
                                                                @name,
									                            @status,
									                            @search_code)";

                    OperateXmlUtil xmlHelper = new OperateXmlUtil();
                    DbCommand command = ctx.GetSqlStringCommand(sql.ToString());
                    Resolving(message, ctx, command);

                    ctx.ExecuteNonQuery(command);
                }
                else
                {
                    string sql = "update dbo.dic_department set name=@name,search_code=@search_code where id=@id";
                    OperateXmlUtil xmlHelper = new OperateXmlUtil();
                    DbCommand command = ctx.GetSqlStringCommand(sql.ToString());
                    Resolving(message, ctx, command);

                    ctx.ExecuteNonQuery(command);
                }
                scope.Complete();
            }
            return 1;
        }


        /// <summary>
        /// 科室删除
        /// </summary>
        /// <param name="seg">传入资料</param>
        /// <param name="HealthCareContainer">连线</param>
        /// <param name="scope">交易</param>
        public void Division_SyncDel(string pkid)
        {
            //string sql4 = "update HealthCare.dbo.Division set IsIss=1 where DivNo=@DivNo";
            string sql4 = "delete from HealthCare.dbo.Division where DivNo=@DivNo";
            DbCommand command4 = ctx.GetSqlStringCommand(sql4.ToString());
            ctx.AddInParameter(command4, "DivNo", System.Data.DbType.AnsiString, pkid);
            ctx.ExecuteNonQuery(command4);
        }

        public void Resolving(string Content, Database ctx, DbCommand command)
        {
            string value = string.Empty;
            OperateXmlUtil xmlHelper = new OperateXmlUtil();

            value = xmlHelper.XpathRead(Content, "//*[@root='2.16.156.10011.1.26']", "extension");
            ctx.AddInParameter(command, "id", System.Data.DbType.String, value);
            var valueNode = xmlHelper.XpathRead(Content, "//*[name()='assignedPrincipalOrganization']").Item(0);
      
            foreach (XmlNode xe in valueNode.ChildNodes)
            {
                if (xe.Name == "name")
                {
                    value = xe.LastChild.LastChild.Attributes["value"].Value;
                    ctx.AddInParameter(command, "name", System.Data.DbType.String, value);
                    value = xe.LastChild.LastChild.Attributes["code"].Value;
                    ctx.AddInParameter(command, "search_code", System.Data.DbType.String, value);
                    break;
                }
            }
        }
    }
}
