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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.conver.InspectionConverMapper;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.AirPortTaskStatusEnum;
import com.tuoheng.admin.enums.MarkTypeEnum;
import com.tuoheng.admin.enums.TaskStatusEnum;
import com.tuoheng.admin.enums.code.dept.AddDeptCodeEnum;
import com.tuoheng.admin.enums.code.inspection.EditInspectionCodeEnum;
import com.tuoheng.admin.enums.code.inspection.EditInspectionStatusCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.request.inspection.EditInspectionRequest;
import com.tuoheng.admin.request.inspection.MissionStatusRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.DspCallbackVo;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 修改任务状态业务层处理
 *
 * @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("进入修改任务状态业务接口");
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, missionStatusRequest);
        if (0 != result.getCode()) {
            log.info("修改任务状态业务接口：校验失败：{}", result.getMsg());
            return result;
        }

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

        if (AirPortTaskStatusEnum.FLIGHT.getCode() != missionStatusRequest.getStatus() && AirPortTaskStatusEnum.WAIT.getCode() == missionStatusRequest.getStatus()) {
            return JsonResult.error(EditInspectionStatusCodeEnum.TASK_NOT_STATUS.getCode(), EditInspectionStatusCodeEnum.TASK_NOT_STATUS.getMsg());
        }

        if (AirPortTaskStatusEnum.FLIGHT.getCode() == missionStatusRequest.getStatus()) {
            updateFlightStatusService.updateFlightStatus(inspection, missionStatusRequest);
        } else if (AirPortTaskStatusEnum.WAIT.getCode() == missionStatusRequest.getStatus()) {
            updateWaittStatusService.updateWaittStatus(inspection);
        }

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

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

}
