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

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.TaskStatusEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.request.inspection.UpdateTaskByCodeRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 修改任务(修改任务状态和飞手信息)
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-23
 */
@Slf4j
@Service
public class UpdateFlyerService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private OnlineIdentifService onlineIdentifService;

    @Autowired
    private OnlineCompleteService onlineCompleteService;

    @Autowired
    private OffLineCompleteService offLineCompleteService;

    /**
     * 修改任务(修改任务状态和飞手信息)
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult update(UpdateTaskByCodeRequest request) {
        log.info("进入修改任务状态和飞手信息业务接口", JacksonUtil.obj2StringPretty(request));
        JsonResult result = this.check(request);
        if (0 != result.getCode()) {
            log.info("修改任务状态和飞手信息业务接口：校验失败：{}", result.getMsg());
            return result;
        }

        Inspection inspection = (Inspection) result.getData();

        Integer status = request.getStatus();
        // 如果任务审核未通过，状态改为待审核
        if (status == TaskStatusEnum.AUDITFAILED.getCode()) {
            request.setStatus(1);
        }

        if (status == TaskStatusEnum.INFLIGHT.getCode()) {
            //开始飞行
            this.updateInflight(request, inspection);
        } else if (status == TaskStatusEnum.COMPLETE.getCode()) {
            //飞行完成
            this.updateComplete(request, inspection);
        }

        // 直播
        if (1 == inspection.getIsLive() && status == TaskStatusEnum.INFLIGHT.getCode()) {
            onlineIdentifService.handle(request, inspection);
        }

        //离线
        if (2 == inspection.getIsLive() && status == TaskStatusEnum.COMPLETE.getCode()) {
            offLineCompleteService.handle(inspection);
        }

        // 如果直播状态，飞行完成修改状态为待关闭（如果任务分析状态为失败或成功，不修改通道状态）
        if (1 == inspection.getIsLive() && status == TaskStatusEnum.COMPLETE.getCode()
                && 4 != inspection.getAnalyseStatus()
                && 5 != inspection.getAnalyseStatus()
                && 6 != inspection.getAnalyseStatus()) {
            onlineCompleteService.handle(inspection);
        }
        return JsonResult.success();
    }

    /**
     * 检查参数
     *
     * @param request
     * @return
     */
    private JsonResult check(UpdateTaskByCodeRequest request) {
        if (ObjectUtil.isNull(request.getStatus())) {
            throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "变量status不能为空");
        }
        if (ObjectUtil.isNull(request.getTaskCode())) {
            throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "变量id不能为空");
        }
        // 检查任务是否存在
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                                .eq(Inspection::getCode, request.getTaskCode())
                                .eq(Inspection::getMark, 1));
        if (inspection == null) {
            throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "当前任务不存在");
        }
        return JsonResult.success(inspection);
    }

    private void updateInflight(UpdateTaskByCodeRequest request, Inspection inspection) {
        //直播填充盒子设备
        if (1 == inspection.getIsLive()) {
            inspection.setCloudBoxId(request.getCloudBoxId());
            inspection.setCloudBoxName(request.getCloudBoxName());
            inspection.setBoxSn(request.getBoxSn());
        }
        inspection.setEquipmentId(request.getEquipmentId());
        inspection.setEquipmentName(request.getEquipmentName());
        inspection.setEquipmentMountId(request.getEquipmentMountId());
        inspection.setEquipmentMountName(request.getEquipmentMountName());
        inspection.setStatus(request.getStatus());
        inspection.setFlightHand(request.getFlightHand());
        inspection.setFlightHandName(request.getFlightHandName());
        inspection.setExecutionStartTime(request.getFlightStartTime());
        //更改
        inspectionMapper.updateById(inspection);
    }

    private void updateComplete(UpdateTaskByCodeRequest request, Inspection inspection) {
        inspection.setStatus(request.getStatus());
        inspection.setExecutionEndTime(request.getFlightEndTime());
        inspectionMapper.updateById(inspection);
    }
}
