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_TaskVoiceTemp
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Model.T_CTI_TaskVoiceTemp model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CTI_TaskVoiceTemp(");
            strSql.Append("F_CategoryId,F_Title,F_Content,F_UpdateTime)");
            strSql.Append(" values (");
            strSql.Append("@F_CategoryId,@F_Title,@F_Content,@F_UpdateTime)");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), new SqlParameter[] {
                new SqlParameter("@F_CategoryId",model.F_CategoryId),
                new SqlParameter("@F_Title",model.F_Title),
                new SqlParameter("@F_Content",model.F_Content),
                new SqlParameter("@F_UpdateTime",model.F_UpdateTime)
            });
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_CTI_TaskVoiceTemp model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_TaskVoiceTemp set ");
            strSql.Append(" F_CategoryId=@F_CategoryId, ");
            strSql.Append(" F_Title=@F_Title, ");
            strSql.Append(" F_Content=@F_Content, ");
            strSql.Append(" F_UpdateTime=@F_UpdateTime ");
            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_CategoryId",model.F_CategoryId),
                new SqlParameter("@F_Title",model.F_Title),
                new SqlParameter("@F_Content",model.F_Content),
                new SqlParameter("@F_UpdateTime",model.F_UpdateTime)
            });
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除多条数据
        /// </summary>
        public bool DeleteBatch(string ids)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CTI_TaskVoiceTemp ");
            strSql.Append(" where F_Id in(" + ids + ")");

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 T1.F_Id,T1.F_CategoryId,T1.F_Title,T1.F_Content,T1.F_UpdateTime,T2.F_Name AS F_CategoryName from T_CTI_TaskVoiceTemp T1 left join T_CTI_TaskCategory AS T2 on  T1.F_CategoryId =T2.F_Id ");
            strSql.Append(" where T1.F_Id=@F_Id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", TaskID)
            };
            CallCenterApi.Model.T_CTI_TaskVoiceTemp model = new CallCenterApi.Model.T_CTI_TaskVoiceTemp();
            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_CategoryId = Convert.ToInt32(ds.Tables[0].Rows[0]["F_CategoryId"]);
                model.F_Title = ds.Tables[0].Rows[0]["F_Title"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["F_Title"].ToString();
                model.F_Content = ds.Tables[0].Rows[0]["F_Content"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["F_Content"].ToString();
                model.F_UpdateTime = ds.Tables[0].Rows[0]["F_UpdateTime"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["F_UpdateTime"].ToString());
                model.F_CategoryName = ds.Tables[0].Rows[0]["F_CategoryName"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["F_CategoryName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select  T1.F_Id,T1.F_CategoryId,T1.F_Title,T1.F_Content,T1.F_UpdateTime,T2.F_Name AS F_CategoryName from T_CTI_TaskVoiceTemp T1 left join T_CTI_TaskCategory AS T2 on  T1.F_CategoryId =T2.F_Id  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
