using CallCenterApi.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.QuartzService
{
    public class CallPlanWork : QuartzJob
    {
        public CallPlanWork()
        {
            CronExpression = "/30 * * * * ?";
            DoWork += MyWork_DoWork;
        }
        private readonly CallCenterApi.BLL.T_CTI_Task taskBLL = new CallCenterApi.BLL.T_CTI_Task();
        private readonly CallCenterApi.BLL.T_Call_TaskTelNum taskNumBLL = new CallCenterApi.BLL.T_Call_TaskTelNum();
        private readonly CallCenterApi.BLL.T_Sys_SystemConfig sysConfigBLL = new CallCenterApi.BLL.T_Sys_SystemConfig();
        private List<long> taskIdList = new List<long>();
        private void MyWork_DoWork(object sender, EventArgs e)
        {
            var now = DateTime.Now;
            var taskList = taskBLL.DataTableToList(taskBLL.GetTaskList(" State>0 ").Tables[0]).Where(x => x.PlanStartDate < now && now < x.PlanEndDate).ToList();

            #region 语音自动回访任务启动状态判断 & 号码清除 taskid=-10002 是语音自动回访任务，固定，仅可以修改内容
            var autoVoiceNotice = taskBLL.GetModel(-10002);
            if (autoVoiceNotice != null)
            {
                var autoCallBack = sysConfigBLL.GetParamValueByParamCode("AutoCallBack");
                if (autoCallBack == "1")
                {
                    taskBLL.UpdateState(-10002, 1);
                }
                else
                {
                    taskBLL.UpdateState(-10002, 2);
                    taskNumBLL.DeleteListWhere(" F_TaskId=-10002 and  F_HCState=0 ");
                }

            }

            #endregion

            #region 外呼任务状态由【暂停】=>【处理中】
            var startList = taskList.Where(x => x.State == (int)EnumTaskState.暂停 && ((x.StartTime1.TimeOfDay < now.TimeOfDay && now.TimeOfDay < x.EndTime1.TimeOfDay) || (x.StartTime2.TimeOfDay < now.TimeOfDay && now.TimeOfDay < x.EndTime2.TimeOfDay))).ToList();
            if (startList.Count > 0)
            {
                var startFlag = taskBLL.UpdateStateBatch($" where TaskID in ({string.Join(", ", startList.Select(x => x.TaskID).ToArray())})", (int)EnumTaskState.处理中);
            }
            #endregion

            #region 外呼任务状态由【处理中】=>【处理完成】
            var query = taskList.Where(x => x.State == (int)EnumTaskState.处理中).ToList();
            foreach (var item in query)
            {
                var taskNumList = taskNumBLL.DataTableToList(taskNumBLL.GetList(" F_TaskId=" + item.TaskID).Tables[0]);
                var allCount = taskNumList.Count();
                var connected = taskNumList.Where(x => x.F_YJState == 1).Count();
                //item.LoopTime 默认为0; F_HCCount默认为0; 所以这里判断要用 item.LoopTime + 1
                var notconnected = taskNumList.Where(x => x.F_YJState == 0 && x.F_HCCount == (item.LoopTime + 1)).Count();
                if (allCount == (connected + notconnected))
                {
                    taskIdList.Add(item.TaskID);
                }
            }
            if (taskIdList.Count > 0)
            {
                taskBLL.UpdateStateBatch($" where TaskID  in ({string.Join(", ", taskIdList.Select(x => x).ToArray())})", (int)EnumTaskState.处理完成);
            }
            #endregion

            #region 外呼任务状态由【处理中】=>【暂停】
            var closeList = query.Where(x => x.State == (int)EnumTaskState.处理中 && (x.StartTime1.TimeOfDay > now.TimeOfDay || (now.TimeOfDay > x.EndTime1.TimeOfDay && now.TimeOfDay < x.StartTime2.TimeOfDay) || now.TimeOfDay > x.EndTime2.TimeOfDay)).ToList();
            if (closeList.Count > 0)
            {
                var closeFlag = taskBLL.UpdateStateBatch($" where TaskID  in ({string.Join(", ", closeList.Select(x => x.TaskID).ToArray())})", (int)EnumTaskState.暂停);
            }
            #endregion



        }
    }
}
