package com.tuoheng.admin.service.inspection.update.status;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.AirPortTaskStatusEnum;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.EditInspectionStatusCodeEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.request.inspection.MissionStatusRequest;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 修改任务状态业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-24
 */
@Slf4j
@Service
public class UpdateInspectionStatusService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private UpdateFlightStatusService updateFlightStatusService;

    @Autowired
    private UpdateWaittStatusService updateWaittStatusService;

    /**
     * 添加任务
     *
     * @return
     */
    public JsonResult updateStatus(MissionStatusRequest missionStatusRequest) {
        log.info("进入修改任务飞行开始/结束状态业务接口, missionStatusRequest:{}", missionStatusRequest);
        JsonResult result = this.check(missionStatusRequest);
        if (0 != result.getCode()) {
            log.info("修改任务状态业务接口：校验失败：{}", result.getMsg());
            return result;
        }

        Inspection inspection = (Inspection) result.getData();
        Integer status = null;
        if (AirPortTaskStatusEnum.FLIGHT.getCode() != missionStatusRequest.getStatus() && AirPortTaskStatusEnum.WAIT.getCode() != missionStatusRequest.getStatus()) {
            log.info("修改任务状态业务接口：飞行状态出错！");
            return JsonResult.error(EditInspectionStatusCodeEnum.TASK_NOT_STATUS.getCode(), EditInspectionStatusCodeEnum.TASK_NOT_STATUS.getMsg());
        }

        if (AirPortTaskStatusEnum.FLIGHT.getCode() == missionStatusRequest.getStatus()) {
            log.info("修改任务状态业务接口：修改执行中状态");
            status = InspectionStatusEnum.IN_FLIGHT.getCode();
            updateFlightStatusService.updateFlightStatus(inspection, missionStatusRequest);
        } else if (AirPortTaskStatusEnum.WAIT.getCode() == missionStatusRequest.getStatus()) {
            log.info("修改任务状态业务接口：修改待执行状态");
            status = InspectionStatusEnum.FLIGHT_COMPLETED.getCode();
            updateWaittStatusService.updateWaittStatus(inspection);
        }

        this.updateEmergencyStatus(inspection, status);

        log.info("修改任务业务接口：修改任务成功:{}");
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param missionStatusRequest
     * @return
     */
    private JsonResult check(MissionStatusRequest missionStatusRequest) {
        //查询出对应的巡检任务
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                .eq(Inspection::getId, missionStatusRequest.getRequestId()));
        if (ObjectUtil.isEmpty(inspection)) {
            log.info("任务不存在");
            return JsonResult.error(EditInspectionStatusCodeEnum.INSPECTION_IS_NOT_EXIST.getCode(), EditInspectionStatusCodeEnum.INSPECTION_IS_NOT_EXIST.getMsg());
        }
        return JsonResult.success(inspection);
    }

    /**
     * 修改应急任务数据
     *
     * @param oldInspection
     * @param status
     */
    public void updateEmergencyStatus(Inspection oldInspection, Integer status) {
        log.info("修改应急任务数据, id:{}, type:{}, status={}", oldInspection.getId(), oldInspection.getType(), status);
        Inspection emergencyInspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getEmergencyDataInspectionId, oldInspection.getId())
                .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(emergencyInspection)) {
            log.info("修改应急任务数据，该巡检任务没有对应的应急任务");
            return;
        }
        // 在查询一次，保证数据是最新的
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getId, oldInspection.getId())
                .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNotNull(status)) {
            emergencyInspection.setStatus(status);
        }
        emergencyInspection.setAnalyseStatus(inspection.getAnalyseStatus());
        emergencyInspection.setVideoUrl(inspection.getVideoUrl());
        emergencyInspection.setAiVideoUrl(inspection.getAiVideoUrl());
        inspectionMapper.updateById(emergencyInspection);
        log.info("修改应急任务数据完成");
    }
}
