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_TaskRecord
    {
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CallCenterApi.Model.T_CTI_TaskRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CTI_TaskRecord(");
            strSql.Append("Name,RecordTime,RecordPath,Remark)");
            strSql.Append(" values (");
            strSql.Append(" @Name,@RecordTime,@RecordPath,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@Name",model.Name),
                new SqlParameter("@RecordTime",model.RecordTime),
                new SqlParameter("@RecordPath",model.RecordPath),
                new SqlParameter("@Remark",model.Remark)
            };
            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_TaskRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public Model.T_CTI_TaskRecord GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,RecordTime,RecordPath,Remark from T_CTI_TaskRecord ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@Id",id)
            };

            CallCenterApi.Model.T_CTI_TaskRecord model = new CallCenterApi.Model.T_CTI_TaskRecord();
            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_TaskRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_TaskAgent set ");
            strSql.Append(" Name = @Name, ");
            strSql.Append(" RecordTime = @RecordTime ");
            strSql.Append(" RecordPath = @RecordPath, ");
            strSql.Append(" Remark = @Remark ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@Name",model.Name),
                new SqlParameter("@RecordTime",model.RecordTime),
                new SqlParameter("@RecordPath",model.RecordPath),
                new SqlParameter("@Remark",model.Remark),
                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_TaskRecord DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_CTI_TaskRecord model = new CallCenterApi.Model.T_CTI_TaskRecord();
            if (row != null)
            {
                model = new CallCenterApi.Model.T_CTI_TaskRecord();
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Name"] != null && row["Name"].ToString() != "")
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["RecordTime"] != null && row["RecordTime"].ToString() != "")
                {
                    model.RecordTime = int.Parse(row["RecordTime"].ToString());
                }
                if (row["RecordPath"] != null && row["RecordPath"].ToString() != "")
                {
                    model.RecordPath = row["RecordPath"].ToString();
                }
                if (row["Remark"] != null && row["Remark"].ToString() != "")
                {
                    model.Remark = row["Remark"].ToString();
                }
            }
            return model;
        }

       
        #endregion  Method
    }
}
