package com.tuoheng.admin.service.airport.drone;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.InspectionConverMapper;
import com.tuoheng.admin.entity.domain.ThInspection;
import com.tuoheng.admin.entity.domain.ThMission;
import com.tuoheng.admin.entity.domain.WarningRecord;
import com.tuoheng.admin.entity.vo.index.DroneDataVo;
import com.tuoheng.admin.enums.MarkTypeEnum;
import com.tuoheng.admin.enums.TaskStatusEnum;
import com.tuoheng.admin.mapper.TenantMapper;
import com.tuoheng.admin.mapper.ThInspectionMapper;
import com.tuoheng.admin.mapper.ThMissionMapper;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
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;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class QueryDroneDataService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private ThMissionMapper missionMapper;

    @Autowired
    private ThInspectionMapper inspectionMapper;

    public JsonResult getDroneDataById(String airportId) {
        log.info("获取无人机数据信息, airportId={}", airportId);
        // 读取不同租户的机场平台url
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();
        this.check(airportId);
        ThMission mission = missionMapper.selectOne(new LambdaQueryWrapper<ThMission>()
                .eq(ThMission::getTenantId, tenantId)
                .eq(ThMission::getAirportId, airportId)
                .eq(ThMission::getStatus, TaskStatusEnum.FLIGHT.getCode())
                .eq(ThMission::getMark, MarkTypeEnum.VALID.getCode()));
        if (ObjectUtil.isNull(mission)) {
            log.info("获取无人机数据信息, 没有正在飞行的任务");
//            throw new ServiceException("没有正在飞行的任务");
            return JsonResult.success("没有正在飞行的任务");
        }
        Integer missionId;
        if (0 == mission.getEmergencyDataMissionId()) {
            missionId = mission.getId();
        } else {
            missionId = mission.getEmergencyDataMissionId();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", missionId);
        map.put("tenantId", tenantId);
        List<ThInspection> inspectionList = inspectionMapper.selectListByMissionId(map);
        if (CollectionUtil.isEmpty(inspectionList)) {
            log.info("获取无人机数据信息, 数据为空");
            return JsonResult.success("数据为空");
        }
        List<DroneDataVo> droneDataVoList = InspectionConverMapper.INSTANCE.fromInspectionListToDroneDataVoList(inspectionList);
        return JsonResult.success(droneDataVoList);
    }

    /**
     * 检查参数
     *
     * @param airportId
     * @return
     */
    private void check(String airportId) {
        if (StringUtils.isEmpty(airportId)) {
            log.info("获取无人机数据信息, 机场ID为空");
            throw new ServiceException("机场ID为空");
        }
    }
}