package com.tuoheng.admin.service.airportline.query.inspection;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.AccidentInspection;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentInspectionMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.service.airportline.query.accident.QueryAirportLineByAccidentIdService;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
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.stereotype.Service;

/**
 * 根据任务ID获取航线业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-04-26
 */
@Slf4j
@Service
public class QueryAirportLineByInspectionIdFactory {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private QueryAirportLineByAirportService queryAirportLineByAirportService;

    @Autowired
    private QueryAirportLineByWaterwayService queryAirportLineByWaterwayService;

    /**
     * 无人机前往
     *
     * @return
     */
    public JsonResult getAirportLineList(String inspectionId) {
        log.info("进入根据任务ID获取航线业务");
        User user = CurrentUserUtil.getUserInfo();
        JsonResult result = this.checkParam(user, inspectionId);
        if (0 != result.getCode()) {
            log.info("根据任务ID获取航线：校验参数失败：{}", result.getMsg());
            return result;
        }
        Inspection inspection = (Inspection) result.getData();
        if (1 == inspection.getType()) {
            log.info("根据任务ID获取航线业务，日常巡检，inspectionId={}，inspectionLine={}", inspection.getId(), inspection.getInspectionLine());
            result = queryAirportLineByAirportService.getAirportLineList(inspection);
        } else if (2 == inspection.getType()) {
            log.info("根据任务ID获取航线业务，应急巡检，inspectionId={}，inspectionLine={}", inspection.getId(), inspection.getInspectionLine());
            result = queryAirportLineByWaterwayService.getAirportLineList(inspection);
        } else {
            log.info("根据任务ID获取航线业务，类型错误");
            return JsonResult.error("根据任务ID获取航线业务，类型错误");
        }
        return result;
    }

    /**
     * 检查参数
     *
     * @param user
     * @param inspectionId
     * @return
     */
    private JsonResult checkParam(User user, String inspectionId) {
        if (StringUtils.isEmpty(inspectionId)) {
            throw new ServiceException("任务ID为空");
        }
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getTenantId, user.getTenantId())
                .eq(Inspection::getId, inspectionId)
                .eq(Inspection::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(inspection)) {
            throw new ServiceException("任务不存在空");
        }
        return JsonResult.success(inspection);
    }

}
