using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CallCenter.Utility;

namespace CallCenterApi.BLL
{
    /// <summary>
    /// 计划表
    /// </summary>
    public partial class T_CTI_Task
    {
        private readonly CallCenterApi.DAL.T_CTI_Task dal = new CallCenterApi.DAL.T_CTI_Task();
        private readonly CallCenterApi.DAL.T_Call_TaskTelNum telNumDal = new CallCenterApi.DAL.T_Call_TaskTelNum();
        public T_CTI_Task()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long TaskID)
        {
            return dal.Exists(TaskID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CallCenterApi.Model.T_CTI_Task model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_CTI_Task model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateState(int id, int state)
        {
            return dal.UpdateState(id, state);
        }

        /// <summary>
        /// 更新多条数据
        /// </summary>
        public bool UpdateStateBatch(string where, int state)
        {
            return dal.UpdateStateBatch(where, state);
        }

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

            return dal.Delete(TaskID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string TaskIDlist)
        {
            return dal.DeleteList(TaskIDlist);
        }

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

            return dal.GetModel(TaskID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetTaskList(string strWhere)
        {
            return dal.GetTaskList(strWhere);
        }
        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top, string strWhere, string filedOrder)
        //{
        //    return dal.GetList(Top, strWhere, filedOrder);
        //}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CallCenterApi.Model.T_CTI_Task> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CallCenterApi.Model.T_CTI_Task> DataTableToList(DataTable dt)
        {
            List<CallCenterApi.Model.T_CTI_Task> modelList = new List<CallCenterApi.Model.T_CTI_Task>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CallCenterApi.Model.T_CTI_Task model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new CallCenterApi.Model.T_CTI_Task();
                    if (dt.Rows[n]["TaskID"] != null && dt.Rows[n]["TaskID"].ToString() != "")
                    {
                        model.TaskID = long.Parse(dt.Rows[n]["TaskID"].ToString());
                    }
                    if (dt.Rows[n]["TaskName"] != null && dt.Rows[n]["TaskName"].ToString() != "")
                    {
                        model.TaskName = dt.Rows[n]["TaskName"].ToString();
                    }

                    if (dt.Rows[n]["State"] != null && dt.Rows[n]["State"].ToString() != "")
                    {
                        model.State = int.Parse(dt.Rows[n]["State"].ToString());
                    }
                    if (dt.Rows[n]["AddTime"] != null && dt.Rows[n]["AddTime"].ToString() != "")
                    {
                        model.AddTime = DateTime.Parse(dt.Rows[n]["AddTime"].ToString());
                    }

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


                    modelList.Add(model);
                }
            }
            return modelList;
        }

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public List<CallCenterApi.Model.vw_CTI_TaskPager> DataTableToListPager(DataTable dt)
        //{
        //    List<CallCenterApi.Model.vw_CTI_TaskPager> modelList = new List<CallCenterApi.Model.vw_CTI_TaskPager>();
        //    int rowsCount = dt.Rows.Count;
        //    var columns = dt.Columns;
        //    if (rowsCount > 0)
        //    {

        //        CallCenterApi.Model.vw_CTI_TaskPager model;
        //        for (int n = 0; n < rowsCount; n++)
        //        {
        //            try
        //            {
        //                model = new CallCenterApi.Model.vw_CTI_TaskPager();
        //                if (dt.Rows[n]["TaskID"] != null && dt.Rows[n]["TaskID"].ToString() != "")
        //                {
        //                    model.TaskID = long.Parse(dt.Rows[n]["TaskID"].ToString());
        //                }
        //                if (dt.Rows[n]["TaskName"] != null && dt.Rows[n]["TaskName"].ToString() != "")
        //                {
        //                    model.TaskName = dt.Rows[n]["TaskName"].ToString();
        //                }
        //                if (columns.Contains("TrunkGroupID"))
        //                {
        //                    if (dt.Rows[n]["TrunkGroupID"] != null && dt.Rows[n]["TrunkGroupID"].ToString() != "")
        //                    {
        //                        model.TrunkGroupID = int.Parse(dt.Rows[n]["TrunkGroupID"].ToString());
        //                    }
        //                }
        //                if (dt.Rows[n]["CallerNum"] != null && dt.Rows[n]["CallerNum"].ToString() != "")
        //                {
        //                    model.CallerNum = dt.Rows[n]["CallerNum"].ToString();
        //                }
        //                if (dt.Rows[n]["CallType"] != null && dt.Rows[n]["CallType"].ToString() != "")
        //                {
        //                    model.CallType = int.Parse(dt.Rows[n]["CallType"].ToString());
        //                }
        //                if (dt.Rows[n]["ExInfo"] != null && dt.Rows[n]["ExInfo"].ToString() != "")
        //                {
        //                    model.ExInfo = dt.Rows[n]["ExInfo"].ToString();
        //                }
        //                if (dt.Rows[n]["MaxTrunkCount"] != null && dt.Rows[n]["MaxTrunkCount"].ToString() != "")
        //                {
        //                    model.MaxTrunkCount = int.Parse(dt.Rows[n]["MaxTrunkCount"].ToString());
        //                }
        //                if (dt.Rows[n]["IntensityCoefficient"] != null && dt.Rows[n]["IntensityCoefficient"].ToString() != "")
        //                {
        //                    model.IntensityCoefficient = decimal.Parse(dt.Rows[n]["IntensityCoefficient"].ToString());
        //                }
        //                if (dt.Rows[n]["ItemTableName"] != null && dt.Rows[n]["ItemTableName"].ToString() != "")
        //                {
        //                    model.ItemTableName = dt.Rows[n]["ItemTableName"].ToString();
        //                }
        //                if (dt.Rows[n]["State"] != null && dt.Rows[n]["State"].ToString() != "")
        //                {
        //                    model.State = int.Parse(dt.Rows[n]["State"].ToString());
        //                }
        //                if (dt.Rows[n]["AddTime"] != null && dt.Rows[n]["AddTime"].ToString() != "")
        //                {
        //                    model.AddTime = Convert.ToDateTime(dt.Rows[n]["AddTime"].ToString());
        //                }
        //                if (dt.Rows[n]["y_PSort"] != null && dt.Rows[n]["y_PSort"].ToString() != "")
        //                {
        //                    model.y_PSort = int.Parse(dt.Rows[n]["y_PSort"].ToString());
        //                }
        //                if (dt.Rows[n]["y_TkModelId"] != null && dt.Rows[n]["y_TkModelId"].ToString() != "")
        //                {
        //                    model.y_TkModelId = long.Parse(dt.Rows[n]["y_TkModelId"].ToString());
        //                }
        //                if (dt.Rows[n]["y_SXH"] != null && dt.Rows[n]["y_SXH"].ToString() != "")
        //                {
        //                    model.y_SXH = int.Parse(dt.Rows[n]["y_SXH"].ToString());
        //                }
        //                if (dt.Rows[n]["y_FPCount"] != null && dt.Rows[n]["y_FPCount"].ToString() != "")
        //                {
        //                    model.y_FPCount = int.Parse(dt.Rows[n]["y_FPCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_YJCount"] != null && dt.Rows[n]["y_YJCount"].ToString() != "")
        //                {
        //                    model.y_YJCount = int.Parse(dt.Rows[n]["y_YJCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_HCCount"] != null && dt.Rows[n]["y_HCCount"].ToString() != "")
        //                {
        //                    model.y_HCCount = int.Parse(dt.Rows[n]["y_HCCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_HSCount"] != null && dt.Rows[n]["y_HSCount"].ToString() != "")
        //                {
        //                    model.y_HSCount = int.Parse(dt.Rows[n]["y_HSCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_HTCount"] != null && dt.Rows[n]["y_HTCount"].ToString() != "")
        //                {
        //                    model.y_HTCount = int.Parse(dt.Rows[n]["y_HTCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_HMCount"] != null && dt.Rows[n]["y_HMCount"].ToString() != "")
        //                {
        //                    model.y_HMCount = int.Parse(dt.Rows[n]["y_HMCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_OkCount"] != null && dt.Rows[n]["y_OkCount"].ToString() != "")
        //                {
        //                    model.y_OkCount = int.Parse(dt.Rows[n]["y_OkCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_RFCount"] != null && dt.Rows[n]["y_RFCount"].ToString() != "")
        //                {
        //                    model.y_RFCount = int.Parse(dt.Rows[n]["y_RFCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_ConsCount"] != null && dt.Rows[n]["y_ConsCount"].ToString() != "")
        //                {
        //                    model.y_ConsCount = int.Parse(dt.Rows[n]["y_ConsCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_InvlCount"] != null && dt.Rows[n]["y_InvlCount"].ToString() != "")
        //                {
        //                    model.y_InvlCount = int.Parse(dt.Rows[n]["y_InvlCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_NoAnswerCount"] != null && dt.Rows[n]["y_NoAnswerCount"].ToString() != "")
        //                {
        //                    model.y_NoAnswerCount = int.Parse(dt.Rows[n]["y_NoAnswerCount"].ToString());
        //                }
        //                if (dt.Rows[n]["y_ShutDownCount"] != null && dt.Rows[n]["y_ShutDownCount"].ToString() != "")
        //                {
        //                    model.y_ShutDownCount = int.Parse(dt.Rows[n]["y_ShutDownCount"].ToString());
        //                }
        //                if (dt.Rows[n]["TaskType"] != null && dt.Rows[n]["TaskType"].ToString() != "")
        //                {
        //                    model.TaskType = int.Parse(dt.Rows[n]["TaskType"].ToString());
        //                }
        //                model.F_Title = dt.Rows[n]["F_Title"].ToString();
        //                modelList.Add(model);
        //            }
        //            catch
        //            {
        //                throw new Exception("AddTime=" + dt.Rows[n]["AddTime"].ToString());
        //            }
        //        }

        //    }
        //    return modelList;
        //}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}


        public DataSet GetTask(int groupId)
        {
            return dal.GetTask(groupId);
        }

        /// <summary>
        /// 语音自动回访  //taskid=-10002 是语音自动回访任务，固定，仅可以修改内容
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="workOrderId"></param>
        /// <param name="assignId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool AddToAutoVoiceNotice(string phone, string workOrderId, int? assignId, out string msg)
        {
            msg = "";
            var model = dal.GetModel(-10002);
            if (model == null)
            {
                msg = "语音回访队列阻塞，请联系管理员";
                return false;
            }


            //string fix = ""; string bfix = ""; string wfix = "";
            //string zipcode = Configs.GetValue("CallOutZipCode");
            //bfix = Configs.GetValue("CallOutBPre");
            //wfix = Configs.GetValue("CallOutWPre");
            //int zip = GetZipCodeByPhone(phone, zipcode);
            //if (zip == 1)
            //{
            //    fix = bfix;
            //}
            //else if (zip == 2)
            //{
            //    fix = wfix;
            //}



            var i = telNumDal.Add(new Model.T_Call_TaskTelNum
            {
                F_TaskId = -10002,
                //F_Phone = fix + phone,
                F_Phone = phone,
                WorkOrderId = workOrderId,
                AssignedId = assignId
            });
            if (i > 0)
            {
                model.y_HMCount = model.y_HMCount + 1;
                var flag = dal.Update(model);
                if (flag)
                {
                    msg = "号码已加入语音回访队列";
                    return true;
                }
                return false;
            }
            msg = "语音回访队列阻塞，请联系管理员";
            return false;
        }

        public int GetZipCodeByPhone(string phone, string zipcode)
        {
            int res = 0;
            if (phone.Trim().Length >= 7)
            {
                //7位及7位以上是固定电话或手机
                //判断是否手机
                if (phone.Trim().Length == 11 && phone[0] == '1')
                {//号码为11位，首位是1，为手机号
                    BLL.T_Sys_MobileData mobile_Bll = new BLL.T_Sys_MobileData();
                    Model.T_Sys_MobileData mobileModel = mobile_Bll.GetModelList(" F_MobileNum = '" + phone.Substring(0, 7) + "'").FirstOrDefault();
                    if (mobileModel != null)
                    {
                        if (mobileModel.F_ZipCode.Equals(zipcode))
                        {
                            res = 1;
                        }
                        else
                        {
                            res = 2;
                        }
                    }
                }
                else
                {
                    if (phone.Trim().Length == 11 && phone.Substring(0, 3).Equals(zipcode))
                    {//号码为11位
                        //截取前三位区号判断是否本地
                        bool resbd3 = phone.Substring(0, 3).Equals(zipcode);
                        //截取前四位区号判断是否本地
                        bool resbd4 = phone.Substring(0, 4).Equals(zipcode);
                        if (resbd3 || resbd4)
                        {
                            res = 1;
                        }
                    }
                    else if (phone.Trim().Length < 11)
                    {//号码小于11位，为本地
                        res = 1;
                    }
                    else if (phone.Trim().Length > 11 && phone.Substring(0, 4).Equals(zipcode))
                    {//号码大于11位，截取前四位区号判断是否本地
                        res = 1;
                    }
                    else
                    {
                        res = 2;
                    }
                }
            }
            return res;
        }

        #endregion  Method
    }
}

