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_Sys_CallForwarding
    {
        public int Add(Model.T_Sys_CallForwarding model)
        {
            string sql = " insert into  T_Sys_CallForwarding (UserId,StartTime,EndTime) values (@UserId,@StartTime,@EndTime)";
            return DbHelperSQL.ExecuteSql(sql, new SqlParameter[] {
                new SqlParameter("@UserId",model.UserId),
                new SqlParameter("@StartTime",model.StartTime),
                new SqlParameter("@EndTime",model.EndTime),
            });
        }

        public bool delete(int userId)
        {
            string sql = "delete  from  T_Sys_CallForwarding where userId=@userId";
            return DbHelperSQL.ExecuteSql(sql, new SqlParameter("@userId", userId)) > 0;
        }

        public List<Model.T_Sys_CallForwarding> GetList(int userId)
        {
            string sql = "select Id,UserID,StartTime,EndTime from T_Sys_CallForwarding where userId=@userId";
            var dt = DbHelperSQL.Query(sql, new SqlParameter("@userId", userId)).Tables[0];
            List<Model.T_Sys_CallForwarding> list = new List<Model.T_Sys_CallForwarding>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    list.Add(new Model.T_Sys_CallForwarding
                    {
                        Id = row["Id"] == DBNull.Value ? 0 : Convert.ToInt32(row["Id"]),
                        UserId = row["UserId"] == DBNull.Value ? 0 : Convert.ToInt32(row["UserId"]),
                        StartTime = row["StartTime"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["StartTime"]),
                        EndTime = row["EndTime"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(row["EndTime"])
                    });
                }
            }
            return list;
        }
    }
}
