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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.AirportLine;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentInspectionMapper;
import com.tuoheng.admin.mapper.AirportLineMapper;
import com.tuoheng.admin.request.accident.AirportRoute;
import com.tuoheng.admin.vo.airport.AirPortLineLocationVo;
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;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据应急事故ID查询航线业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-04-21
 */
@Slf4j
@Service
public class QueryAirportLineByWaterwayService {

    @Autowired
    private AccidentInspectionMapper accidentInspectionMapper;

    @Autowired
    private AirportLineMapper airportLineMapper;

    public JsonResult getAirportLineList(Inspection inspection) {
//        log.info("进入根据应急事故ID查询航线业务");
        JsonResult result = this.check(inspection);
        if (0 != result.getCode()) {
            log.info("进入根据应急事故ID查询航线业务：校验失败：{}", result.getMsg());
            return result;
        }
        String coordinates = (String) result.getData();
        List<AirportRoute> airportRouteList = JSONObject.parseArray(coordinates, AirportRoute.class);
        List<AirPortLineLocationVo> airPortLineLocationVoList = new ArrayList<>();
        AirPortLineLocationVo airPortLineLocationVo;
        for (AirportRoute airportRoute : airportRouteList) {
            airPortLineLocationVo = new AirPortLineLocationVo();
            airPortLineLocationVo.setLon(airportRoute.getLng());
            airPortLineLocationVo.setLat(airportRoute.getLat());
            airPortLineLocationVoList.add(airPortLineLocationVo);
        }
        return JsonResult.success(airPortLineLocationVoList);
    }

    /**
     * 检查参数
     *
     * @param inspection
     * @return
     */
    private JsonResult check(Inspection inspection) {
        AirportLine airportLine = airportLineMapper.selectOne(new LambdaQueryWrapper<AirportLine>()
                .eq(AirportLine::getInspectionId, inspection.getId())
                .eq(AirportLine::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isEmpty(airportLine)) {
            log.info("根据任务ID查询航线业务：航线数据不存在：inspectionId={}", inspection.getId());
            throw new ServiceException("航线数据不存在");
        }
        String coordinates = airportLine.getCoordinates();
        if (StringUtils.isEmpty(coordinates)) {
            log.info("根据任务ID查询航线业务：航线数据为空：inspectionId={}", inspection.getId());
            throw new ServiceException("航线数据为空");
        }
        return JsonResult.success(coordinates, "");
    }

}
