using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CallCenterApi.DB;
namespace CallCenterApi.DAL
{
	/// <summary>
	/// 数据访问类:T_Sys_Function
	/// </summary>
	public partial class T_Sys_Function
	{
		public T_Sys_Function()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("F_FunctionId", "T_Sys_Function"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int F_FunctionId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_Sys_Function");
			strSql.Append(" where F_FunctionId=@F_FunctionId ");
			SqlParameter[] parameters = {
					new SqlParameter("@F_FunctionId", SqlDbType.Int,4)};
			parameters[0].Value = F_FunctionId;

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

        /// <summary>
        /// 由id返回code
        /// </summary>
        public string GetCodeById(int functionId)
        {
            string code = "";
            string sql = "select F_FunctionCode from T_Sys_Function where F_FunctionId=" + functionId;
            DataSet ds = DbHelperSQL.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["F_FunctionCode"] != null && ds.Tables[0].Rows[0]["F_FunctionCode"].ToString() != "")
                {
                    code = ds.Tables[0].Rows[0]["F_FunctionCode"].ToString();
                }
            }
            return code;
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CallCenterApi.Model.T_Sys_Function model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_Sys_Function(");
			strSql.Append("F_FunctionName,F_FunctionCode,F_ParentId,F_State,F_Layer,F_flag,F_Sort,F_OptUrl,F_ImgUrl,F_Remark)");
			strSql.Append(" values (");
			strSql.Append("@F_FunctionName,@F_FunctionCode,@F_ParentId,@F_State,@F_Layer,@F_flag,@F_Sort,@F_OptUrl,@F_ImgUrl,@F_Remark)");
            strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@F_FunctionId", SqlDbType.Int,4),
					new SqlParameter("@F_FunctionName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FunctionCode", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ParentId", SqlDbType.Int,4),
					new SqlParameter("@F_State", SqlDbType.Int,4),
					new SqlParameter("@F_Layer", SqlDbType.Int,4),
					new SqlParameter("@F_flag", SqlDbType.Int,4),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_OptUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@F_ImgUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@F_Remark", SqlDbType.NVarChar,200)};
			parameters[0].Value = model.F_FunctionId;
			parameters[1].Value = model.F_FunctionName;
			parameters[2].Value = model.F_FunctionCode;
			parameters[3].Value = model.F_ParentId;
			parameters[4].Value = model.F_State;
			parameters[5].Value = model.F_Layer;
			parameters[6].Value = model.F_flag;
			parameters[7].Value = model.F_Sort;
			parameters[8].Value = model.F_OptUrl;
			parameters[9].Value = model.F_ImgUrl;
			parameters[10].Value = model.F_Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
			//return DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(CallCenterApi.Model.T_Sys_Function model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_Sys_Function set ");
			strSql.Append("F_FunctionName=@F_FunctionName,");
			strSql.Append("F_FunctionCode=@F_FunctionCode,");
			strSql.Append("F_ParentId=@F_ParentId,");
			strSql.Append("F_State=@F_State,");
			strSql.Append("F_Layer=@F_Layer,");
			strSql.Append("F_flag=@F_flag,");
			strSql.Append("F_Sort=@F_Sort,");
			strSql.Append("F_OptUrl=@F_OptUrl,");
			strSql.Append("F_ImgUrl=@F_ImgUrl,");
			strSql.Append("F_Remark=@F_Remark");
			strSql.Append(" where F_FunctionId=@F_FunctionId ");
			SqlParameter[] parameters = {
					new SqlParameter("@F_FunctionName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FunctionCode", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ParentId", SqlDbType.Int,4),
					new SqlParameter("@F_State", SqlDbType.Int,4),
					new SqlParameter("@F_Layer", SqlDbType.Int,4),
					new SqlParameter("@F_flag", SqlDbType.Int,4),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_OptUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@F_ImgUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
					new SqlParameter("@F_FunctionId", SqlDbType.Int,4)};
			parameters[0].Value = model.F_FunctionName;
			parameters[1].Value = model.F_FunctionCode;
			parameters[2].Value = model.F_ParentId;
			parameters[3].Value = model.F_State;
			parameters[4].Value = model.F_Layer;
			parameters[5].Value = model.F_flag;
			parameters[6].Value = model.F_Sort;
			parameters[7].Value = model.F_OptUrl;
			parameters[8].Value = model.F_ImgUrl;
			parameters[9].Value = model.F_Remark;
			parameters[10].Value = model.F_FunctionId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int F_FunctionId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Sys_Function ");
			strSql.Append(" where F_FunctionId=@F_FunctionId ");
			SqlParameter[] parameters = {
					new SqlParameter("@F_FunctionId", SqlDbType.Int,4)};
			parameters[0].Value = F_FunctionId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string F_FunctionIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Sys_Function ");
			strSql.Append(" where F_FunctionId in ("+F_FunctionIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CallCenterApi.Model.T_Sys_Function GetModel(int F_FunctionId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 F_FunctionId,F_FunctionName,F_FunctionCode,F_ParentId,F_State,F_Layer,F_flag,F_Sort,F_OptUrl,F_ImgUrl,F_Remark from T_Sys_Function ");
			strSql.Append(" where F_FunctionId=@F_FunctionId ");
			SqlParameter[] parameters = {
					new SqlParameter("@F_FunctionId", SqlDbType.Int,4)};
			parameters[0].Value = F_FunctionId;

			CallCenterApi.Model.T_Sys_Function model=new CallCenterApi.Model.T_Sys_Function();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["F_FunctionId"]!=null && ds.Tables[0].Rows[0]["F_FunctionId"].ToString()!="")
				{
					model.F_FunctionId=int.Parse(ds.Tables[0].Rows[0]["F_FunctionId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_FunctionName"]!=null && ds.Tables[0].Rows[0]["F_FunctionName"].ToString()!="")
				{
					model.F_FunctionName=ds.Tables[0].Rows[0]["F_FunctionName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_FunctionCode"]!=null && ds.Tables[0].Rows[0]["F_FunctionCode"].ToString()!="")
				{
					model.F_FunctionCode=ds.Tables[0].Rows[0]["F_FunctionCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_ParentId"]!=null && ds.Tables[0].Rows[0]["F_ParentId"].ToString()!="")
				{
					model.F_ParentId=int.Parse(ds.Tables[0].Rows[0]["F_ParentId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_State"]!=null && ds.Tables[0].Rows[0]["F_State"].ToString()!="")
				{
					model.F_State=int.Parse(ds.Tables[0].Rows[0]["F_State"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Layer"]!=null && ds.Tables[0].Rows[0]["F_Layer"].ToString()!="")
				{
					model.F_Layer=int.Parse(ds.Tables[0].Rows[0]["F_Layer"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_flag"]!=null && ds.Tables[0].Rows[0]["F_flag"].ToString()!="")
				{
					model.F_flag=int.Parse(ds.Tables[0].Rows[0]["F_flag"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Sort"]!=null && ds.Tables[0].Rows[0]["F_Sort"].ToString()!="")
				{
					model.F_Sort=int.Parse(ds.Tables[0].Rows[0]["F_Sort"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_OptUrl"]!=null && ds.Tables[0].Rows[0]["F_OptUrl"].ToString()!="")
				{
					model.F_OptUrl=ds.Tables[0].Rows[0]["F_OptUrl"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_ImgUrl"]!=null && ds.Tables[0].Rows[0]["F_ImgUrl"].ToString()!="")
				{
					model.F_ImgUrl=ds.Tables[0].Rows[0]["F_ImgUrl"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_Remark"]!=null && ds.Tables[0].Rows[0]["F_Remark"].ToString()!="")
				{
					model.F_Remark=ds.Tables[0].Rows[0]["F_Remark"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select F_FunctionId,F_FunctionName,F_FunctionCode,F_ParentId,F_State,F_Layer,F_flag,F_Sort,F_OptUrl,F_ImgUrl,F_Remark ");
			strSql.Append(" FROM T_Sys_Function ");
			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(" F_FunctionId,F_FunctionName,F_FunctionCode,F_ParentId,F_State,F_Layer,F_flag,F_Sort,F_OptUrl,F_ImgUrl,F_Remark ");
			strSql.Append(" FROM T_Sys_Function ");
			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 = "T_Sys_Function";
			parameters[1].Value = "F_FunctionId";
			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  Method
	}
}

