using System;
using System.Data;
using System.Data.SqlClient;

using HISClinic.DataModel;
using Microsoft.ApplicationBlocks.Data;
using HISClinic.DataServices.Database;

namespace HISClinic.DataServices
{
	/// <summary>
	/// BillOfPrescriptionMapper 的摘要说明。
	/// </summary>
	public class BillOfPrescriptionMapper
	{
		private const string PARAM_BillOfRxID="@BillOfRxID";
		private const string PARAM_CashierID="@CashierID";
		private const string PARAM_CashierName="@CashierName";		
		private const string PARAM_PaymentAmount="@PaymentAmount";

		public BillOfPrescriptionMapper()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		/// <summary>
		/// 保存增加的收费记录及将相应的处方标识为已经收费
		/// </summary>
		/// <param name="prescriptionMasterRows"></param>
		/// <param name="billOfPrescritpionRow"></param>
		/// <returns></returns>
		public bool Insert(PrescriptionsEx.t_PrescriptionMasterRow[] prescriptionMasterRows,BillOfPrescriptionsEx.t_BillOfPrescritpionRow billOfPrescritpionRow)
		{
			bool Result=false;
			using(SqlConnection conn=HISDBConfig.GetConnection())
			{
				if(conn.State== ConnectionState.Closed)
				{
					conn.Open();
				}
				SqlTransaction tran=conn.BeginTransaction();

				try
				{   
					long BillOfRxID=Insert(billOfPrescritpionRow,tran);
					
					if(BillOfRxID<=0)
					{
						throw(new ApplicationException("保存收费记录出错！"));
					}
					
					if(!(new PrescriptionMapper()).BillPrescriptions(prescriptionMasterRows,BillOfRxID,tran))
					{
						throw(new ApplicationException("保存收费记录出错！"));
					}					
					tran.Commit();
				}
				catch(Exception ex)
				{
					tran.Rollback();
					throw(ex);
				}
			}
			return Result;
		}

		/// <summary>
		/// 保存增加的收费记录
		/// </summary>
		/// <param name="billOfPrescritpionRow"></param>
		/// <param name="tran"></param>
		/// <returns></returns>
		private long Insert(BillOfPrescriptionsEx.t_BillOfPrescritpionRow billOfPrescritpionRow,SqlTransaction tran)
		{
			SqlParameter paramBillOfRxID=new SqlParameter();
			paramBillOfRxID.ParameterName=PARAM_BillOfRxID;			
			paramBillOfRxID.SourceColumn=BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_BillOfRxID;
			paramBillOfRxID.Direction=ParameterDirection.Output;		
			paramBillOfRxID.SqlDbType=SqlDbType.BigInt;
			paramBillOfRxID.DbType=DbType.Int64;
			paramBillOfRxID.Size=0;

			SqlParameter[] parameters={
										  paramBillOfRxID,
										  new SqlParameter(PARAM_CashierID,billOfPrescritpionRow[BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_CashierID]),
										  new SqlParameter(PARAM_CashierName,billOfPrescritpionRow[BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_CashierName]),										  
										  new SqlParameter(PARAM_PaymentAmount,billOfPrescritpionRow[BillOfPrescriptionsEx.t_BillOfPrescritpionDataTableEx.FIELD_PaymentAmount])
									  };
			int Result=0;

			SqlCommand comm=new SqlCommand();
			comm.CommandText="ut_BillOfPrescriptionInsert";
			comm.Connection=tran.Connection;
			comm.Transaction=tran;
			comm.CommandType=CommandType.StoredProcedure;	
            
			foreach(SqlParameter p in parameters )
			{
				comm.Parameters.Add(p);
			}

			Result=comm.ExecuteNonQuery();
			
			return Convert.ToInt64(paramBillOfRxID.Value);			
		}
	}
}
