using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZXDT.DBUtility;

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZXDT.CallCenter.Model.T_Com_Tag model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Com_Tag(");
            strSql.Append("F_TagName,F_Type,F_Content,F_AddUser,F_AddUserName,F_Sort,F_AddTime,F_Rate,F_Table,F_TableId,F_About)");
            strSql.Append(" values (");
            strSql.Append("@F_TagName,@F_Type,@F_Content,@F_AddUser,@F_AddUserName,@F_Sort,@F_AddTime,@F_Rate,@F_Table,@F_TableId,@F_About)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TagName", model.F_TagName),
                    new SqlParameter("@F_Type", model.F_Type),
                    new SqlParameter("@F_Content",  model.F_Content),
                    new SqlParameter("@F_AddUser",  model.F_AddUser),
                    new SqlParameter("@F_AddUserName", model.F_AddUserName),
                    new SqlParameter("@F_Sort", model.F_Sort),
                    new SqlParameter("@F_AddTime", model.F_AddTime),
                    new SqlParameter("@F_Rate",  model.F_Rate),
                    new SqlParameter("@F_Table", model.F_Table),
                    new SqlParameter("@F_TableId", model.F_TableId),
                    new SqlParameter("@F_About", model.F_About)};


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZXDT.CallCenter.Model.T_Com_Tag model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Com_Tag set ");
            strSql.Append("F_TagName=@F_TagName,");
            strSql.Append("F_Type=@F_Type,");
            strSql.Append("F_Content=@F_Content,");
            strSql.Append("F_AddUser=@F_AddUser,");
            strSql.Append("F_AddUserName=@F_AddUserName,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_AddTime=@F_AddTime,");
            strSql.Append("F_Rate=@F_Rate,");
            strSql.Append("F_Table=@F_Table,");
            strSql.Append("F_About=@F_About,");
            strSql.Append("F_TableId=@F_TableId");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TagName", SqlDbType.VarChar,20),
                    new SqlParameter("@F_Type", SqlDbType.Int,4),
                    new SqlParameter("@F_Content", SqlDbType.VarChar,500),
                    new SqlParameter("@F_AddUser", SqlDbType.Int,4),
                    new SqlParameter("@F_AddUserName", SqlDbType.VarChar,20),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_AddTime", SqlDbType.DateTime),
                    new SqlParameter("@F_Rate", SqlDbType.Int,4),
                    new SqlParameter("@F_Table", SqlDbType.VarChar,50),
                    new SqlParameter("@F_TableId", SqlDbType.VarChar,32),
                    new SqlParameter("@F_Id", SqlDbType.Int,4),
                    new SqlParameter("@F_About", SqlDbType.VarChar, 500)};
            parameters[0].Value = model.F_TagName;
            parameters[1].Value = model.F_Type;
            parameters[2].Value = model.F_Content;
            parameters[3].Value = model.F_AddUser;
            parameters[4].Value = model.F_AddUserName;
            parameters[5].Value = model.F_Sort;
            parameters[6].Value = model.F_AddTime;
            parameters[7].Value = model.F_Rate;
            parameters[8].Value = model.F_Table;
            parameters[9].Value = model.F_TableId;
            parameters[10].Value = model.F_Id;
            parameters[11].Value = model.F_About;

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

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

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

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.T_Com_Tag GetModel(int F_Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_TagName,F_Type,F_Content,F_AddUser,F_AddUserName,F_Sort,F_AddTime,F_Id,F_Rate,F_Table,F_TableId,F_About from T_Com_Tag ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

            ZXDT.CallCenter.Model.T_Com_Tag model = new ZXDT.CallCenter.Model.T_Com_Tag();
            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 ZXDT.CallCenter.Model.T_Com_Tag DataRowToModel(DataRow row)
        {
            ZXDT.CallCenter.Model.T_Com_Tag model = new ZXDT.CallCenter.Model.T_Com_Tag();
            if (row != null)
            {
                if (row["F_TagName"] != null)
                {
                    model.F_TagName = row["F_TagName"].ToString();
                }
                if (row["F_Type"] != null && row["F_Type"].ToString() != "")
                {
                    model.F_Type = int.Parse(row["F_Type"].ToString());
                }
                if (row["F_Content"] != null)
                {
                    model.F_Content = row["F_Content"].ToString();
                }
                if (row["F_AddUser"] != null && row["F_AddUser"].ToString() != "")
                {
                    model.F_AddUser = int.Parse(row["F_AddUser"].ToString());
                }
                if (row["F_AddUserName"] != null)
                {
                    model.F_AddUserName = row["F_AddUserName"].ToString();
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                if (row["F_AddTime"] != null && row["F_AddTime"].ToString() != "")
                {
                    model.F_AddTime = DateTime.Parse(row["F_AddTime"].ToString());
                }
                if (row["F_Id"] != null && row["F_Id"].ToString() != "")
                {
                    model.F_Id = int.Parse(row["F_Id"].ToString());
                }
                if (row["F_Rate"] != null && row["F_Rate"].ToString() != "")
                {
                    model.F_Rate = int.Parse(row["F_Rate"].ToString());
                }
                if (row["F_Table"] != null)
                {
                    model.F_Table = row["F_Table"].ToString();
                }
                if (row["F_TableId"] != null)
                {
                    model.F_TableId = row["F_TableId"].ToString();
                }

                model.F_About = row["F_About"].ToMyString();

                
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_TagName,F_Type,F_Content,F_AddUser,F_AddUserName,F_Sort,F_AddTime,F_Id,F_Rate,F_Table,F_TableId ");
            strSql.Append(" FROM T_Com_Tag ");
            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_TagName,F_Type,F_Content,F_AddUser,F_AddUserName,F_Sort,F_AddTime,F_Id,F_Rate,F_Table,F_TableId ");
            strSql.Append(" FROM T_Com_Tag ");
            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_Com_Tag ");
            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_Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_Com_Tag 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());
        }

        /*
		/// <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_Com_Tag";
			parameters[1].Value = "F_Id";
			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  BasicMethod
        #region  ExtensionMethod
        public bool SetTopping(int id)
        {

            int minsort = DbHelperSQL.GetMinID("F_sort", "T_com_tag");
            string sql1 = "  Update T_com_tag set F_sort=1 where F_id ='" + id + "'";
            string sql = "  Update T_com_tag set F_sort=F_sort+1 where F_id !='" + id + "'";
            try
            {
                DbHelperSQL.ExecuteSql(sql1);
                DbHelperSQL.ExecuteSql(sql);
                return true;
            }
            catch (Exception)
            {

                return false;
            } 
        }
        #endregion  ExtensionMethod
    }
}

