package com.yuchen.saas.park.service.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuchen.saas.park.api.entity.park.FireInspectTaskEntity;
import com.yuchen.saas.park.api.entity.park.FireTaskPartEntity;
import com.yuchen.saas.park.api.enums.TaskCompleteSituEnum;
import com.yuchen.saas.park.api.enums.TaskStatusEnum;
import com.yuchen.saas.park.service.service.FireInspectTaskService;
import com.yuchen.saas.park.service.service.FireTaskPartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author xiaoxiang
 * @Date: 2022/10/27 17:31
 * @Description: 定时任务处理类
 */
@Component
@Slf4j
public class ParkInspectTask {

    @Resource
    private FireTaskPartService fireTaskPartService;

    @Resource
    private FireInspectTaskService fireInspectTaskService;

    /**
     * updateTaskStatusScheduled 10 min exec one
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void updateTaskStatusScheduled() {
        log.info("巡检计划任务开始=====================================》");
        long startTime = System.currentTimeMillis();
        updateTaskCompleteStatus();
        log.info("巡检计划任务结束================================》{}",(System.currentTimeMillis()-startTime));
    }

    /**
     * updateTaskCompleteStatus
     */
    private void updateTaskCompleteStatus() {
        LambdaQueryWrapper<FireInspectTaskEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(FireInspectTaskEntity::getTaskStatus, Arrays.asList(1, 2));
        List<FireInspectTaskEntity> fireInspectTaskEntityList = fireInspectTaskService.list(lambdaQueryWrapper);
        for (FireInspectTaskEntity fireInspectTask : fireInspectTaskEntityList) {
            //update task processor
            Integer fenZi = fireTaskPartService.lambdaQuery()
                    .eq(FireTaskPartEntity::getTaskId, fireInspectTask.getId())
                    .eq(FireTaskPartEntity::getPartInspectStatus, 1).count();
            Integer fenMu = fireTaskPartService.lambdaQuery()
                    .eq(FireTaskPartEntity::getTaskId, fireInspectTask.getId()).count();

            //查询任务
            FireInspectTaskEntity fireInspectTaskEntity = fireInspectTaskService.getById(fireInspectTask.getId());
            LambdaUpdateWrapper<FireInspectTaskEntity> fireTask = new LambdaUpdateWrapper<>();
            //任务完成情况:1、按时完成 2、超时完成3、异常终止4、超时未完成
            Date now = new Date();//当前完成时间
            if (!Objects.equals(fenZi, fenMu) && now.after(fireInspectTaskEntity.getTaskEndTime())) {
                fireTask.set(FireInspectTaskEntity::getTaskCompletionSituation, TaskCompleteSituEnum.TIMEOUT_NOT_COMPLETED.getCode());
                fireTask.set(FireInspectTaskEntity::getTaskStatus, TaskStatusEnum.STARTING.getCode());
            }
            fireTask.set(FireInspectTaskEntity::getTaskProcessor, fenZi + "/" + fenMu);
            fireTask.eq(FireInspectTaskEntity::getId, fireInspectTask.getId());
            fireInspectTaskService.update(new FireInspectTaskEntity(), fireTask);
        }
    }


}
