package com.tuoheng.admin.service.warning.query;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.conver.WarningConverMapper;
import com.tuoheng.admin.entity.domain.Question;
import com.tuoheng.admin.entity.domain.ThMission;
import com.tuoheng.admin.entity.domain.Warning;
import com.tuoheng.admin.entity.vo.warning.WarningVo;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.QuestionMapper;
import com.tuoheng.admin.mapper.ThMissionMapper;
import com.tuoheng.admin.mapper.WarningMapper;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 根据Id查询预警信息业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2023-02-07
 */
@Slf4j
@Service
public class QueryWarningByIdService {

    @Autowired
    private WarningMapper warningMapper;

    @Autowired
    private ThMissionMapper missionMapper;

    @Autowired
    private QuestionMapper questionMapper;

    public JsonResult getOneById(Integer id) {
//        log.info("进入根据查询Id查询预警信息业务");
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();
        JsonResult result = this.check(tenantId, id);
        if (0 != result.getCode()) {
            log.info("根据Id查询预警信息业务：校验失败：{}", result.getMsg());
            return result;
        }
        Warning warning = (Warning) result.getData();

        // 构造返回结果对象
        WarningVo warningVo = this.buildWarningVo(warning);

        return JsonResult.success(warningVo);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param id
     * @return
     */
    private JsonResult check(Integer tenantId, Integer id) {
        // 判断预警id是否为空
        if (ObjectUtil.isNull(id)) {
            throw new ServiceException("预警ID为空");
        }
        Warning warning = warningMapper.selectOne(new LambdaQueryWrapper<Warning>()
                .eq(Warning::getId, id)
                .eq(Warning::getTenantId, tenantId)
                .eq(Warning::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(warning)) {
            throw new ServiceException("预警信息不存在");
        }
        return JsonResult.success(warning);
    }

    /**
     * @param warning
     * @return warningVo
     */
    private WarningVo buildWarningVo(Warning warning) {
        WarningVo warningVo = WarningConverMapper.INSTANCE.fromWarningToWarningVo(warning);
        ThMission mission = missionMapper.selectById(warningVo.getMissionId());
        if (ObjectUtil.isNotNull(mission)) {
            warningVo.setAirportId(mission.getAirportId());
            warningVo.setAirportName(mission.getAirportName());
            warningVo.setInspectionLine(mission.getInspectionLine());
            warningVo.setInspectionLineName(mission.getInspectionLineName());
        }
        Question question = questionMapper.selectOne(Wrappers.<Question>lambdaQuery()
                .eq(Question::getId, warning.getQuestionId())
                .eq(Question::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNotNull(question)) {
            warningVo.setQuestionType(question.getType());
            warningVo.setQuestionName(question.getQuestionName());
            warningVo.setQuestionDesc(question.getQuestionDesc());
            warningVo.setFileCode(question.getFileCode());
            warningVo.setFileName(question.getFileName());
            warningVo.setFileOriginalUrl(question.getFileOriginalUrl());
            warningVo.setFileMarkerUrl(question.getFileMarkerUrl());
            warningVo.setLng(question.getLng());
            warningVo.setLat(question.getLat());
        }
        return warningVo;
    }
}
