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_TaskTelNum
    /// </summary>
    public partial class T_Call_TaskTelNum
    {
        public T_Call_TaskTelNum()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Call_TaskTelNum");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.T_Call_TaskTelNum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Call_TaskTelNum(");
            strSql.Append("F_TaskId,F_Phone,F_YJState,F_HCState,F_HCCount,DTMF,WorkOrderId,AssignedId)");
            strSql.Append(" values (");
            strSql.Append("@F_TaskId,@F_Phone,@F_YJState,@F_HCState,@F_HCCount,@DTMF,@WorkOrderId,@AssignedId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskId", SqlDbType.Int,4),
                    new SqlParameter("@F_Phone", SqlDbType.VarChar,200),
                    new SqlParameter("@F_YJState", SqlDbType.Int,4),
                    new SqlParameter("@F_HCState", SqlDbType.Int,4),
                    new SqlParameter("@F_HCCount", SqlDbType.Int,4),
                    new SqlParameter("@DTMF", SqlDbType.Int,4),
                    new SqlParameter("@WorkOrderId",SqlDbType.VarChar,200),
                    new SqlParameter("@AssignedId",SqlDbType.Int)
            };


            parameters[0].Value = model.F_TaskId;
            parameters[1].Value = model.F_Phone;
            parameters[2].Value = model.F_YJState;
            parameters[3].Value = model.F_HCState;
            parameters[4].Value = model.F_HCCount;
            parameters[5].Value = model.DTMF;
            parameters[6].Value = model.WorkOrderId;
            parameters[7].Value = model.AssignedId;
            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_TaskTelNum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_TaskTelNum set ");
            strSql.Append("F_TaskId=@F_TaskId,");
            strSql.Append("F_Phone=@F_Phone,");
            strSql.Append("F_YJState=@F_YJState,");
            strSql.Append("F_HCState=@F_HCState,");
            strSql.Append("F_HCCount=@F_HCCount,");
            strSql.Append("DTMF=@DTMF ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskId", SqlDbType.Int,4),
                    new SqlParameter("@F_Phone", SqlDbType.VarChar,200),
                    new SqlParameter("@F_YJState", SqlDbType.Int,4),
                    new SqlParameter("@F_HCState", SqlDbType.Int,4),
                    new SqlParameter("@F_HCCount", SqlDbType.Int,4),
                    new SqlParameter("@DTMF", SqlDbType.Int,4),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.F_TaskId;
            parameters[1].Value = model.F_Phone;
            parameters[2].Value = model.F_YJState;
            parameters[3].Value = model.F_HCState;
            parameters[4].Value = model.F_HCCount;
            parameters[5].Value = model.DTMF;
            parameters[6].Value = model.F_Id;

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

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateFP(int id, int userid, string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_TaskTelNum set ");
            strSql.Append("F_FPState=1,");
            strSql.Append("F_UserId=@F_UserId,");
            strSql.Append("F_UserName=@F_UserName");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_UserId", SqlDbType.Int,4),
                    new SqlParameter("@F_UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};

            parameters[0].Value = userid;
            parameters[1].Value = username;
            parameters[2].Value = id;

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

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateHJJG(int id, int resid, string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_TaskTelNum set ");
            strSql.Append("F_HJJGId=@F_HJJGId,");
            strSql.Append("F_HJJGName=@F_HJJGName");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_HJJGId", SqlDbType.Int,4),
                    new SqlParameter("@F_HJJGName", SqlDbType.VarChar,500),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};

            parameters[0].Value = resid;
            parameters[1].Value = name;
            parameters[2].Value = id;

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

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateQXFP(string arrid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_TaskTelNum set ");
            strSql.Append("F_FPState=0,");
            strSql.Append("F_UserId=0,");
            strSql.Append("F_UserName=''");
            strSql.Append(" where F_Id in (" + arrid + ")");

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

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateHC(int id, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_TaskTelNum set ");
            strSql.Append("F_HCState=@F_HCState");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_HCState", SqlDbType.Int,4),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};

            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 UpdateYJ(int id, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Call_TaskTelNum set ");
            strSql.Append("F_YJState=@F_YJState");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_YJState", SqlDbType.Int,4),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};

            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 UpdateAsk(CallCenterApi.Model.T_Call_TaskTelNum model)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("update T_Call_TaskTelNum set ");
        //    strSql.Append("F_AskInfo=@F_AskInfo,");
        //    strSql.Append("F_AskRes=@F_AskRes");
        //    strSql.Append(" where F_Id=@F_Id");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@F_AskInfo", SqlDbType.Text),
        //            new SqlParameter("@F_AskRes", SqlDbType.Text),
        //            new SqlParameter("@F_Id", SqlDbType.Int,4)};

        //    parameters[0].Value = model.F_AskInfo;
        //    parameters[1].Value = model.F_AskRes;
        //    parameters[2].Value = model.F_Id;

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

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Call_TaskTelNum ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

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

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int ClearPhone(int F_Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Call_TaskTelNum ");
            strSql.Append(" where F_TaskId=@F_TaskId and F_FPState=0 ");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }

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

        /// <summary>
        /// 删除指定任务下所有号码
        /// </summary>
        public int DeletePhoneByTaskId(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Call_TaskTelNum ");
            strSql.Append(" where F_TaskId=@F_TaskId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TaskId", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }

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

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

            CallCenterApi.Model.T_Call_TaskTelNum model = new CallCenterApi.Model.T_Call_TaskTelNum();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.F_Id = ds.Tables[0].Rows[0]["F_Id"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["F_Id"]);
                model.F_TaskId = ds.Tables[0].Rows[0]["F_TaskId"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["F_TaskId"]);
                model.F_Phone = ds.Tables[0].Rows[0]["F_Phone"] == DBNull.Value ? "" : ds.Tables[0].Rows[0]["F_Phone"].ToString();
                model.F_YJState = ds.Tables[0].Rows[0]["F_YjState"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["F_YjState"]);
                model.F_HCState = ds.Tables[0].Rows[0]["F_HCState"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["F_HCState"]);
                model.F_HCCount = ds.Tables[0].Rows[0]["F_HCCount"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["F_HCCount"]);
                model.DTMF = ds.Tables[0].Rows[0]["DTMF"] == DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["DTMF"]);

                return model;
            }
            else
            {
                return null;
            }
        }

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

        public DataSet GetPhoneList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_Phone ");
            strSql.Append(" FROM T_Call_TaskTelNum ");
            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(" F_Id,F_TaskId,F_Phone,F_Customer,F_PhoneBy,F_Sex,F_FPState,F_YJState,F_HCState,F_UserId,F_UserName,F_CreateTime,ExpandIntField1,ExpandIntField2,ExpandIntField3,ExpandIntField4,ExpandIntField5,ExpandIntField6,ExpandDecField1,ExpandDecField2,ExpandDecField3,ExpandDecField4,ExpandDecField5,ExpandDecField6,ExpandDatField1,ExpandDatField2,ExpandDatField3,ExpandDatField4,ExpandDatField5,ExpandDatField6,ExpandVchField1,ExpandVchField2,ExpandVchField3,ExpandVchField4,ExpandVchField5,ExpandVchField6,ExpandVchField7,ExpandVchField8,ExpandVchField9,ExpandVchField10,ExpandVchField11,ExpandVchField12,ExpandVchField13,ExpandVchField14,ExpandVchField15,ExpandSintField1,ExpandSintField2,ExpandSintField3,ExpandSintField4,ExpandSintField5 ");
            strSql.Append(" FROM T_Call_TaskTelNum ");
            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_TaskTelNum ");
            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_Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_Call_TaskTelNum 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>
        /// <returns></returns>
        public DataTable GetTableDesign()
        {
            return DbHelperSQL.Query("SET FMTONLY ON;SELECT * FROM T_Call_TaskTelNum ;SET FMTONLY OFF; ").Tables[0];
        }

        /// <summary>
        /// 大量数据导入SqlBulkCopy
        /// </summary>
        /// <returns></returns>
        public void SqlBulkCopy(DataTable dt)
        {
            DbHelperSQL.SqlBulkCopyByDatatable("T_Call_TaskTelNum", dt);
        }


        /*
        /// <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_Call_TaskTelNum";
            parameters[1].Value = "F_Id";
            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
    }
}
