using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using HISClinic.DataModel;
using HISClinic.DataServices.Database;

namespace HISClinic.DataServices
{
	/// <summary>
	/// RegistrationMapper 的摘要说明。
	/// </summary>
	public class RegistrationMapper
	{
		private const string PARAM_RegistrationID="@RegistrationID";
		private const string PARAM_PatientID="@PatientID";
		private const string PARAM_RegistrationTypeID="@RegistrationTypeID";
		private const string PARAM_DeparptmentID="@DeparptmentID";		
		private const string PARAM_RegistrarID="@RegistrarID";
		private const string PARAM_DoctorID="@DoctorID";		
		private const string PARAM_Status="@Status";

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

		/// <summary>
		///向数据库增加一条挂号信息 
		/// </summary>
		/// <param name="registrationRow"></param>
		/// <returns></returns>
		public bool Insert(RegistrationsEx.t_RegistrationRow registrationRow)
		{
			SqlParameter[] parameters={
										  new SqlParameter(PARAM_PatientID,registrationRow[RegistrationsEx.t_RegistrationDataTableEx.FIELD_PatientID]),
										  new SqlParameter(PARAM_RegistrationTypeID,registrationRow[RegistrationsEx.t_RegistrationDataTableEx.FIELD_RegistrationTypeID]),
										  new SqlParameter(PARAM_DeparptmentID,registrationRow[RegistrationsEx.t_RegistrationDataTableEx.FIELD_DeparptmentID]),
										  new SqlParameter(PARAM_RegistrarID,registrationRow[RegistrationsEx.t_RegistrationDataTableEx.FIELD_RegistrarID]),
										  new SqlParameter(PARAM_DoctorID,registrationRow[RegistrationsEx.t_RegistrationDataTableEx.FIELD_DoctorID]),
										  new SqlParameter(PARAM_Status,registrationRow[RegistrationsEx.t_RegistrationDataTableEx.FIELD_Status])										  
									  };
			int Result;
			using(SqlConnection conn=HISDBConfig.GetConnection())
			{
				Result=SqlHelper.ExecuteNonQuery(conn,"ut_RegistrationInsert",parameters);
			}
			return (Result>0);
		}


		/// <summary>
		/// 根据PatientID和DoctorID查找挂号信息
		/// </summary>
		/// <param name="patientID"></param>
		/// <param name="doctorID"></param>
		/// <returns></returns>
		public RegistrationsEx FindRegistration(int patientID,int doctorID)
		{
			SqlParameter[] parameters={
										  new SqlParameter(PARAM_PatientID,patientID),
										  new SqlParameter(PARAM_DoctorID,doctorID)
									  };
			RegistrationsEx registrationsEx=new RegistrationsEx();
			using(SqlConnection conn=HISDBConfig.GetConnection())
			{
				SqlHelper.FillDataset(conn,CommandType.StoredProcedure,"ut_FindRegistrationByPatientIDAndDoctorID",registrationsEx,new string[] {RegistrationsEx.t_RegistrationDataTableEx.TableName_Registration}, parameters);
			}
			return registrationsEx;
		}

		internal bool UseRegistration(long registrationID,SqlTransaction tran)
		{
			SqlParameter parameter= new SqlParameter(PARAM_RegistrationID,registrationID);
			SqlCommand comm=new SqlCommand();
			
			comm.Connection=tran.Connection;
    		comm.Transaction=tran;
			comm.CommandType=CommandType.StoredProcedure;
			comm.CommandText="ut_UseRegistration";
			comm.Parameters.Add(parameter);	
			return (comm.ExecuteNonQuery()>0);
		}
	}	
}
