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.InspectionFileStatusEnum;
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.admin.tzhl.service.fly.log.FlyLogService;
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.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private FlyLogService flyLogService;

    @XxlJob("telecomumaleFlyLogTaskHandler")
    public void airportTaskHandler() {
        log.info("执行定时，获取飞行日志：" + LocalDateTime.now());
        List<Integer> statusList = new ArrayList<>();
        statusList.add(InspectionStatusEnum.PREPARING.getCode());
        statusList.add(InspectionStatusEnum.IN_FLIGHT.getCode());
        List<Inspection> inspectionList = inspectionMapper.selectList(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                .eq(Inspection::getInspectionType, InspectionTypeEnum.AIRPORT.getCode())
                .in(Inspection::getStatus,statusList));
        if (StringUtils.isEmpty(inspectionList)) {
            log.info("执行定时，获取飞行日志：任务为空" + inspectionList.size());
            return;
        }
        for (Inspection inspection : inspectionList) {
            log.info("执行定时，获取飞行日志开始: inspectionId:{}", inspection.getId());
            flyLogService.getFlyLog(inspection);
            log.info("执行定时，获取飞行日志结束: inspectionId:{}", inspection.getId());
        }
    }

}
