using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HySoft.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace HySoft.BaseCallCenter.DAL
{
    /// <summary>
    /// 数据访问类:T_Sys_ModuleInfo
    /// </summary>
    public partial class T_Sys_ModuleInfo
    {
        public T_Sys_ModuleInfo()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_ModuleId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Sys_ModuleInfo");
            strSql.Append(" where F_ModuleId=@F_ModuleId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_ModuleId", SqlDbType.Int,4)
			};
            parameters[0].Value = F_ModuleId;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_ModuleInfo(");
            strSql.Append("F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl)");
            strSql.Append(" values (");
            strSql.Append("@F_ModuleName,@F_ModuleCode,@F_Remark,@F_Sort,@F_StateFlag,@F_OptUrl,@F_ImgUrl)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@F_ModuleName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ModuleCode", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_StateFlag", SqlDbType.Int,4),
					new SqlParameter("@F_OptUrl", SqlDbType.VarChar,200),
					new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200)};
            parameters[0].Value = model.F_ModuleName;
            parameters[1].Value = model.F_ModuleCode;
            parameters[2].Value = model.F_Remark;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_StateFlag;
            parameters[5].Value = model.F_OptUrl;
            parameters[6].Value = model.F_ImgUrl;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_ModuleInfo set ");
            strSql.Append("F_ModuleName=@F_ModuleName,");
            strSql.Append("F_ModuleCode=@F_ModuleCode,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_StateFlag=@F_StateFlag,");
            strSql.Append("F_OptUrl=@F_OptUrl,");
            strSql.Append("F_ImgUrl=@F_ImgUrl");
            strSql.Append(" where F_ModuleId=@F_ModuleId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_ModuleName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ModuleCode", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_StateFlag", SqlDbType.Int,4),
					new SqlParameter("@F_OptUrl", SqlDbType.VarChar,200),
					new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
					new SqlParameter("@F_ModuleId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ModuleName;
            parameters[1].Value = model.F_ModuleCode;
            parameters[2].Value = model.F_Remark;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_StateFlag;
            parameters[5].Value = model.F_OptUrl;
            parameters[6].Value = model.F_ImgUrl;
            parameters[7].Value = model.F_ModuleId;

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

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_ModuleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Sys_ModuleInfo ");
            strSql.Append(" where F_ModuleId=@F_ModuleId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_ModuleId", SqlDbType.Int,4)
			};
            parameters[0].Value = F_ModuleId;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo GetModel(int F_ModuleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ModuleId,F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl from T_Sys_ModuleInfo ");
            strSql.Append(" where F_ModuleId=@F_ModuleId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_ModuleId", SqlDbType.Int,4)
			};
            parameters[0].Value = F_ModuleId;

            HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo model = new HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo DataRowToModel(DataRow row)
        {
            HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo model = new HySoft.BaseCallCenter.Model.T_Sys_ModuleInfo();
            if (row != null)
            {
                if (row["F_ModuleId"] != null && row["F_ModuleId"].ToString() != "")
                {
                    model.F_ModuleId = int.Parse(row["F_ModuleId"].ToString());
                }
                if (row["F_ModuleName"] != null)
                {
                    model.F_ModuleName = row["F_ModuleName"].ToString();
                }
                if (row["F_ModuleCode"] != null)
                {
                    model.F_ModuleCode = row["F_ModuleCode"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                if (row["F_StateFlag"] != null && row["F_StateFlag"].ToString() != "")
                {
                    model.F_StateFlag = int.Parse(row["F_StateFlag"].ToString());
                }
                if (row["F_OptUrl"] != null)
                {
                    model.F_OptUrl = row["F_OptUrl"].ToString();
                }
                if (row["F_ImgUrl"] != null)
                {
                    model.F_ImgUrl = row["F_ImgUrl"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_ModuleId,F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl ");
            strSql.Append(" FROM T_Sys_ModuleInfo ");
            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_ModuleId,F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl ");
            strSql.Append(" FROM T_Sys_ModuleInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Sys_ModuleInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.F_ModuleId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Sys_ModuleInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

      

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
