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

namespace MediII.Adapter.BizComponent.DFT
{
    public class BizComponent_DFT : BaseBizComponent, IBizComponent
    {
        const string Insert = "P01"; //财务明细


        //数据库连接
        protected Database ctx;
        protected Database Scanctx;
        public BizComponent_DFT()
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();
            ctx = factory.Create("HealthCare");
            Scanctx = factory.CreateDefault();
        }
        ////HealthCareContainer HealthCareContainer = new HealthCareContainer();
        //private Database ctx = null;
        //public BizComponent_PMU(Database dbCtx)
        //{
        //    ctx = dbCtx;
        //}
        public override string DoProcess(string m, string msgType)
        {
            string strMsgType = null;
            var resultCode = string.Empty;
            var result = string.Empty;
            

            //
            OperateXmlUtil xmlHelper = new OperateXmlUtil();

            //返回数据解析
            XmlNode root = xmlHelper.GetContentRootNode(m);
            XmlNodeList list = root.SelectNodes("//FinaDetails//FinaDetail");
            if (list.Count <= 0)
                return string.Empty;
            int num = 0;

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                 foreach (XmlElement xn in list)
                 {
                     var year = xn.SelectSingleNode("year").InnerText;
                     var month = xn.SelectSingleNode("month").InnerText;
                     var deptCode = xn.SelectSingleNode("deptCode").InnerText;
                     var acntCpde = xn.SelectSingleNode("acntCpde").InnerText;
                   
                    num = GetCount(year, month, deptCode, acntCpde);
                     if (num>0)
                         UpDateData(xn);
                     else
                         AddData(xn);
                    
                 }
                scope.Complete();
            }
           
            return string.Empty;
        }
        public void AddData(XmlElement xn)
        {
            #region 新增
            string sql = @"INSERT INTO dbo.data_caiwu
                                    ( fee_date ,
                                    year ,
                                    month ,
                                    unit_id ,
                                    unit_name,
cost_code,cost_name,fee
                                    )
                            VALUES  ( @feeDate,
                                    @year ,
                                    @month ,
                                    @unit_id ,
                                    @unit_name,@cost_code,@cost_name,@fee)";
            var year = xn.SelectSingleNode("year").InnerText;
            var month = xn.SelectSingleNode("month").InnerText;
            var deptCode = xn.SelectSingleNode("deptCode").InnerText;
            var deptName = xn.SelectSingleNode("deptName").InnerText;
            var acntCpde = xn.SelectSingleNode("acntCpde").InnerText;
            var acntName = xn.SelectSingleNode("acntName").InnerText;
            decimal Cost = 0;
            var costtmp = xn.SelectSingleNode("Cost").InnerText;
            decimal.TryParse(costtmp, out Cost);
            DateTime feeDate = Convert.ToDateTime("1999-01-01 00:00:00");
            var feedatetmp = xn.SelectSingleNode("feeDate").InnerText;
            DateTime.TryParse(feedatetmp, out feeDate);

            var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
            SqlDataAccess.AddInParameter(ctx,cmd, "year", System.Data.DbType.String, year);
            SqlDataAccess.AddInParameter(ctx, cmd, "month", System.Data.DbType.String, month);
            SqlDataAccess.AddInParameter(ctx, cmd, "feeDate", System.Data.DbType.DateTime, feeDate);
            SqlDataAccess.AddInParameter(ctx, cmd, "unit_id", System.Data.DbType.String, deptCode);
            SqlDataAccess.AddInParameter(ctx, cmd, "unit_name", System.Data.DbType.String, deptName);
            SqlDataAccess.AddInParameter(ctx, cmd, "cost_code", System.Data.DbType.String, acntCpde);
            SqlDataAccess.AddInParameter(ctx, cmd, "cost_name", System.Data.DbType.String, acntName);
            SqlDataAccess.AddInParameter(ctx, cmd, "fee", System.Data.DbType.Decimal, Cost);
            SqlDataAccess.ExecuteNonQuery(ctx, cmd);
            #endregion
        }
        public void UpDateData(XmlElement xn)
        {
            #region 修改
            string sql1 = @"update dbo.data_caiwu set 
                                    fee_date =@feeDate,
                                    unit_name =@unit_name,
                                    cost_name =@cost_name,
                                    fee=@fee
                                where year=@year and month=@month and unit_id=@unit_id and cost_code=@cost_code";
            
            var year = xn.SelectSingleNode("year").InnerText;
            var month = xn.SelectSingleNode("month").InnerText;
            var deptCode = xn.SelectSingleNode("deptCode").InnerText;
            var deptName = xn.SelectSingleNode("deptName").InnerText;
            var acntCpde = xn.SelectSingleNode("acntCpde").InnerText;
            var acntName = xn.SelectSingleNode("acntName").InnerText;
            decimal Cost = 0;
            var costtmp = xn.SelectSingleNode("Cost").InnerText;
            decimal.TryParse(costtmp, out Cost);
            DateTime feeDate = Convert.ToDateTime("1999-01-01 00:00:00");
            var feedatetmp = xn.SelectSingleNode("feeDate").InnerText;
            DateTime.TryParse(feedatetmp, out feeDate);

            DbCommand command1 = ctx.GetSqlStringCommand(sql1.ToString());
            SqlDataAccess.AddInParameter(ctx, command1, "year", System.Data.DbType.String, year);
            SqlDataAccess.AddInParameter(ctx, command1, "month", System.Data.DbType.String, month);
            SqlDataAccess.AddInParameter(ctx, command1, "feeDate", System.Data.DbType.DateTime, feeDate);
            SqlDataAccess.AddInParameter(ctx, command1, "unit_id", System.Data.DbType.String, deptCode);
            SqlDataAccess.AddInParameter(ctx, command1, "unit_name", System.Data.DbType.String, deptName);
            SqlDataAccess.AddInParameter(ctx, command1, "cost_code", System.Data.DbType.String, acntCpde);
            SqlDataAccess.AddInParameter(ctx, command1, "cost_name", System.Data.DbType.String, acntName);
            SqlDataAccess.AddInParameter(ctx, command1, "fee", System.Data.DbType.Decimal, Cost);
            SqlDataAccess.ExecuteNonQuery(ctx, command1);
            #endregion
        }

        protected int GetCount(string year,string month,string deptCode,string acntCode)
        {
            var sql = "SELECT count(1) FROM dbo.data_caiwu WITH(NOLOCK) WHERE year=@year and month=@month and unit_id=@deptCode and cost_code=@acntCode";
            var cmd = ctx.DBGetSqlStringCommand(sql);
            ctx.AddInParameter(cmd, "year", DbType.String, year);
            ctx.AddInParameter(cmd, "month", DbType.String, month);
            ctx.AddInParameter(cmd, "deptCode", DbType.String, deptCode);
            ctx.AddInParameter(cmd, "acntCode", DbType.String, acntCode);
            var obj = int.Parse(StrHelepr.Obj2StrTrim(ctx.DBExecuteScalar(cmd)));
            return obj;
        }
      

    }

}