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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.controller.AirportLineController;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.AirportLine;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.mapper.AirportLineMapper;
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-21
 */
@Slf4j
@Service
public class QueryAirportLineByAccidentIdService {

    @Autowired
    private AirportLineMapper airportLineMapper;

    public JsonResult getInfoByAccidentId(String accidentId) {
//        log.info("进入根据应急事故ID查询航线业务");
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, accidentId);
        if (0 != result.getCode()) {
            log.info("进入根据应急事故ID查询航线业务：校验失败：{}", result.getMsg());
            return result;
        }

        AirportLine airportLine = (AirportLine) result.getData();

        return JsonResult.success(airportLine);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param accidentId
     * @return
     */
    private JsonResult check(String tenantId, String accidentId) {
        if (StringUtils.isEmpty(accidentId)) {
            throw new ServiceException("应急事故ID为空");
        }
        AirportLine airportLine = airportLineMapper.selectOne(new LambdaQueryWrapper<AirportLine>()
                .eq(AirportLine::getTenantId, tenantId)
                .eq(AirportLine::getAccidentId, accidentId)
                .eq(AirportLine::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(airportLine)) {
            throw new ServiceException("航线数据不存在");
        }
        return JsonResult.success(airportLine);
    }

}
