package com.tuoheng.admin.tzhl.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.InspectionTypeEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.service.third.airport.AirportService;
import com.tuoheng.admin.tzhl.service.fly.data.FlyDataService;
import com.tuoheng.common.core.utils.StringUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 定时扫描，然后飞行
 */
@Component
@Slf4j
public class FlyDataTask {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private FlyDataService flyDataService;

    @XxlJob("telecomumaleFlyDataTaskHandler")
    public void airportTaskHandler() {
        log.info("执行定时，获取飞行数据：" + LocalDateTime.now());
        List<Inspection> inspectionList = inspectionMapper.selectList(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                //查询巡检方式为机场的任务
                .eq(Inspection::getInspectionType, InspectionTypeEnum.AIRPORT.getCode())
                //任务飞行中
                .eq(Inspection::getStatus, InspectionStatusEnum.IN_FLIGHT.getCode()));
        if (StringUtils.isEmpty(inspectionList)) {
            log.info("执行定时，获取飞行数据：任务为空" + inspectionList.size());
            return;
        }
        //遍历任务列表中正在执行的任务进行飞行数据入库
        for (Inspection inspection : inspectionList) {
            log.info("执行定时，获取飞行数据开始: inspectionId:{}", inspection.getId());
            flyDataService.getFlyData(inspection);
            log.info("执行定时，获取飞行数据结束: inspectionId:{}", inspection.getId());
        }
    }

}
