using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CallCenterApi.Model;

namespace CallCenterApi.DAL
{
    public class T_CTI_TaskAgent
    {
        public T_CTI_TaskAgent() { }
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CallCenterApi.Model.T_CTI_TaskAgent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CTI_TaskAgent(");
            strSql.Append("taskId,AgentId,GroupId)");
            strSql.Append(" values (");
            strSql.Append(" @taskId,@AgentId,@GroupId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@taskId",model.TaskId),
                new SqlParameter("@AgentId",model.AgentId),
                new SqlParameter("@GroupId",model.GroupId)
            };
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_CTI_TaskAgent ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public Model.T_CTI_TaskAgent GetModel(int taskId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,TaskId,AgentId,GroupId from T_CTI_TaskAgent ");
            strSql.Append(" where TaskId=@TaskId ");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskId",taskId)
            };

            CallCenterApi.Model.T_CTI_TaskAgent model = new CallCenterApi.Model.T_CTI_TaskAgent();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public bool Update(Model.T_CTI_TaskAgent model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_TaskAgent set ");
            strSql.Append(" AgentId = @AgentId, ");
            strSql.Append(" GroupId = @GroupId ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@AgentId",model.AgentId),
                new SqlParameter("@GroupId",model.GroupId),
                new SqlParameter("@Id",model.Id)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_CTI_TaskAgent DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_CTI_TaskAgent model = new CallCenterApi.Model.T_CTI_TaskAgent();
            if (row != null)
            {
                model = new CallCenterApi.Model.T_CTI_TaskAgent();
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = long.Parse(row["Id"].ToString());
                }
                if (row["TaskID"] != null && row["TaskID"].ToString() != "")
                {
                    model.TaskId = long.Parse(row["TaskID"].ToString());
                }
                if (row["AgentId"] != null && row["AgentId"].ToString() != "")
                {
                    model.AgentId = row["AgentId"].ToString();
                }
                if (row["GroupId"] != null && row["GroupId"].ToString() != "")
                {
                    model.GroupId = row["GroupId"].ToString();
                }
            }
            return model;
        }

        public void BatchAdd(string tableName, DataTable dt)
        {
            DbHelperSQL.SqlBulkCopyByDatatable(tableName, dt);
        }
        #endregion  Method
    }
}
