using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHapi.Model.V24.Message;
using NHapi.Model.V24.Group;
using NHapi.Model.V24.Segment;
using System.Data.Common;
using System.Transactions;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using IL.Common;

namespace MediII.Adapter.BizComponent.MFN
{
   public class MFN_Z2A_HisInfo : MFNBase
    {
       private Database ctx = null;
       public MFN_Z2A_HisInfo(Database dbCtx)
        {
            ctx = dbCtx;
        }
        public override int Process(NHapi.Base.Model.IMessage message)
        {
            
            //取得消息类型 MAD新增 MUP更新 MDL删除 "MDC"作废 "MAC"恢复
            string strMsgType;
            
            MFN_Z2A msg = message as MFN_Z2A;
        
            using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                for (int i = 0; i < msg.MFRepetitionsUsed; i++)
                {
                    MFN_Z2A_Nofification group = msg.GetMF(i);
                    strMsgType = group.MFE.RecordLevelEventCode.Value;

                    string pkid = group.Z2A.YuanQuId.Value;
                    string qurysql = "select count(*) from HealthCare.dbo.HisInfo where Zone=@Zone";
                    DbCommand command = ctx.GetSqlStringCommand(qurysql.ToString());
                    ctx.AddInParameter(command, "Zone", System.Data.DbType.String, pkid);
                    int counts = Convert.ToInt32(ctx.ExecuteScalar(command));
                    if (counts == 0)
                    {
                        // 新增
                        AddData(group, pkid);
                    }
                    else
                    {
                        
                        if ("MUP" == strMsgType || "MAD" == strMsgType)
                        {
                            UpDateData(group, pkid);//修改
                        }
                        if ("MDL" == strMsgType)
                        {
                            #region 删除/作废
                            //StringBuilder sql2 = new StringBuilder();
                            //sql2.Append("update HealthCare.dbo.DrugStock set IsDelete=1  WHERE YaoPinKcID=@YaoPinKcID");
                            //DbCommand command2 = ctx.GetSqlStringCommand(sql2.ToString());
                            //ctx.AddInParameter(command2, "YaoPinKcID", System.Data.DbType.AnsiString, pkid);
                            //ctx.ExecuteNonQuery(command2);
                            #endregion
                        }
                    }
                }
                scope.Complete();
            }
            return 1;
        }
        private void AddData(MFN_Z2A_Nofification group, string pkid)
        {
            string sql = @"INSERT INTO HealthCare.dbo.HisInfo
                                                          ( HospCode ,
                                                            VAT ,
                                                            HospName ,
                                                            ShortName ,
                                                            EngName ,
                                                            Zone ,
                                                            ZoneName ,
                                                            Tel ,
                                                            AdrArea ,
                                                            AdrDesc ,
                                                            EngAdrDesc ,
                                                            EMail ,
                                                            LicenseNo ,
                                                            HasDelete ,
                                                            UpdateUser ,
                                                            UpdateTime ,
                                                            HisZone
                                                          )
                                                  VALUES  ( @HospCode ,
                                                            @VAT,
                                                            @HospName ,
                                                            @ShortName,
                                                            @EngName ,
                                                            @Zone ,
                                                            @ZoneName ,
                                                            @Tel ,
                                                            @AdrArea,
                                                            @AdrDesc ,
                                                            @EngAdrDesc ,
                                                            @EMail ,
                                                            @LicenseNo ,
                                                            @HasDelete ,
                                                            @UpdateUser ,
                                                            @UpdateTime ,
                                                            @HisZone 
                                                          )";
            DbCommand cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
            SetEntity(group.Z2A, ctx, cmd);
            SqlDataAccess.AddInParameter(ctx, cmd, "Zone", DbType.AnsiString, pkid);
            SqlDataAccess.ExecuteNonQuery(ctx, cmd);
        }
        private void UpDateData(MFN_Z2A_Nofification group, string pkid)
        {
        }
        private void SetEntity(Z2A seg, Database ctx, DbCommand cmd)
        {
            SqlDataAccess.AddInParameter(ctx, cmd, "HospCode", DbType.AnsiString, seg.YuanQuBh.Value);
            SqlDataAccess.AddInParameter(ctx, cmd, "VAT", DbType.AnsiString, seg.YuanQuBh.Value);
            SqlDataAccess.AddInParameter(ctx, cmd, "HospName", DbType.AnsiString, seg.YuanQuMc.Value);
            SqlDataAccess.AddInParameter(ctx, cmd, "ShortName", DbType.AnsiString, seg.YiYuanJc.Value);
            SqlDataAccess.AddInParameter(ctx, cmd, "HasDelete", DbType.Int16, seg.ZuoFeiBZ.Value=="1"? 1 : 0);

            SqlDataAccess.AddInParameter(ctx, cmd, "UpdateUser", DbType.AnsiString, seg.XiuGaiRen.IDNumber.Value);//修改人
            SqlDataAccess.AddInParameter(ctx, cmd, "UpdateTime", DbType.DateTime, seg.XiuGaiSJ.TimeOfAnEvent.GetAsDate());//修改时间
        }
    }
}
