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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.T_Bus_DemandsType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Bus_DemandsType(");
            strSql.Append("F_Code,F_Name,F_ParentId,F_ParentCode,F_layer,F_Sort,F_Remark,F_CreateBy,F_CreateOn,F_IsDelete,F_DeleteBy,F_DeleteOn)");
            strSql.Append(" values (");
            strSql.Append("@F_Code,@F_Name,@F_ParentId,@F_ParentCode,@F_layer,@F_Sort,@F_Remark,@F_CreateBy,@F_CreateOn,@F_IsDelete,@F_DeleteBy,@F_DeleteOn)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Code", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentCode", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_layer", SqlDbType.Int,4),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateOn", 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_Code;
            parameters[1].Value = model.F_Name;
            parameters[2].Value = model.F_ParentId;
            parameters[3].Value = model.F_ParentCode;
            parameters[4].Value = model.F_layer;
            parameters[5].Value = model.F_Sort;
            parameters[6].Value = model.F_Remark;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateOn;
            parameters[9].Value = model.F_IsDelete;
            parameters[10].Value = model.F_DeleteBy;
            parameters[11].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_Bus_DemandsType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Bus_DemandsType set ");
            strSql.Append("F_Code=@F_Code,");
            strSql.Append("F_Name=@F_Name,");
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_ParentCode=@F_ParentCode,");
            strSql.Append("F_layer=@F_layer,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            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_Code", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Name", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentCode", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_layer", SqlDbType.Int,4),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateOn", 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_Code;
            parameters[1].Value = model.F_Name;
            parameters[2].Value = model.F_ParentId;
            parameters[3].Value = model.F_ParentCode;
            parameters[4].Value = model.F_layer;
            parameters[5].Value = model.F_Sort;
            parameters[6].Value = model.F_Remark;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateOn;
            parameters[9].Value = model.F_IsDelete;
            parameters[10].Value = model.F_DeleteBy;
            parameters[11].Value = model.F_DeleteOn;
            parameters[12].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, string usercode)
        {

            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Bus_DemandsType ");
            strSql.Append("update T_Bus_DemandsType set ");
            strSql.Append("F_IsDelete=1,");
            strSql.Append("F_DeleteBy=@F_DeleteBy,");
            strSql.Append("F_DeleteOn=getdate() ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50)
            };
            parameters[0].Value = F_Id;
            parameters[1].Value = usercode;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_Idlist, string usercode)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Bus_DemandsType ");
            strSql.Append("update T_Bus_DemandsType set ");
            strSql.Append("F_IsDelete=1,");
            strSql.Append("F_DeleteBy='"+ usercode + "',");
            strSql.Append("F_DeleteOn=getdate() ");
            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_Bus_DemandsType GetModel(int F_Id)
        {

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

            CallCenterApi.Model.T_Bus_DemandsType model = new CallCenterApi.Model.T_Bus_DemandsType();
            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_Bus_DemandsType DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Bus_DemandsType model = new CallCenterApi.Model.T_Bus_DemandsType();
            if (row != null)
            {
                if (row["F_Id"] != null && row["F_Id"].ToString() != "")
                {
                    model.F_Id = int.Parse(row["F_Id"].ToString());
                }
                if (row["F_Code"] != null)
                {
                    model.F_Code = row["F_Code"].ToString();
                }
                if (row["F_Name"] != null)
                {
                    model.F_Name = row["F_Name"].ToString();
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_ParentCode"] != null)
                {
                    model.F_ParentCode = row["F_ParentCode"].ToString();
                }
                if (row["F_layer"] != null && row["F_layer"].ToString() != "")
                {
                    model.F_layer = int.Parse(row["F_layer"].ToString());
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].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_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_Bus_DemandsType ");
            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_Bus_DemandsType ");
            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_Bus_DemandsType ");
            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_Bus_DemandsType 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
    }
}
