using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CallCenterApi.DB;
namespace CallCenterApi.DAL
{
    /// <summary>
    /// 数据访问类:T_CTI_Task
    /// </summary>
    public partial class T_CTI_Task
    {
        public T_CTI_Task()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long TaskID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CTI_Task");
            strSql.Append(" where TaskID=@TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskID", SqlDbType.BigInt)
            };
            parameters[0].Value = TaskID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Model.T_CTI_Task model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CTI_Task(");
            strSql.Append("TaskName,State,AddTime,y_YJCount,y_HMCount,Pre,Concurrency,ConcurrencyType,PlanStartDate,PlanEndDate,StartTime1,EndTime1,StartTime2,EndTime2,TaskCategoryId,TaskContent,IsExcuteNow,LoopTime)");
            strSql.Append(" values (");
            strSql.Append("@TaskName,@State,@AddTime,@y_YJCount,@y_HMCount,@Pre,@Concurrency,@ConcurrencyType,@PlanStartDate,@PlanEndDate,@StartTime1,@EndTime1,@StartTime2,@EndTime2,@TaskCategoryId,@TaskContent,@IsExcuteNow,@LoopTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskName", SqlDbType.VarChar,200),
                    new SqlParameter("@State", SqlDbType.Int,4),
                    new SqlParameter("@AddTime", SqlDbType.DateTime),
                    new SqlParameter("@y_YJCount", SqlDbType.Int,4),
                    new SqlParameter("@y_HMCount", SqlDbType.Int,4),
                    new SqlParameter("@Pre", SqlDbType.VarChar,300),
                    new SqlParameter("@Concurrency", SqlDbType.Int),
                    new SqlParameter("@ConcurrencyType", SqlDbType.Int),
                    new SqlParameter("@StartTime1", SqlDbType.DateTime),
                    new SqlParameter("@EndTime1", SqlDbType.DateTime),
                    new SqlParameter("@StartTime2", SqlDbType.DateTime),
                    new SqlParameter("@EndTime2", SqlDbType.DateTime),
                    new SqlParameter("@TaskCategoryId", SqlDbType.Int),
                    new SqlParameter("@TaskContent", SqlDbType.NVarChar,500),
                    new SqlParameter("@IsExcuteNow", SqlDbType.Bit),
                    new SqlParameter("@LoopTime", SqlDbType.Int),
                    new SqlParameter("@PlanStartDate",SqlDbType.DateTime),
                    new SqlParameter("@PlanEndDate",SqlDbType.DateTime)
        };
            parameters[0].Value = model.TaskName;
            parameters[1].Value = model.State;
            parameters[2].Value = model.AddTime;
            parameters[3].Value = model.y_YJCount;
            parameters[4].Value = model.y_HMCount;
            parameters[5].Value = model.Pre;
            parameters[6].Value = model.Concurrency;
            parameters[7].Value = model.ConcurrencyType;
            parameters[8].Value = model.StartTime1;
            parameters[9].Value = model.EndTime1;
            parameters[10].Value = model.StartTime2;
            parameters[11].Value = model.EndTime2;
            parameters[12].Value = model.TaskCategoryId;
            parameters[13].Value = model.TaskContent;
            parameters[14].Value = model.IsExcuteNow;
            parameters[15].Value = model.LoopTime;
            parameters[16].Value = model.PlanStartDate;
            parameters[17].Value = model.PlanEndDate;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_CTI_Task model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_Task set ");
            strSql.Append("TaskName=@TaskName,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("State=@State,");
            strSql.Append("y_YJCount=@y_YJCount,");
            strSql.Append("y_HMCount=@y_HMCount,");
            strSql.Append("Pre=@Pre, ");
            strSql.Append("Concurrency=@Concurrency, ");
            strSql.Append("ConcurrencyType=@ConcurrencyType, ");
            strSql.Append("PlanStartDate=@PlanStartDate, ");
            strSql.Append("PlanEndDate=@PlanEndDate, ");
            strSql.Append("StartTime1=@StartTime1, ");
            strSql.Append("EndTime1=@EndTime1, ");
            strSql.Append("StartTime2=@StartTime2, ");
            strSql.Append("EndTime2=@EndTime2, ");
            strSql.Append("TaskCategoryId=@TaskCategoryId, ");
            strSql.Append("TaskContent=@TaskContent, ");
            strSql.Append("IsExcuteNow=@IsExcuteNow, ");
            strSql.Append("LoopTime=@LoopTime");
            strSql.Append(" where TaskID=@TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskName", SqlDbType.VarChar,200),
                    new SqlParameter("@State", SqlDbType.Int,4),
                    new SqlParameter("@AddTime", SqlDbType.DateTime),
                    new SqlParameter("@y_YJCount", SqlDbType.Int,4),
                    new SqlParameter("@y_HMCount", SqlDbType.Int,4),
                    new SqlParameter("@TaskID", SqlDbType.Int),
                    new SqlParameter("@Pre",SqlDbType.VarChar,300),
                    new SqlParameter("@Concurrency", SqlDbType.Int),
                    new SqlParameter("@ConcurrencyType", SqlDbType.Int),
                    new SqlParameter("@StartTime1", SqlDbType.DateTime),
                    new SqlParameter("@EndTime1", SqlDbType.DateTime),
                    new SqlParameter("@StartTime2", SqlDbType.DateTime),
                    new SqlParameter("@EndTime2", SqlDbType.DateTime),
                    new SqlParameter("@TaskCategoryId", SqlDbType.Int),
                    new SqlParameter("@TaskContent", SqlDbType.NVarChar,500),
                    new SqlParameter("@IsExcuteNow", SqlDbType.Bit),
                    new SqlParameter("@LoopTime", SqlDbType.Int),
                    new SqlParameter("@PlanStartDate",SqlDbType.DateTime),
                    new SqlParameter("@PlanEndDate",SqlDbType.DateTime)
            };
            parameters[0].Value = model.TaskName;
            parameters[1].Value = model.State;
            parameters[2].Value = model.AddTime;
            parameters[3].Value = model.y_YJCount;
            parameters[4].Value = model.y_HMCount;
            parameters[5].Value = model.TaskID;
            parameters[6].Value = model.Pre;
            parameters[7].Value = model.Concurrency;
            parameters[8].Value = model.ConcurrencyType;
            parameters[9].Value = model.StartTime1;
            parameters[10].Value = model.EndTime1;
            parameters[11].Value = model.StartTime2;
            parameters[12].Value = model.EndTime2;
            parameters[13].Value = model.TaskCategoryId;
            parameters[14].Value = model.TaskContent;
            parameters[15].Value = model.IsExcuteNow;
            parameters[16].Value = model.LoopTime;
            parameters[17].Value = model.PlanStartDate;
            parameters[18].Value = model.PlanEndDate;

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

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateState(int id, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_Task set ");
            strSql.Append("State=@State");
            strSql.Append(" where TaskID=@TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@State", SqlDbType.Int,4),
                    new SqlParameter("@TaskID", SqlDbType.BigInt,8)};
            parameters[0].Value = state;
            parameters[1].Value = id;

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

        /// <summary>
        /// 更新多条数据
        /// </summary>
        public bool UpdateStateBatch(string where, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CTI_Task set ");
            strSql.Append("State=@State");
            if (!string.IsNullOrWhiteSpace(where))
            {
                strSql.Append(where);
            }

            SqlParameter[] parameters = {
                    new SqlParameter("@State",state) };
            parameters[0].Value = state;

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

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long TaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CTI_Task ");
            strSql.Append(" where TaskID=@TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskID", SqlDbType.BigInt)
            };
            parameters[0].Value = TaskID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string TaskIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CTI_Task ");
            strSql.Append(" where TaskID in (" + TaskIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_CTI_Task GetModel(long TaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 TaskID,TaskName,State,AddTime,y_YJCount,y_HMCount,Pre,Concurrency,ConcurrencyType,PlanStartDate,PlanEndDate,StartTime1,EndTime1,StartTime2,EndTime2,TaskCategoryId,TaskContent,IsExcuteNow,LoopTime from T_CTI_Task ");
            strSql.Append(" where State>=0 and  TaskID=@TaskID ");
            SqlParameter[] parameters = {
                    new SqlParameter("@TaskID", SqlDbType.BigInt)
            };
            parameters[0].Value = TaskID;

            CallCenterApi.Model.T_CTI_Task model = new CallCenterApi.Model.T_CTI_Task();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TaskID"] != null && ds.Tables[0].Rows[0]["TaskID"].ToString() != "")
                {
                    model.TaskID = long.Parse(ds.Tables[0].Rows[0]["TaskID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TaskName"] != null && ds.Tables[0].Rows[0]["TaskName"].ToString() != "")
                {
                    model.TaskName = ds.Tables[0].Rows[0]["TaskName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["State"] != null && ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["y_YJCount"] != null && ds.Tables[0].Rows[0]["y_YJCount"].ToString() != "")
                {
                    model.y_YJCount = int.Parse(ds.Tables[0].Rows[0]["y_YJCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["y_HMCount"] != null && ds.Tables[0].Rows[0]["y_HMCount"].ToString() != "")
                {
                    model.y_HMCount = int.Parse(ds.Tables[0].Rows[0]["y_HMCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre"] != null && ds.Tables[0].Rows[0]["Pre"].ToString() != "")
                {
                    model.Pre = ds.Tables[0].Rows[0]["Pre"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Concurrency"] != null && ds.Tables[0].Rows[0]["Concurrency"].ToString() != "")
                {
                    model.Concurrency = float.Parse(ds.Tables[0].Rows[0]["Concurrency"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ConcurrencyType"] != null && ds.Tables[0].Rows[0]["ConcurrencyType"].ToString() != "")
                {
                    model.ConcurrencyType = int.Parse(ds.Tables[0].Rows[0]["ConcurrencyType"].ToString());
                }

                model.PlanStartDate = ds.Tables[0].Rows[0]["PlanStartDate"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["PlanStartDate"]);
                model.PlanEndDate = ds.Tables[0].Rows[0]["PlanEndDate"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["PlanEndDate"]);
                model.StartTime1 = ds.Tables[0].Rows[0]["StartTime1"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["StartTime1"]);
                model.EndTime1 = ds.Tables[0].Rows[0]["EndTime1"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["EndTime1"]);
                model.StartTime2 = ds.Tables[0].Rows[0]["StartTime2"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["StartTime2"]);
                model.EndTime2 = ds.Tables[0].Rows[0]["EndTime2"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(ds.Tables[0].Rows[0]["EndTime2"]);
                model.TaskCategoryId = ds.Tables[0].Rows[0]["TaskCategoryId"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["TaskCategoryId"]);
                model.TaskContent = ds.Tables[0].Rows[0]["TaskContent"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["TaskContent"].ToString();
                model.IsExcuteNow = ds.Tables[0].Rows[0]["IsExcuteNow"] == DBNull.Value ? false : Convert.ToBoolean(ds.Tables[0].Rows[0]["IsExcuteNow"]);
                model.LoopTime = ds.Tables[0].Rows[0]["LoopTime"] == DBNull.Value ? 0 : Convert.ToInt16(ds.Tables[0].Rows[0]["LoopTime"]);

                return model;
            }
            else
            {
                return null;
            }
        }

        public DataSet GetTask(int groupId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ");
            strSql.Append(" FROM vw_CTI_TaskPager ");

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM vw_CTI_TaskPager ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetTaskList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_CTI_Task ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top, string strWhere, string filedOrder)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select ");
        //    if (Top > 0)
        //    {
        //        strSql.Append(" top " + Top.ToString());
        //    }
        //    strSql.Append(" TaskID,TaskName,TrunkGroupID,CallerNum,CallType,ExInfo,MaxTrunkCount,IntensityCoefficient,ItemTableName,State,AddTime,y_PSort,y_TkModelId,y_SXH,y_FPCount,y_YJCount,y_HCCount,y_HSCount,y_HTCount,y_HMCount,y_OkCount,y_RFCount,y_ConsCount,y_InvlCount,y_NoAnswerCount,y_ShutDownCount,TaskType,Pre,Speed ");
        //    strSql.Append(" FROM T_CTI_Task ");
        //    if (strWhere.Trim() != "")
        //    {
        //        strSql.Append(" where " + strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CTI_Task ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.TaskID desc");
            }
            strSql.Append(")AS Row, T.*  from T_CTI_Task T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_CTI_Task";
            parameters[1].Value = "TaskID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

