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
{
    public class T_CTI_TaskCategory
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Model.T_CTI_TaskCategory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CTI_TaskCategory(");
            strSql.Append("F_ParentId,F_Name)");
            strSql.Append(" values (");
            strSql.Append("@F_ParentId,@F_Name)");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), new SqlParameter[] {
                new SqlParameter("@F_ParentId",model.F_ParentId),
                new SqlParameter("@F_Name",model.F_Name)
            });
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_CTI_TaskCategory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_TaskCategory set ");
            strSql.Append(" F_ParentId=@F_ParentId, ");
            strSql.Append(" F_Name=@F_Name ");
            strSql.Append(" where F_Id=@F_Id");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), new SqlParameter[] {
                new SqlParameter("@F_Id",model.F_Id),
                new SqlParameter("@F_ParentId",model.F_ParentId),
                new SqlParameter("@F_Name",model.F_Name)
            });
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CTI_TaskCategory ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.BigInt)
            };
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_CTI_TaskCategory GetModel(long TaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 T1.F_Id,T1.F_ParentId,T1.F_Name,T2.F_Name AS F_ParentName from T_CTI_TaskCategory T1 left join T_CTI_TaskCategory AS T2 on T1.F_ParentId =T2.F_Id ");
            strSql.Append(" where T1.F_Id=@F_Id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", TaskID)
            };
            CallCenterApi.Model.T_CTI_TaskCategory model = new CallCenterApi.Model.T_CTI_TaskCategory();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.F_Id = Convert.ToInt32(ds.Tables[0].Rows[0]["F_Id"]);
                model.F_ParentId = Convert.ToInt32(ds.Tables[0].Rows[0]["F_ParentId"]);
                model.F_Name = ds.Tables[0].Rows[0]["F_Name"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["F_Name"].ToString();
                model.F_ParentName = ds.Tables[0].Rows[0]["F_ParentName"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["F_ParentName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        public DataSet GetList(string where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select T1.F_Id,T1.F_ParentId,T1.F_Name,T2.F_Name AS F_ParentName from T_CTI_TaskCategory T1 left join T_CTI_TaskCategory AS T2 on T1.F_ParentId =T2.F_Id ");
            if (!string.IsNullOrWhiteSpace(where))
            {
                sb.Append(" where ");
            }
            return DbHelperSQL.Query(sb.ToString());
        }

    }
}
