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
{
    /// <summary>
	/// 数据访问类:T_Call_OutTask
	/// </summary>
	public partial class T_Call_OutTask
    {
        public T_Call_OutTask()
        { }
        #region  BasicMethod


        /// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CallCenterApi.Model.T_Call_OutTask model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_OutTask(");
            strSql.Append("F_TaskName,F_TaskRemark,F_PagerID,F_IsStart,F_StartTime,F_EndTime,F_IsAllot,F_AllotUserCode,F_AllotTime,F_CreateUserCode,F_CreateTime,F_DeleteFlag)");
            strSql.Append(" values (");
            strSql.Append("@F_TaskName,@F_TaskRemark,@F_PagerID,@F_IsStart,@F_StartTime,@F_EndTime,@F_IsAllot,@F_AllotUserCode,@F_AllotTime,@F_CreateUserCode,@F_CreateTime,@F_DeleteFlag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_TaskRemark", SqlDbType.NText),
                    new SqlParameter("@F_PagerID", SqlDbType.Int,4),
                    new SqlParameter("@F_IsStart", SqlDbType.Int,4),
                    new SqlParameter("@F_StartTime", SqlDbType.DateTime),
                    new SqlParameter("@F_EndTime", SqlDbType.DateTime),
                    new SqlParameter("@F_IsAllot", SqlDbType.Int,4),
                    new SqlParameter("@F_AllotUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AllotTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4)};
            parameters[0].Value = model.F_TaskName;
            parameters[1].Value = model.F_TaskRemark;
            parameters[2].Value = model.F_PagerID;
            parameters[3].Value = model.F_IsStart;
            parameters[4].Value = model.F_StartTime;
            parameters[5].Value = model.F_EndTime;
            parameters[6].Value = model.F_IsAllot;
            parameters[7].Value = model.F_AllotUserCode;
            parameters[8].Value = model.F_AllotTime;
            parameters[9].Value = model.F_CreateUserCode;
            parameters[10].Value = model.F_CreateTime;
            parameters[11].Value = model.F_DeleteFlag;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_Call_OutTask model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_OutTask set ");
            strSql.Append("F_TaskName=@F_TaskName,");
            strSql.Append("F_TaskRemark=@F_TaskRemark,");
            strSql.Append("F_PagerID=@F_PagerID,");
            strSql.Append("F_IsStart=@F_IsStart,");
            strSql.Append("F_StartTime=@F_StartTime,");
            strSql.Append("F_EndTime=@F_EndTime,");
            strSql.Append("F_IsAllot=@F_IsAllot,");
            strSql.Append("F_AllotUserCode=@F_AllotUserCode,");
            strSql.Append("F_AllotTime=@F_AllotTime,");
            strSql.Append("F_CreateUserCode=@F_CreateUserCode,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag");
            strSql.Append(" where F_TaskID=@F_TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_TaskRemark", SqlDbType.NText),
                    new SqlParameter("@F_PagerID", SqlDbType.Int,4),
                    new SqlParameter("@F_IsStart", SqlDbType.Int,4),
                    new SqlParameter("@F_StartTime", SqlDbType.DateTime),
                    new SqlParameter("@F_EndTime", SqlDbType.DateTime),
                    new SqlParameter("@F_IsAllot", SqlDbType.Int,4),
                    new SqlParameter("@F_AllotUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AllotTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_TaskID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_TaskName;
            parameters[1].Value = model.F_TaskRemark;
            parameters[2].Value = model.F_PagerID;
            parameters[3].Value = model.F_IsStart;
            parameters[4].Value = model.F_StartTime;
            parameters[5].Value = model.F_EndTime;
            parameters[6].Value = model.F_IsAllot;
            parameters[7].Value = model.F_AllotUserCode;
            parameters[8].Value = model.F_AllotTime;
            parameters[9].Value = model.F_CreateUserCode;
            parameters[10].Value = model.F_CreateTime;
            parameters[11].Value = model.F_DeleteFlag;
            parameters[12].Value = model.F_TaskID;

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

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

            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Call_OutTask ");
            strSql.Append("update T_Call_OutTask set F_DeleteFlag=1 ");
            strSql.Append(" where F_TaskID=@F_TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_TaskID;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_Call_OutTask GetModel(int F_TaskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Call_OutTask ");
            strSql.Append(" where F_TaskID=@F_TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_TaskID;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_Call_OutTask DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Call_OutTask model = new CallCenterApi.Model.T_Call_OutTask();
            if (row != null)
            {
                if (row["F_TaskID"] != null && row["F_TaskID"].ToString() != "")
                {
                    model.F_TaskID = int.Parse(row["F_TaskID"].ToString());
                }
                if (row["F_TaskName"] != null)
                {
                    model.F_TaskName = row["F_TaskName"].ToString();
                }
                if (row["F_TaskRemark"] != null)
                {
                    model.F_TaskRemark = row["F_TaskRemark"].ToString();
                }
                if (row["F_PagerID"] != null && row["F_PagerID"].ToString() != "")
                {
                    model.F_PagerID = int.Parse(row["F_PagerID"].ToString());
                }
                if (row["F_IsStart"] != null && row["F_IsStart"].ToString() != "")
                {
                    model.F_IsStart = int.Parse(row["F_IsStart"].ToString());
                }
                if (row["F_StartTime"] != null && row["F_StartTime"].ToString() != "")
                {
                    model.F_StartTime = DateTime.Parse(row["F_StartTime"].ToString());
                }
                if (row["F_EndTime"] != null && row["F_EndTime"].ToString() != "")
                {
                    model.F_EndTime = DateTime.Parse(row["F_EndTime"].ToString());
                }
                if (row["F_IsAllot"] != null && row["F_IsAllot"].ToString() != "")
                {
                    model.F_IsAllot = int.Parse(row["F_IsAllot"].ToString());
                }
                if (row["F_AllotUserCode"] != null)
                {
                    model.F_AllotUserCode = row["F_AllotUserCode"].ToString();
                }
                if (row["F_AllotTime"] != null && row["F_AllotTime"].ToString() != "")
                {
                    model.F_AllotTime = DateTime.Parse(row["F_AllotTime"].ToString());
                }
                if (row["F_CreateUserCode"] != null)
                {
                    model.F_CreateUserCode = row["F_CreateUserCode"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_DeleteFlag"] != null && row["F_DeleteFlag"].ToString() != "")
                {
                    model.F_DeleteFlag = int.Parse(row["F_DeleteFlag"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Call_OutTask ");
            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(" * ");
            strSql.Append(" FROM T_Call_OutTask ");
            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_Call_OutTask ");
            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.F_TaskID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Call_OutTask 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());
        }


        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 更新分配状态
        /// </summary>
        public bool UpdateAllot(int taskid,int isallot,string alUCode,DateTime altime)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_OutTask set ");
            strSql.Append("F_IsAllot=@F_IsAllot,");
            strSql.Append("F_AllotUserCode=@F_AllotUserCode,");
            strSql.Append("F_AllotTime=@F_AllotTime");
            strSql.Append(" where F_TaskID=@F_TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_IsAllot", SqlDbType.Int,4),
                    new SqlParameter("@F_AllotUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_AllotTime", SqlDbType.DateTime),
                    new SqlParameter("@F_TaskID", SqlDbType.Int,4)};
            parameters[0].Value = isallot;
            parameters[1].Value = alUCode;
            parameters[2].Value = altime;
            parameters[3].Value = taskid;

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

        /// <summary>
        /// 更新启动状态
        /// </summary>
        public bool UpdateStart(int taskid,int isstart,DateTime stime,DateTime etime)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_OutTask set ");
            strSql.Append("F_IsStart=@F_IsStart,");
            strSql.Append("F_StartTime=@F_StartTime,");
            strSql.Append("F_EndTime=@F_EndTime");
            strSql.Append(" where F_TaskID=@F_TaskID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_IsStart", SqlDbType.Int,4),
                    new SqlParameter("@F_StartTime", SqlDbType.DateTime),
                    new SqlParameter("@F_EndTime", SqlDbType.DateTime),
                    new SqlParameter("@F_TaskID", SqlDbType.Int,4)};
            parameters[0].Value = isstart;
            parameters[1].Value = stime;
            parameters[2].Value = etime;
            parameters[3].Value = taskid;

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