using System;
using System.Text;
using Medivators.BusinessObjects;
using Medivators.Common;
using Medivators.Common.Logging;

namespace Medivators.Data.Queries
{
	public class SelectLogRecords : AsynchronousExecute
	{
		private string baseSQL = " select LR.* " +
								 " from LogRecords LR";

		public void SelectByDates(DateTime StartDate, DateTime EndDate)
		{
			string sql = baseSQL +
						 " where  Timestamp >= " + Constants.DateDelimiter + StartDate + Constants.DateDelimiter +
						 " and    Timestamp <= " + Constants.DateDelimiter + EndDate + Constants.DateDelimiter;

			base.Select(sql, new LogRecordDataMapper());
		}
		public void SelectByKey(string LogRecordKey)
		{
			string sql = baseSQL +
						 " where  LR.[Key] = " + Constants.DoubleQuote + LogRecordKey + Constants.DoubleQuote;
			base.Select(sql, new LogRecordDataMapper());
		}
		public void SelectByCycleReport(string CycleReportKey)
		{
			string sql = baseSQL +
						 " where LR.[Key] in (" + 
						 " select LogRecordKey from CycleReportLogRecords where CycleReportKey = " + 
						 Constants.DoubleQuote + CycleReportKey + Constants.DoubleQuote + ")";
			base.Select(sql, new LogRecordDataMapper());
		}
		public void SelectByKeys(string[] LogRecordKeys)
		{
			string sql = string.Empty;
			foreach (string key in LogRecordKeys)
			{
				if ( ! sql.Equals(string.Empty ))
					sql += ", ";
				sql += Constants.DoubleQuote + key + Constants.DoubleQuote;
			}				
			sql = baseSQL +
				  " where  LR.[Key] in (" + sql + ")";
			base.Select(sql, new LogRecordDataMapper());
		}
		public void SelectByDateAndLogNumber(DateTime StartDate, DateTime EndDate, int StartLogNumber, int EndLogNumber)
		{
			string sql = baseSQL +
						 " where  Timestamp >= " + Constants.DateDelimiter + StartDate + Constants.DateDelimiter +
						 " and    Timestamp <= " + Constants.DateDelimiter + EndDate + Constants.DateDelimiter +
						 " and	  LogNumber >= " + StartLogNumber.ToString() +
						 " and	  LogNumber <= " + EndLogNumber.ToString() ;

			base.Select(sql, new LogRecordDataMapper());
		}
	}
}
