package org.security.trans.taskCheck.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.common.util.DataFormatUtil;
import org.security.trans.dailyDuties.entity.Inspection;
import org.security.trans.dailyDuties.service.IInspectionService;
import org.security.trans.prevent.entity.PreventMaterial;
import org.security.trans.prevent.service.IPreventMaterialService;
import org.security.trans.securityCommandCenter.entity.SccAlarmPush;
import org.security.trans.securityCommandCenter.entity.SccUnlocaleCheck;
import org.security.trans.securityCommandCenter.service.ISccAlarmPushService;
import org.security.trans.securityCommandCenter.service.ISccUnlocaleCheckService;
import org.security.trans.task.entity.TaskHandin;
import org.security.trans.task.service.ITaskHandinService;
import org.springblade.core.tool.utils.Func;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zj 2022/4/14
 */
@Slf4j
@AllArgsConstructor
@Component
public class TaskDeadlineCheckTask {
	private final ITaskHandinService taskHandinService;
	private final IInspectionService iInspectionService;
	private final IPreventMaterialService preventMaterialService;
	private final ISccUnlocaleCheckService sccUnlocaleCheckService;
	private final ISccAlarmPushService sccAlarmPushService;

	/**
	 * 检查提报任务是否超时
	 */
	@Async("securityThreadPool")
	@Scheduled(cron = "0 0 23 * * ?")
	public void deadlineCeheck() throws ParseException {
		log.info("==============检测提报任务是否超时开始处理==============");
		List<TaskHandin> taskHandinList = taskHandinService.getTaskList(Constant.TASK_HANDIN_STATE_DRAFT); //待执行的taskHandin记录
		List<Inspection> inspectionList = iInspectionService.getCheckList(Constant.PROBLEM_STATE_DRAFT); //待提交检查的inspection记录
		List<Inspection> inspectionListR = iInspectionService.getCheckList(Constant.PROBLEM_STATE_WAIT_RECTIFY); //待整改的inspection记录
		List<PreventMaterial> preventMaterialsList = preventMaterialService.getPreventList(Constant.TASK_HANDIN_STATE_DRAFT); //待提交的案件防控preventMaterials记录
		//非现场检查整改整改通知书待整改任务
		List<SccUnlocaleCheck> sccUnlocaleCheckList = sccUnlocaleCheckService.getListData(Constant.PROBLEM_STATE_DRAFT_ONE);
		//警情信息未提报任务数据
//		List<SccAlarmPush> sccAlarmPushList = sccAlarmPushService.getListData(Constant.PUSH_STATUS_THREE);
		Date today = DataFormatUtil.getLocalDate();
		//教育培训或应急演练任务超期未提报
		updateEduOrDrill(taskHandinList, today);
		//检查整改超期未提交检查记录
		setOvertimeCheck(inspectionList, today);
		//检查整改记录超期未进行整改提交
		setOvertimeRectification(inspectionListR, today);
		//案件防控任务超期未提报
		setOvertimePrevent(preventMaterialsList, today);
		//整改通知书超时未提报
		setOvertimeUnlocaleCheck(sccUnlocaleCheckList, today);
		//警情信息超时未提报
//		ArrayList<SccAlarmPush> alarmPushes = new ArrayList<>();
//		if (Func.isNotEmpty(sccAlarmPushList)){
//			sccAlarmPushList.forEach(item ->{
//			});
//		}
		log.info("==============检测提报任务是否超时处理结束==============");
	}

	/**
	 *整改通知书超时未提报
	 */
	private void setOvertimeUnlocaleCheck(List<SccUnlocaleCheck> sccUnlocaleCheckList, Date today) {
		ArrayList<SccUnlocaleCheck> sccUnlocaleChecks = new ArrayList<>();
		if (Func.isNotEmpty(sccUnlocaleCheckList)){
			sccUnlocaleCheckList.forEach(item ->{
				if (Func.isNotEmpty(item.getRectificationDeadline()) && today.after(item.getRectificationDeadline())){
					item.setStatus(Constant.PROBLEM_STATE_FOURE);
					sccUnlocaleChecks.add(item);
				}
			});
			sccUnlocaleCheckService.updateBatchById(sccUnlocaleChecks);
		}
	}

	/**
	 * 案件防控任务超期未提报
	 */
	private void setOvertimePrevent(List<PreventMaterial> preventMaterialsList, Date today) {
		ArrayList<PreventMaterial> list = new ArrayList<>();
		if (Func.isNotEmpty(preventMaterialsList)) {
			preventMaterialsList.forEach(item -> {
				Date deadline = item.getDeadline();
				if (Func.isNotEmpty(deadline)) {
					if (today.after(deadline)) {
						item.setStatus(Constant.TASK_HANDIN_STATE_DRAFT_OVERTIME);
						list.add(item);
					}
				}
			});
			preventMaterialService.updateBatchById(list);
		}
	}

	/**
	 * 检查整改记录超期未进行整改提交
	 */
	private void setOvertimeRectification(List<Inspection> inspectionListR, Date today) {
		ArrayList<Inspection> list = new ArrayList<>();
		if (Func.isNotEmpty(inspectionListR)) {
			inspectionListR.forEach(inspection -> {
				Date deadline = inspection.getRectifyDeadline();
				if (Func.isNotEmpty(deadline)) {
					if (today.after(deadline)) {
						inspection.setStatus(Constant.PROBLEM_STATE_OVERTIME);
						list.add(inspection);
					}
				}
			});
			iInspectionService.updateBatchById(list);
		}
	}

	/**
	 * 检查整改超期未提交检查记录
	 */
	private void setOvertimeCheck(List<Inspection> inspectionList, Date today) {
		ArrayList<Inspection> list = new ArrayList<>();
		if (Func.isNotEmpty(inspectionList)) {
			inspectionList.forEach(inspection -> {
				Date deadline = inspection.getDeadline();
				if (Func.isNotEmpty(deadline)) {
					if (today.after(deadline)) {
						inspection.setStatus(Constant.PROBLEM_STATE_DRAFT_OVERTIME);
						list.add(inspection);
					}
				}
			});
			iInspectionService.updateBatchById(list);
		}
	}

	/**
	 * 教育培训或应急演练任务超期未提报
	 */
	private void updateEduOrDrill(List<TaskHandin> taskHandinList, Date today) {
		ArrayList<TaskHandin> list = new ArrayList<>();
		if (Func.isNotEmpty(taskHandinList)) {
			taskHandinList.forEach(taskHandin -> {
				Date deadline = taskHandin.getDeadline();
				//超时
				if (Func.isNotEmpty(deadline)) {
					if (today.after(deadline)) {
						taskHandin.setStatus(Constant.TASK_HANDIN_STATE_DRAFT_OVERTIME);
						list.add(taskHandin);
					}
				}
			});
			taskHandinService.updateBatchById(list);
		}
	}
}
