using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//ÇëÏÈÌí¼ÓÒýÓÃ
namespace TSM.DAL
{
	/// <summary>
	/// Êý¾Ý·ÃÎÊÀàCK_People¡£
	/// </summary>
	public class CK_People
	{
		public CK_People()
		{}
		#region  ³ÉÔ±·½·¨

		/// <summary>
		/// µÃµ½×î´óID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("CK_PeopleID", "CK_People"); 
		}

		/// <summary>
		/// ÊÇ·ñ´æÔÚ¸Ã¼ÇÂ¼
		/// </summary>
		public bool Exists(int CK_PeopleID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from CK_People");
			strSql.Append(" where CK_PeopleID=@CK_PeopleID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CK_PeopleID", SqlDbType.Int,4)};
			parameters[0].Value = CK_PeopleID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// Ôö¼ÓÒ»ÌõÊý¾Ý
		/// </summary>
		public int Add(TSM.Model.CK_People model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into CK_People(");
			strSql.Append("CK_PeopleName,CK_PhoneNo,CK_Comment)");
			strSql.Append(" values (");
			strSql.Append("@CK_PeopleName,@CK_PhoneNo,@CK_Comment)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CK_PeopleName", SqlDbType.VarChar,32),
					new SqlParameter("@CK_PhoneNo", SqlDbType.VarChar,32),
					new SqlParameter("@CK_Comment", SqlDbType.VarChar,100)};
			parameters[0].Value = model.CK_PeopleName;
			parameters[1].Value = model.CK_PhoneNo;
			parameters[2].Value = model.CK_Comment;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 1;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// ¸üÐÂÒ»ÌõÊý¾Ý
		/// </summary>
		public void Update(TSM.Model.CK_People model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update CK_People set ");
			strSql.Append("CK_PeopleName=@CK_PeopleName,");
			strSql.Append("CK_PhoneNo=@CK_PhoneNo,");
			strSql.Append("CK_Comment=@CK_Comment");
			strSql.Append(" where CK_PeopleID=@CK_PeopleID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CK_PeopleID", SqlDbType.Int,4),
					new SqlParameter("@CK_PeopleName", SqlDbType.VarChar,32),
					new SqlParameter("@CK_PhoneNo", SqlDbType.VarChar,32),
					new SqlParameter("@CK_Comment", SqlDbType.VarChar,100)};
			parameters[0].Value = model.CK_PeopleID;
			parameters[1].Value = model.CK_PeopleName;
			parameters[2].Value = model.CK_PhoneNo;
			parameters[3].Value = model.CK_Comment;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}

		/// <summary>
		/// É¾³ýÒ»ÌõÊý¾Ý
		/// </summary>
		public void Delete(int CK_PeopleID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from CK_People ");
			strSql.Append(" where CK_PeopleID=@CK_PeopleID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CK_PeopleID", SqlDbType.Int,4)};
			parameters[0].Value = CK_PeopleID;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}


		/// <summary>
		/// µÃµ½Ò»¸ö¶ÔÏóÊµÌå
		/// </summary>
		public TSM.Model.CK_People GetModel(int CK_PeopleID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 CK_PeopleID,CK_PeopleName,CK_PhoneNo,CK_Comment from CK_People ");
			strSql.Append(" where CK_PeopleID=@CK_PeopleID ");
			SqlParameter[] parameters = {
					new SqlParameter("@CK_PeopleID", SqlDbType.Int,4)};
			parameters[0].Value = CK_PeopleID;

			TSM.Model.CK_People model=new TSM.Model.CK_People();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["CK_PeopleID"].ToString()!="")
				{
					model.CK_PeopleID=int.Parse(ds.Tables[0].Rows[0]["CK_PeopleID"].ToString());
				}
				model.CK_PeopleName=ds.Tables[0].Rows[0]["CK_PeopleName"].ToString();
				model.CK_PhoneNo=ds.Tables[0].Rows[0]["CK_PhoneNo"].ToString();
				model.CK_Comment=ds.Tables[0].Rows[0]["CK_Comment"].ToString();
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// »ñµÃÊý¾ÝÁÐ±í
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select CK_PeopleID,CK_PeopleName,CK_PhoneNo,CK_Comment ");
			strSql.Append(" FROM CK_People ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// »ñµÃÇ°¼¸ÐÐÊý¾Ý
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" CK_PeopleID,CK_PeopleName,CK_PhoneNo,CK_Comment ");
			strSql.Append(" FROM CK_People ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		
		/// <summary>
		/// ·ÖÒ³»ñÈ¡Êý¾ÝÁÐ±í
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "CK_People";
            parameters[1].Value = "CK_PeopleID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}

		#endregion  ³ÉÔ±·½·¨
	}
}

