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

namespace SK.MemberDAL
{
	/// <summary>
	/// Message 的摘要说明。
	/// </summary>
	public class Message
	{
		public Message()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		public void Add(object Subject,object Body,object SendMemberID,object ReceiveMemberID)
		{
			SqlParameter[] oParms = new SqlParameter[4];
			oParms[0] = new SqlParameter("@Subject",Subject);
			oParms[1] = new SqlParameter("@Body",Body);
			oParms[2] = new SqlParameter("@SendMemberID",SendMemberID);
			oParms[3] = new SqlParameter("@ReceiveMemberID",ReceiveMemberID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"insert into Message(Subject,Body,SendMemberID,ReceiveMemberID,SendTime,ReadTime,Readed) values(@Subject,@Body,@SendMemberID,@ReceiveMemberID,getdate(),null,0)",oParms);
		}
		public void Read(object MessageID)
		{
			SqlParameter[] oParms = new SqlParameter[1];
			oParms[0] = new SqlParameter("@MessageID",MessageID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"update Message set Readed=1 where MessageID=@MessageID",oParms);
		}
		public void Delete(object MessageID)
		{
			SqlParameter[] oParms = new SqlParameter[1];
			oParms[0] = new SqlParameter("@MessageID",MessageID);

			SqlHelper.ExecuteNonQuery(Data.ConnString,CommandType.Text,"delete Message where MessageID=@MessageID",oParms);
		}
		public DataSet List(object ReceiveMemberID,object PageSize,object PageIndex)
		{
			string spName;
			SqlParameter[] oParms;
			spName = "Message_List";
			oParms = SqlHelperParameterCache.GetSpParameterSet(Data.ConnString, spName);
			oParms[0].Value=ReceiveMemberID;
			oParms[1].Value=PageSize;
			oParms[2].Value=PageIndex;
			oParms[3].Value=0;
			
			return SqlHelper.ExecuteDataset(Data.ConnString, spName, oParms);
		}
		public int List_Count(object ReceiveMemberID)
		{
			string spName;
			SqlParameter[] oParms;
			spName = "Message_List";
			oParms = SqlHelperParameterCache.GetSpParameterSet(Data.ConnString, spName);
			oParms[0].Value=ReceiveMemberID;
			oParms[1].Value=0;
			oParms[2].Value=0;
			oParms[3].Value=1;
			
			return Convert.ToInt32(SqlHelper.ExecuteScalar(Data.ConnString, spName, oParms));
		}
		public DataRow Info(object MessageID)
		{
			SqlParameter[] oParms = new SqlParameter[1];
			oParms[0] = new SqlParameter("@MessageID",MessageID);

			using(DataSet ds = SqlHelper.ExecuteDataset(Data.ConnString,CommandType.Text,"select a.*,b.MemberName as SendMemberName from Message a LEFT JOIN Member b ON a.SendMemberID=b.MemberID where MessageID=@MessageID",oParms))
			{
				if(ds.Tables[0].Rows.Count>0)
					return ds.Tables[0].Rows[0];
				else
					return null;
			}
		}
		public DataSet PM_Info(object MessageID)
		{
			SqlParameter[] oParms = new SqlParameter[1];
			oParms[0] = new SqlParameter("@MessageID",MessageID);

			return SqlHelper.ExecuteDataset(Data.ConnString,CommandType.Text,"select a.*,b.MemberName as SendMemberName from Message a LEFT JOIN Member b ON a.SendMemberID=b.MemberID where MessageID=@MessageID",oParms);

		}
		public int PM_NoRead_Count(object ReceiveMemberID)
		{
			SqlParameter[] oParms = new SqlParameter[1];
			oParms[0] = new SqlParameter("@ReceiveMemberID",ReceiveMemberID);

			return Convert.ToInt32(SqlHelper.ExecuteScalar(Data.ConnString,CommandType.Text,"select count(MessageID) from Message where ReceiveMemberID=@ReceiveMemberID and Readed=0",oParms));
		}
	}
}
