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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tuoheng.admin.entity.vo.airport.AirportLineVo;
import com.tuoheng.admin.service.third.airport.AirportService;
import com.tuoheng.common.core.exception.ServiceException;
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 java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class QueryAirportLineInfoService {

    @Autowired
    private AirportService airportService;

    public JsonResult getInfo(Integer id) {
        JsonResult result = this.check(id);
        if (0 != result.getCode()) {
            log.info("根据id查询航线信息：校验失败：{}", result.getMsg());
            return result;
        }
        List<Integer> airportLineIdList = new ArrayList<>();
        airportLineIdList.add(id);
        List<AirportLineVo> airportLineVoList = airportService.getAirportLineListByAirportIdAndLineIdList(null, airportLineIdList);

        if (CollectionUtil.isEmpty(airportLineVoList)) {
            log.info("该航线不存在，id={}", id);
            return JsonResult.success();
        }
        AirportLineVo airportLineVo = airportLineVoList.get(0);
        return JsonResult.success(airportLineVo);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            log.info("参数ID为空");
            throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "参数ID为空");
        }
        return JsonResult.success();
    }
}
