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_Wo_QuestionManage
	/// </summary>
	public partial class T_Wo_QuestionManage
    {
        public T_Wo_QuestionManage()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Wo_QuestionManage");
            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(CallCenterApi.Model.T_Wo_QuestionManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wo_QuestionManage(");
            strSql.Append("F_QuestionName,F_QuestionContent,F_ParentId,F_Label,F_DealMethods,F_Type,F_Remark,F_CreateBy,F_CreateOn,F_UpdateCount,F_UpdateBy,F_UpdateOn,F_IsDelete,F_DeleteBy,F_DeleteOn)");
            strSql.Append(" values (");
            strSql.Append("@F_QuestionName,@F_QuestionContent,@F_ParentId,@F_Label,@F_DealMethods,@F_Type,@F_Remark,@F_CreateBy,@F_CreateOn,@F_UpdateCount,@F_UpdateBy,@F_UpdateOn,@F_IsDelete,@F_DeleteBy,@F_DeleteOn)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_QuestionName", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_QuestionContent", SqlDbType.NText),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_Label", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_DealMethods", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Type", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_UpdateCount", SqlDbType.Int,4),
                    new SqlParameter("@F_UpdateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_UpdateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime)};
            parameters[0].Value = model.F_QuestionName;
            parameters[1].Value = model.F_QuestionContent;
            parameters[2].Value = model.F_ParentId;
            parameters[3].Value = model.F_Label;
            parameters[4].Value = model.F_DealMethods;
            parameters[5].Value = model.F_Type;
            parameters[6].Value = model.F_Remark;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateOn;
            parameters[9].Value = model.F_UpdateCount;
            parameters[10].Value = model.F_UpdateBy;
            parameters[11].Value = model.F_UpdateOn;
            parameters[12].Value = model.F_IsDelete;
            parameters[13].Value = model.F_DeleteBy;
            parameters[14].Value = model.F_DeleteOn;

            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_Wo_QuestionManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wo_QuestionManage set ");
            strSql.Append("F_QuestionName=@F_QuestionName,");
            strSql.Append("F_QuestionContent=@F_QuestionContent,");
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_Label=@F_Label,");
            strSql.Append("F_DealMethods=@F_DealMethods,");
            strSql.Append("F_Type=@F_Type,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            strSql.Append("F_UpdateCount=@F_UpdateCount,");
            strSql.Append("F_UpdateBy=@F_UpdateBy,");
            strSql.Append("F_UpdateOn=@F_UpdateOn,");
            strSql.Append("F_IsDelete=@F_IsDelete,");
            strSql.Append("F_DeleteBy=@F_DeleteBy,");
            strSql.Append("F_DeleteOn=@F_DeleteOn");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_QuestionName", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_QuestionContent", SqlDbType.NText),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_Label", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_DealMethods", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Type", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_UpdateCount", SqlDbType.Int,4),
                    new SqlParameter("@F_UpdateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_UpdateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.F_QuestionName;
            parameters[1].Value = model.F_QuestionContent;
            parameters[2].Value = model.F_ParentId;
            parameters[3].Value = model.F_Label;
            parameters[4].Value = model.F_DealMethods;
            parameters[5].Value = model.F_Type;
            parameters[6].Value = model.F_Remark;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateOn;
            parameters[9].Value = model.F_UpdateCount;
            parameters[10].Value = model.F_UpdateBy;
            parameters[11].Value = model.F_UpdateOn;
            parameters[12].Value = model.F_IsDelete;
            parameters[13].Value = model.F_DeleteBy;
            parameters[14].Value = model.F_DeleteOn;
            parameters[15].Value = model.F_Id;

            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_Wo_QuestionManage ");
            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_Wo_QuestionManage ");
            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 CallCenterApi.Model.T_Wo_QuestionManage GetModel(string F_QuestionName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Wo_QuestionManage ");
            strSql.Append(" where F_QuestionName=@F_QuestionName");
            SqlParameter[] parameters = {
                      new SqlParameter("@F_QuestionName",  SqlDbType.NVarChar,500)        
        };
            parameters[0].Value = F_QuestionName;

            CallCenterApi.Model.T_Wo_QuestionManage model = new CallCenterApi.Model.T_Wo_QuestionManage();
            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_Wo_QuestionManage GetModel(int F_Id)
        {

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

            CallCenterApi.Model.T_Wo_QuestionManage model = new CallCenterApi.Model.T_Wo_QuestionManage();
            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_Wo_QuestionManage DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Wo_QuestionManage model = new CallCenterApi.Model.T_Wo_QuestionManage();
            if (row != null)
            {
                if (row["F_Id"] != null && row["F_Id"].ToString() != "")
                {
                    model.F_Id = int.Parse(row["F_Id"].ToString());
                }
                if (row["F_QuestionName"] != null)
                {
                    model.F_QuestionName = row["F_QuestionName"].ToString();
                }
                if (row["F_QuestionContent"] != null)
                {
                    model.F_QuestionContent = row["F_QuestionContent"].ToString();
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_Label"] != null)
                {
                    model.F_Label = row["F_Label"].ToString();
                }
                if (row["F_DealMethods"] != null)
                {
                    model.F_DealMethods = row["F_DealMethods"].ToString();
                }
                if (row["F_Type"] != null)
                {
                    model.F_Type = row["F_Type"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_CreateBy"] != null)
                {
                    model.F_CreateBy = row["F_CreateBy"].ToString();
                }
                if (row["F_CreateOn"] != null && row["F_CreateOn"].ToString() != "")
                {
                    model.F_CreateOn = DateTime.Parse(row["F_CreateOn"].ToString());
                }
                if (row["F_UpdateCount"] != null && row["F_UpdateCount"].ToString() != "")
                {
                    model.F_UpdateCount = int.Parse(row["F_UpdateCount"].ToString());
                }
                if (row["F_UpdateBy"] != null)
                {
                    model.F_UpdateBy = row["F_UpdateBy"].ToString();
                }
                if (row["F_UpdateOn"] != null && row["F_UpdateOn"].ToString() != "")
                {
                    model.F_UpdateOn = DateTime.Parse(row["F_UpdateOn"].ToString());
                }
                if (row["F_IsDelete"] != null && row["F_IsDelete"].ToString() != "")
                {
                    model.F_IsDelete = int.Parse(row["F_IsDelete"].ToString());
                }
                if (row["F_DeleteBy"] != null)
                {
                    model.F_DeleteBy = row["F_DeleteBy"].ToString();
                }
                if (row["F_DeleteOn"] != null && row["F_DeleteOn"].ToString() != "")
                {
                    model.F_DeleteOn = DateTime.Parse(row["F_DeleteOn"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
                strSql.Append(" FROM T_Wo_QuestionManage ");
            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_Wo_QuestionManage ");
            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_Wo_QuestionManage ");
            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_Wo_QuestionManage 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
    }
}
