using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using DBUtility;

namespace ZXDT.CallCenter.DAL
{
    /// <summary>
    /// 数据访问类:kf_quick_msgs
    /// </summary>
    public partial class kf_quick_msgs
    {
        public kf_quick_msgs()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMYSQL.GetMaxID("id", "kf_quick_msgs");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from kf_quick_msgs");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32,11)         };
            parameters[0].Value = id;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ZXDT.CallCenter.Model.kf_quick_msgs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into kf_quick_msgs(");
            strSql.Append("id,service_id,msg,rate)");
            strSql.Append(" values (");
            strSql.Append("@id,@service_id,@msg,@rate)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", model.id),
                    new MySqlParameter("@service_id", model.service_id),
                    new MySqlParameter("@msg",  model.msg),
                    new MySqlParameter("@rate",  model.rate)};


            int rows = DbHelperMYSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZXDT.CallCenter.Model.kf_quick_msgs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update kf_quick_msgs set ");
            strSql.Append("service_id=@service_id,");
            strSql.Append("rate=@rate,");
            strSql.Append("msg=@msg");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", model.id),
                    new MySqlParameter("@service_id", model.service_id),
                    new MySqlParameter("@msg",  model.msg),
                    new MySqlParameter("@rate",  model.rate)};

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

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from kf_quick_msgs ");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32,11)         };
            parameters[0].Value = id;

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,service_id,msg,rate from kf_quick_msgs ");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32,11)         };
            parameters[0].Value = id;

            ZXDT.CallCenter.Model.kf_quick_msgs model = new ZXDT.CallCenter.Model.kf_quick_msgs();
            DataSet ds = DbHelperMYSQL.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.kf_quick_msgs DataRowToModel(DataRow row)
        {
            ZXDT.CallCenter.Model.kf_quick_msgs model = new ZXDT.CallCenter.Model.kf_quick_msgs();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["rate"] != null && row["rate"].ToString() != "")
                {
                    model.rate = int.Parse(row["rate"].ToString());
                }
                if (row["service_id"] != null && row["service_id"].ToString() != "")
                {
                    model.service_id = int.Parse(row["service_id"].ToString());
                }
                if (row["msg"] != null)
                {
                    model.msg = row["msg"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,service_id,msg,rate ");
            strSql.Append(" FROM kf_quick_msgs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMYSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM kf_quick_msgs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMYSQL.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 pagesize)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM kf_quick_msgs ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append(" Order by " + orderby);
            }
            strSql.AppendFormat(" limit {0},{1}", startIndex, pagesize);
            return DbHelperMYSQL.Query(strSql.ToString());
        }
        #region
        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "kf_quick_msgs";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMYSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/
        #endregion
        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

