package com.tuoheng.admin.service.accident.index;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.utils.CommonUtils;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.utils.GaodeUtil;
import com.tuoheng.admin.vo.AccidentInspectionVo;
import com.tuoheng.admin.vo.InspectionInfoVo;
import com.tuoheng.common.core.common.BaseEntity;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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 xiaoying
 * @Date 2023/4/14 9:59
 */
@Slf4j
@Service
public class QueryAccidentInfoService {

    @Autowired
    private AccidentMapper accidentMapper;
    @Autowired
    private AccidentInspectionMapper accidentInspectionMapper;
    @Autowired
    private InspectionMapper inspectionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private InspectionFileMapper inspectionFileMapper;
    @Autowired
    private DspServiceInstMapper dspServiceInstMapper;

    /**
     * 查询应急事故记录详情
     *
     * @param accidentId 事故id
     * @return
     */
    public JsonResult getInfoByAccidentId(String accidentId) {
        //查询事故表及其事故对应应急任务表单获取参数
        AccidentInspectionVo vo = new AccidentInspectionVo();
        //添加事故相关数据
        Accident accident = accidentMapper.selectById(accidentId);
        BeanUtils.copyProperties(accident, vo);
        User user = userMapper.selectById(accident.getCheckUser());
        if (StringUtils.isNotNull(user)) {
            vo.setCheckUser(user.getRealname());

        }
        vo.setOccurrenceTime(accident.getCreateTime());
        //JSONObject gaodeCoordinate = GaodeUtil.getGaodeCoordinate(accident.getLongitude(), accident.getLatitude());
        //vo.setAdress(GaodeUtil.getGaodeAddress(gaodeCoordinate.get("longitude").toString(), gaodeCoordinate.get("latitude").toString()));
        vo.setAdress(accident.getLegName());
        //添加事故对应任务相关数据
        List<AccidentInspection> accidentInspections = accidentInspectionMapper.selectList(Wrappers.<AccidentInspection>lambdaQuery()
                .eq(AccidentInspection::getAccidentId, accidentId)
                .eq(AccidentInspection::getTenantId, CurrentUserUtil.getTenantId())
                .eq(BaseEntity::getMark, MarkEnum.VALID.getCode()));
        if (CollectionUtil.isEmpty(accidentInspections)) {
            return JsonResult.success(vo);
        }

        List<InspectionInfoVo> inspectionInfoVos = new ArrayList<>();
        //支持一条应急事故对应多条任务
        for (AccidentInspection accidentInspection : accidentInspections) {
            Inspection inspection = inspectionMapper.selectById(accidentInspection.getInspectionId());
            if (StringUtils.isNull(inspection)) {
                return JsonResult.error(ServiceExceptionEnum.TASK_DOES_NOT_EXIST.getMsg());
            }
            if (2 == inspection.getType() && StringUtils.isNotEmpty(inspection.getEmergencyDataInspectionId())) {
                inspection = inspectionMapper.selectById(inspection.getEmergencyDataInspectionId());
            }
            InspectionInfoVo inspectionInfoVo = new InspectionInfoVo();
            BeanUtils.copyProperties(inspection, inspectionInfoVo);
            inspectionInfoVo.setProcessingRecord(accidentInspection.getName());
            DspServiceInst dspServiceInst = dspServiceInstMapper.selectOne(Wrappers.<DspServiceInst>lambdaQuery()
                    .eq(DspServiceInst::getDspServiceInstId, inspection.getDspServiceInstId())
                    .eq(BaseEntity::getMark, MarkEnum.VALID.getCode()));
            if (ObjectUtil.isNotEmpty(dspServiceInst)) {
                inspectionInfoVo.setDspServiceInstName(dspServiceInst.getName());
            }
            //拼接域名
            if (StringUtils.isNotEmpty(inspection.getAiVideoUrl())) {
                inspectionInfoVo.setAiVideoUrl(CommonConfig.videoURL + inspection.getAiVideoUrl());
            }
            if (StringUtils.isNotEmpty(inspection.getVideoUrl())) {
                inspectionInfoVo.setVideoUrl(CommonConfig.videoURL + inspection.getVideoUrl());
            }
            //查询任务对应识别问题记录
            List<InspectionFile> inspectionFiles = inspectionFileMapper.selectList(Wrappers.<InspectionFile>lambdaQuery()
                    .eq(InspectionFile::getTenantId, CurrentUserUtil.getTenantId())
                    .eq(InspectionFile::getInspectionId, accidentInspection.getInspectionId())
                    .eq(BaseEntity::getMark, MarkEnum.VALID.getCode()));
            if (StringUtils.isNotEmpty(inspectionFiles)) {
                List<InspectionFile> fileRecordVos = inspectionFiles.stream().map(item -> {
                            item.setFileImage(CommonUtils.getImageURL(item.getFileImage()));
                            item.setFileOriginal(CommonUtils.getImageURL(item.getFileOriginal()));
                            return item;
                        }
                ).collect(Collectors.toList());
                inspectionInfoVo.setFileVoList(fileRecordVos);
            }
            inspectionInfoVos.add(inspectionInfoVo);
        }
        vo.setInspectionInfoVos(inspectionInfoVos);
        return JsonResult.success(vo);
    }

}
