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_Vis_TelBZ
	/// </summary>
	public partial class T_Vis_TelBZ
    {
        public T_Vis_TelBZ()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Vis_TelBZ model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Vis_TelBZ(");
            strSql.Append("F_Sheng,F_Store,F_Leader,F_Score,F_Telid)");
            strSql.Append(" values (");
            strSql.Append("@F_Sheng,@F_Store,@F_Leader,@F_Score,@F_Telid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Sheng", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Store", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Leader", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Score", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Telid", SqlDbType.Int,4)};
            parameters[0].Value = model.F_Sheng;
            parameters[1].Value = model.F_Store;
            parameters[2].Value = model.F_Leader;
            parameters[3].Value = model.F_Score;
            parameters[4].Value = model.F_Telid;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_ID,F_Sheng,F_Store,F_Leader,F_Score,F_Telid ");
            strSql.Append(" FROM T_Vis_TelBZ ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.T_Vis_TelBZ DataRowToModel(DataRow row)
        {
            Model.T_Vis_TelBZ model = new Model.T_Vis_TelBZ();
            if (row != null)
            {
                if (row["F_ID"] != null && row["F_ID"].ToString() != "")
                {
                    model.F_ID = int.Parse(row["F_ID"].ToString());
                }
                if (row["F_Sheng"] != null)
                {
                    model.F_Sheng = row["F_Sheng"].ToString();
                }
                if (row["F_Store"] != null)
                {
                    model.F_Store = row["F_Store"].ToString();
                }
                if (row["F_Leader"] != null)
                {
                    model.F_Leader = row["F_Leader"].ToString();
                }
                if (row["F_Score"] != null)
                {
                    model.F_Score = row["F_Score"].ToString();
                }
                if (row["F_Telid"] != null && row["F_Telid"].ToString() != "")
                {
                    model.F_Telid = int.Parse(row["F_Telid"].ToString());
                }
            }
            return model;
        }

        /// <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_ID,F_Sheng,F_Store,F_Leader,F_Score,F_Telid ");
            strSql.Append(" FROM T_Vis_TelBZ ");
            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_Vis_TelBZ ");
            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_Vis_TelBZ 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
    }
}
