using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.T_Call_OutAnswersWX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_OutAnswersWX(");
            strSql.Append("F_PagerID,F_QID,F_QIID,F_Answer,F_CusTelID,F_CusID,F_OptBy,F_OptByOpenid,F_OptOn,F_Expand1,F_Expand2,F_Expand3)");
            strSql.Append(" values (");
            strSql.Append("@F_PagerID,@F_QID,@F_QIID,@F_Answer,@F_CusTelID,@F_CusID,@F_OptBy,@F_OptByOpenid,@F_OptOn,@F_Expand1,@F_Expand2,@F_Expand3)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_PagerID", SqlDbType.Int,4),
                    new SqlParameter("@F_QID", SqlDbType.Int,4),
                    new SqlParameter("@F_QIID", SqlDbType.Int,4),
                    new SqlParameter("@F_Answer", SqlDbType.NVarChar,3000),
                    new SqlParameter("@F_CusTelID", SqlDbType.Int,4),
                    new SqlParameter("@F_CusID", SqlDbType.Int,4),
                    new SqlParameter("@F_OptBy", SqlDbType.BigInt,8),
                    new SqlParameter("@F_OptByOpenid", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_OptOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Expand1", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Expand2", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Expand3", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.F_PagerID;
            parameters[1].Value = model.F_QID;
            parameters[2].Value = model.F_QIID;
            parameters[3].Value = model.F_Answer;
            parameters[4].Value = model.F_CusTelID;
            parameters[5].Value = model.F_CusID;
            parameters[6].Value = model.F_OptBy;
            parameters[7].Value = model.F_OptByOpenid;
            parameters[8].Value = model.F_OptOn;
            parameters[9].Value = model.F_Expand1;
            parameters[10].Value = model.F_Expand2;
            parameters[11].Value = model.F_Expand3;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_Call_OutAnswersWX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_OutAnswersWX set ");
            strSql.Append("F_PagerID=@F_PagerID,");
            strSql.Append("F_QID=@F_QID,");
            strSql.Append("F_QIID=@F_QIID,");
            strSql.Append("F_Answer=@F_Answer,");
            strSql.Append("F_CusTelID=@F_CusTelID,");
            strSql.Append("F_CusID=@F_CusID,");
            strSql.Append("F_OptBy=@F_OptBy,");
            strSql.Append("F_OptByOpenid=@F_OptByOpenid,");
            strSql.Append("F_OptOn=@F_OptOn,");
            strSql.Append("F_Expand1=@F_Expand1,");
            strSql.Append("F_Expand2=@F_Expand2,");
            strSql.Append("F_Expand3=@F_Expand3");
            strSql.Append(" where F_AnswerID=@F_AnswerID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_PagerID", SqlDbType.Int,4),
                    new SqlParameter("@F_QID", SqlDbType.Int,4),
                    new SqlParameter("@F_QIID", SqlDbType.Int,4),
                    new SqlParameter("@F_Answer", SqlDbType.NVarChar,3000),
                    new SqlParameter("@F_CusTelID", SqlDbType.Int,4),
                    new SqlParameter("@F_CusID", SqlDbType.Int,4),
                    new SqlParameter("@F_OptBy", SqlDbType.BigInt,8),
                    new SqlParameter("@F_OptByOpenid", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_OptOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Expand1", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Expand2", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Expand3", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_AnswerID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_PagerID;
            parameters[1].Value = model.F_QID;
            parameters[2].Value = model.F_QIID;
            parameters[3].Value = model.F_Answer;
            parameters[4].Value = model.F_CusTelID;
            parameters[5].Value = model.F_CusID;
            parameters[6].Value = model.F_OptBy;
            parameters[7].Value = model.F_OptByOpenid;
            parameters[8].Value = model.F_OptOn;
            parameters[9].Value = model.F_Expand1;
            parameters[10].Value = model.F_Expand2;
            parameters[11].Value = model.F_Expand3;
            parameters[12].Value = model.F_AnswerID;

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

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

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

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_Call_OutAnswersWX GetModel(int F_AnswerID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Call_OutAnswersWX ");
            strSql.Append(" where F_AnswerID=@F_AnswerID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_AnswerID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_AnswerID;

            CallCenterApi.Model.T_Call_OutAnswersWX model = new CallCenterApi.Model.T_Call_OutAnswersWX();
            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 CallCenterApi.Model.T_Call_OutAnswersWX DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Call_OutAnswersWX model = new CallCenterApi.Model.T_Call_OutAnswersWX();
            if (row != null)
            {
                if (row["F_AnswerID"] != null && row["F_AnswerID"].ToString() != "")
                {
                    model.F_AnswerID = int.Parse(row["F_AnswerID"].ToString());
                }
                if (row["F_PagerID"] != null && row["F_PagerID"].ToString() != "")
                {
                    model.F_PagerID = int.Parse(row["F_PagerID"].ToString());
                }
                if (row["F_QID"] != null && row["F_QID"].ToString() != "")
                {
                    model.F_QID = int.Parse(row["F_QID"].ToString());
                }
                if (row["F_QIID"] != null && row["F_QIID"].ToString() != "")
                {
                    model.F_QIID = int.Parse(row["F_QIID"].ToString());
                }
                if (row["F_Answer"] != null)
                {
                    model.F_Answer = row["F_Answer"].ToString();
                }
                if (row["F_CusTelID"] != null && row["F_CusTelID"].ToString() != "")
                {
                    model.F_CusTelID = int.Parse(row["F_CusTelID"].ToString());
                }
                if (row["F_CusID"] != null && row["F_CusID"].ToString() != "")
                {
                    model.F_CusID = int.Parse(row["F_CusID"].ToString());
                }
                if (row["F_OptBy"] != null && row["F_OptBy"].ToString() != "")
                {
                    model.F_OptBy = long.Parse(row["F_OptBy"].ToString());
                }
                if (row["F_OptByOpenid"] != null)
                {
                    model.F_OptByOpenid = row["F_OptByOpenid"].ToString();
                }
                if (row["F_OptOn"] != null && row["F_OptOn"].ToString() != "")
                {
                    model.F_OptOn = DateTime.Parse(row["F_OptOn"].ToString());
                }
                if (row["F_Expand1"] != null)
                {
                    model.F_Expand1 = row["F_Expand1"].ToString();
                }
                if (row["F_Expand2"] != null)
                {
                    model.F_Expand2 = row["F_Expand2"].ToString();
                }
                if (row["F_Expand3"] != null)
                {
                    model.F_Expand3 = row["F_Expand3"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Call_OutAnswersWX ");
            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(" * ");
            strSql.Append(" FROM T_Call_OutAnswersWX ");
            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_Call_OutAnswersWX ");
            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_AnswerID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Call_OutAnswersWX 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
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByTelid(int telid,string dateflag)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Call_OutAnswers ");
            strSql.Append(" where F_CusTelID=@F_CusTelID ");
            strSql.Append(" and F_Expand3=@F_Expand3 ");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_CusTelID", SqlDbType.Int,4),
                    new SqlParameter("@F_Expand3", SqlDbType.Int,4)
            };
            parameters[0].Value = telid;
            parameters[1].Value = dateflag;

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