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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.common.ServiceExceptionEnum;
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.domain.WarningRecord;
import com.tuoheng.admin.entity.vo.WarningDetailsVO;
import com.tuoheng.admin.entity.vo.WarningRecordVO;
import com.tuoheng.admin.enums.MarkTypeEnum;
import com.tuoheng.admin.enums.WarningEnum;
import com.tuoheng.admin.mapper.QuestionMapper;
import com.tuoheng.admin.mapper.ThMissionMapper;
import com.tuoheng.admin.mapper.WarningMapper;
import com.tuoheng.admin.mapper.WarningRecordMapper;
import com.tuoheng.common.config.CommonConfig;
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.mapper.UserMapper;
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预警详情业务处理层
 * @Author ChengWang
 * @Date 2023/2/8
 */
@Service
@Slf4j
public class WarningDetailsService {

    @Autowired
    private WarningMapper warningMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private WarningRecordMapper warningRecordMapper;

    @Autowired
    private ThMissionMapper missionMapper;

    @Autowired
    private UserMapper userMapper;

    public JsonResult details(Integer id) {
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();

        //校验
        if(null == id){
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        //根据id查询预警详情
        Warning warning = warningMapper.selectOne(Wrappers.<Warning>lambdaQuery()
                .eq(Warning::getId, id)
                .eq(Warning::getTenantId,tenantId)
                .eq(Warning::getMark, MarkTypeEnum.VALID.getCode()));
        if(ObjectUtils.isEmpty(warning)){
            return JsonResult.error(WarningEnum.WARNING_DATA_IS_NULL.getCode(),WarningEnum.WARNING_DATA_IS_NULL.getMsg());
        }
        WarningDetailsVO vo = new WarningDetailsVO();
        vo.setLocation(warning.getLocation());
        vo.setDiscoveryWay(warning.getDiscoveryWay());
        vo.setCreateTime(warning.getCreateTime());
        //当前预警记录对应的问题
        Question question = questionMapper.selectOne(Wrappers.<Question>lambdaQuery()
                .eq(Question::getId, warning.getQuestionId())
                .eq(Question::getMark, 1));
        if(ObjectUtils.isEmpty(question)){
            return JsonResult.error(WarningEnum.QUESTION_IS_NOT_EXIST.getCode(),WarningEnum.QUESTION_IS_NOT_EXIST.getMsg());
        }
        String fileOriginalUrl = question.getFileOriginalUrl();
        if(StringUtils.isNotEmpty(fileOriginalUrl)){
            vo.setFileOriginalUrl(fileOriginalUrl);
        }
        //火灾核实记录 一条预警对应多条火灾预警记录
        List<WarningRecordVO> warningRecordVo = new ArrayList<>();
        List<WarningRecord> warningRecords = warningRecordMapper.selectList(new LambdaQueryWrapper<WarningRecord>()
                .eq(WarningRecord::getWarningId, id).eq(WarningRecord::getMark, 1)
                .eq(WarningRecord::getTenantId, tenantId));
        if(CollectionUtil.isNotEmpty(warningRecords)){
            List<WarningRecordVO> list = warningRecords.stream().map(x -> {
                WarningRecordVO warningRecordVO = new WarningRecordVO();
                warningRecordVO.setRecordStartTime(x.getCreateTime());
                //巡检机场名称
                ThMission thMission = missionMapper.selectOne(new LambdaQueryWrapper<ThMission>()
                        .eq(ThMission::getId, x.getEmergencyMissionId())
                        .eq(ThMission::getMark, 1));
                if (ObjectUtils.isNotEmpty(thMission)) {
                    //获取巡检机场
                    String airportName = thMission.getAirportName();
                    if (StringUtils.isNotEmpty(airportName)) {
                        warningRecordVO.setAirportName(airportName);
                    }
                    //巡检任务id
                    Integer missionId = thMission.getId();
                    if(missionId != null){
                        warningRecordVO.setMissionId(missionId);
                    }
                    //巡检任务状态
                    Integer status = thMission.getStatus();
                    if(status != null){
                        warningRecordVO.setStatus(thMission.getStatus());
                    }
                }
                return warningRecordVO;

            }).collect(Collectors.toList());
            for (WarningRecordVO warningRecordVO : list) {
                warningRecordVo.add(warningRecordVO);
            }
        }
        vo.setWarningRecordVOList(warningRecordVo);
        //处理记录
        if(StringUtils.isNotEmpty(warning.getCheckResult())){
            vo.setCheckResult(warning.getCheckResult());
        }
        if(warning.getCheckUser() != null){
            User userInfo = userMapper.selectById(warning.getCheckUser());
            if(StringUtils.isNotEmpty(userInfo.getRealname())){
                vo.setCheckUser(userInfo.getRealname());
            }
        }
        if(warning.getCheckTime() != null){
            vo.setCheckTime(warning.getCheckTime());
        }

        return JsonResult.success(vo);
    }
}
