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

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.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.request.warning.QueryWarningListRequest;
import com.tuoheng.admin.entity.vo.warning.WarningListVo;
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.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;

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

/**
 * 查询预警业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-22
 */
@Slf4j
@Service
public class QueryWarningListService {

    @Autowired
    private WarningMapper warningMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private ThMissionMapper missionMapper;

    public JsonResult getList(QueryWarningListRequest request) {
//        log.info("进入查询预警列表业务");
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();
        request.setTenantId(tenantId);

        JsonResult result = this.check(tenantId, request);
        if (0 != result.getCode()) {
            log.info("进入查询预警列表业务：校验失败：{}", result.getMsg());
            return result;
        }
        List<Warning> warningList = warningMapper.getList(request);
        if (CollectionUtil.isEmpty(warningList)) {
            log.info("预警列表数据为空");
        }

        // 构造返回结果对象
        List<WarningListVo> warningListVoList = this.buildWarningListVo(warningList);

        return JsonResult.success(warningListVoList);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param request
     * @return
     */
    private JsonResult check(Integer tenantId, QueryWarningListRequest request) {

        return JsonResult.success();
    }

    /**
     *  1）、完善经纬度坐标字段
     *
     * @param warningList
     * @return
     */
    private List<WarningListVo> buildWarningListVo(List<Warning> warningList) {
        List<WarningListVo> warningListVoList = WarningConverMapper.INSTANCE.fromWarningListToWarningListVoList(warningList);
        Map<Integer, ThMission> missionMap = getMissionMap(warningList);
        Map<Integer, Question> questionMap = this.getQuestionMap( warningList);
        ThMission mission;
        Question question;
        for (WarningListVo warningListVo : warningListVoList) {
            mission = missionMap.get(warningListVo.getMissionId());
            if (ObjectUtil.isNotNull(mission)) {
                warningListVo.setAirportId(mission.getAirportId());
                warningListVo.setAirportName(mission.getAirportName());
                warningListVo.setInspectionLine(mission.getInspectionLine());
                warningListVo.setInspectionLineName(mission.getInspectionLineName());
            }
            question = questionMap.get(warningListVo.getQuestionId());
            if (ObjectUtil.isNotNull(question)) {
                warningListVo.setLng(question.getLng());
                warningListVo.setLat(question.getLat());
            }
        }
        return warningListVoList;
    }

    /**
     *  查询到的任务列表，放到map中
     *
     * @param warningList
     * @return
     */
    private Map<Integer, ThMission> getMissionMap(List<Warning> warningList) {
        Map<Integer, ThMission> map = new HashMap<>();
        List<Integer> missionIdList = warningList.stream().map(o -> o.getMissionId()).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(missionIdList)) {
            log.info("missionIdList为空");
            return null;
        }
        List<ThMission> missionList = missionMapper.selectList(new LambdaQueryWrapper<ThMission>()
                .in(ThMission::getId, missionIdList)
                .eq(ThMission::getMark, MarkEnum.VALID.getCode()));
        for (ThMission mission : missionList) {
            map.put(mission.getId(), mission);
        }
        return map;
    }

    /**
     *  查询到的问题列表，放到map中
     *
     * @param warningList
     * @return
     */
    private Map<Integer, Question> getQuestionMap(List<Warning> warningList) {
        Map<Integer, Question> map = new HashMap<>();
        List<Integer> questionIdList = warningList.stream().map(o -> o.getQuestionId()).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(questionIdList)) {
            log.info("questionIdList为空");
            return null;
        }
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                            .in(Question::getId, questionIdList)
                            .eq(Question::getMark, MarkEnum.VALID.getCode()));
        for (Question question : questionList) {
            map.put(question.getId(), question);
        }
        return map;
    }
}
