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

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

namespace HISClinic.DataServices
{
	/// <summary>
	/// PatientMapper 的摘要说明。
	/// </summary>
	public class PatientMapper
	{
		private const string PARAM_PatientID="@PatientID";
		private const string PARAM_Name="@Name";
		private const string PARAM_Sex="@Sex";
		private const string PARAM_BirthDate="@BirthDate";
		private const string PARAM_Telephone="@Telephone";
		private const string PARAM_Mobile="@Mobile";
		private const string PARAM_Address="@Address";
		private const string PARAM_CardNumber="@CardNumber";

		public PatientMapper()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		/// <summary>
		/// 根据医疗卡号获取病人的信息
		/// </summary>
		/// <param name="CardNumber">医疗卡的号码</param>
		/// <returns></returns>
		public PatientsEx FindPatientByCardNumber(string cardNumber)
		{
			PatientsEx patientsEx=new PatientsEx();
			SqlParameter[] parameters={new SqlParameter(PARAM_CardNumber,cardNumber)};
			using(SqlConnection conn=HISDBConfig.GetConnection())
			{
				SqlHelper.FillDataset(conn,CommandType.StoredProcedure,"ut_FindPatientByCardNumber",patientsEx,
					new string[] {PatientsEx.t_PatientDataTableEx.TableName_Patient},parameters);
			}
			return patientsEx;
		}
		
		/// <summary>
		/// 向数据库增加病人信息
		/// </summary>
		/// <param name="patientRow"></param>
		/// <returns>判断是否成功执行</returns>
		public bool Insert(PatientsEx.t_PatientRow patientRow)
		{
			SqlParameter[] parameters={
										  new SqlParameter(PARAM_Name,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Name]),
										  new SqlParameter(PARAM_Sex,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Sex]),
										  new SqlParameter(PARAM_BirthDate,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_BirthDate]),
										  new SqlParameter(PARAM_Telephone,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Telephone]),
										  new SqlParameter(PARAM_Mobile,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Mobile]),
										  new SqlParameter(PARAM_Address,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Address]),
										  new SqlParameter(PARAM_CardNumber,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_CardNumber])
									  };			
			int Result;
			using(SqlConnection conn=HISDBConfig.GetConnection())
			{
				Result=SqlHelper.ExecuteNonQuery(conn,"ut_PatientInsert",parameters);
			}
			return (Result>0);
		}

		/// <summary>
		/// 向数据库保存修改的病人信息
		/// </summary>
		/// <param name="patientRow"></param>
		/// <returns>判断是否成功执行</returns>
		public bool Update(PatientsEx.t_PatientRow patientRow)
		{
			SqlParameter[] parameters={   
										  new SqlParameter(PARAM_PatientID,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_PatientID]),
										  new SqlParameter(PARAM_Name,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Name]),
										  new SqlParameter(PARAM_Sex,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Sex]),
										  new SqlParameter(PARAM_BirthDate,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_BirthDate]),
										  new SqlParameter(PARAM_Telephone,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Telephone]),
										  new SqlParameter(PARAM_Mobile,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Mobile]),
										  new SqlParameter(PARAM_Address,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_Address]),
										  new SqlParameter(PARAM_CardNumber,patientRow[PatientsEx.t_PatientDataTableEx.FIELD_CardNumber])
									  };
			int Result;
			using(SqlConnection conn=HISDBConfig.GetConnection())
			{
				Result=SqlHelper.ExecuteNonQuery(conn,"ut_PatientUpdate",parameters);
			}
			return (Result>0);
		}
	}
}
