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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.yuchen.saas.park.api.entity.park.FireInspectPlanEntity;
import com.yuchen.saas.park.api.entity.park.FireInspectTaskEntity;
import com.yuchen.saas.park.api.enums.PlanStatusEnum;
import com.yuchen.saas.park.api.enums.TaskStatusEnum;
import com.yuchen.saas.park.service.service.FireInspectPlanService;
import com.yuchen.saas.park.service.service.FireInspectTaskService;
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.List;

/**
 * @author xiaoxiang
 * @Date: 2022/10/28 10:08
 * @Description: 定时任务处理类
 */
@Component
@Slf4j
public class ParkInspectPlan {

    @Resource
    private FireInspectPlanService fireInspectPlanService;

    @Resource
    private FireInspectTaskService fireInspectTaskService;

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

    private void updatePlanStatus(){
        //根据状态查询任务
        LambdaQueryWrapper<FireInspectPlanEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //1、未开始2、进行中
        lambdaQueryWrapper.in(FireInspectPlanEntity::getPlanStatus, Arrays.asList(1,2));
        List<FireInspectPlanEntity> fireInspectPlanEntityList = fireInspectPlanService.list(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(fireInspectPlanEntityList)){
            log.info("没有计划可以进行更新");
            return;
        }

        //根据计划查询任务时间
        for (FireInspectPlanEntity planEntity : fireInspectPlanEntityList) {
            //根据计划查询任务
            LambdaQueryWrapper<FireInspectTaskEntity> planTaskQuery = new LambdaQueryWrapper<>();
            //1、未开始2、进行中
            planTaskQuery.eq(FireInspectTaskEntity::getInspectPlanId, planEntity.getId());
            List<FireInspectTaskEntity> taskList = fireInspectTaskService.list(planTaskQuery);
            if(CollectionUtils.isEmpty(taskList)){
                log.info("此计划没有任务:{}",planEntity.getName());
                continue;
            }
            //总共的任务数
            long totalTaskCount = taskList.size();
            //开始中的任务
            long startIng = taskList.stream().filter(task->task.getTaskStatus().equals(TaskStatusEnum.STARTING.getCode())).count();
            //已结束的任务
            long endTask = taskList.stream().filter(task->task.getTaskStatus().equals(TaskStatusEnum.OVER.getCode())).count();
            if(startIng > 0 && totalTaskCount > endTask){
                planEntity.setPlanStatus(PlanStatusEnum.STARTING.getCode());
                fireInspectPlanService.updateById(planEntity);
            }
            if(totalTaskCount == endTask){
                planEntity.setPlanStatus(PlanStatusEnum.OVER.getCode());
                fireInspectPlanService.updateById(planEntity);
            }

        }
    }
}
