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


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.AccidentEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.vo.accident.AccidentDetailsVo;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author ChengWang
 * @Date 2023/3/9
 */
@Service
public class QueryAccidentDetailsService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    /**
     * 事故详情
     * @param id
     * @return
     */
    public JsonResult accidentDetailsById(String id) {
        //校验
        if(StringUtils.isEmpty(id)){
            return JsonResult.error(AccidentEnum.ACCIDENT_ID_IS_NULL.getCode(),AccidentEnum.ACCIDENT_ID_IS_NULL.getMsg());
        }
        //根据事故id查询事故详情
        Accident accident = accidentMapper.selectById(id);
        if(ObjectUtils.isEmpty(accident)){
            return JsonResult.error(AccidentEnum.ACCIDENT_DATA_IS_NULL.getCode(),AccidentEnum.ACCIDENT_DATA_IS_NULL.getMsg());
        }
        AccidentDetailsVo vo = new AccidentDetailsVo();
        BeanUtils.copyProperties(accident,vo);
        //监管部门
        if(StringUtils.isNotEmpty(accident.getDeptId())){
            Dept dept = deptMapper.selectById(accident.getDeptId());
            if(dept.getName() !=null){
                vo.setDeptName(dept.getName());
            }
        }
        //任务机场名称 任务执行时间 任务结束时间 任务名称 任务现场视频
        this.getInspectionById(accident, vo);
        //所属公路路段范围
        this.getSectionById(accident, vo);
        //问题图片
        this.getInspectionFileById(accident, vo);

        return JsonResult.success(vo);
    }

    /**
     * 根据问题id查询相关属性
     * @param accident
     * @param vo
     */
    private void getInspectionFileById(Accident accident, AccidentDetailsVo vo) {
        if(StringUtils.isNotEmpty(accident.getInspectionFileId())){
            InspectionFile inspectionFile = inspectionFileMapper.selectById(accident.getInspectionFileId());
            if(inspectionFile.getFileThumbnail() !=null){
                vo.setFileThumbnail(CommonConfig.imageURL+inspectionFile.getFileThumbnail());
            }
            if(inspectionFile.getFileOriginal() !=null){
                vo.setFileOriginal(CommonConfig.imageURL+inspectionFile.getFileOriginal());
            }
            if(inspectionFile.getFileImage() !=null){
                vo.setFileImage(CommonConfig.imageURL+inspectionFile.getFileImage());
            }
        }
    }

    /**
     * 根据路段id查询路段相关属性
     * @param accident
     * @param vo
     */
    private void getSectionById(Accident accident, AccidentDetailsVo vo) {
        if(StringUtils.isNotEmpty(accident.getSectionId())){
            Section section = sectionMapper.selectById(accident.getSectionId());
            if(section.getSectionRange() !=null){
                vo.setSectionRange(section.getSectionRange());
            }
        }
    }

    /**
     * 根据任务id查询任务相关属性值
     * @param accident
     * @param vo
     */
    private void getInspectionById(Accident accident, AccidentDetailsVo vo) {
        if(StringUtils.isNotEmpty(accident.getInspectionId())){
            Inspection inspection = inspectionMapper.selectById(accident.getInspectionId());
            if(inspection.getAirportName() !=null){
                vo.setAirPortName(inspection.getAirportName());
            }
            if(inspection.getExecutionStartTime() !=null){
                vo.setExecutionStartTime(inspection.getExecutionStartTime());
            }
            if(inspection.getExecutionEndTime() !=null){
                vo.setExecutionEndTime(inspection.getExecutionEndTime());
            }
            if(inspection.getName() !=null){
                vo.setInspectionName(inspection.getName());
            }
            if(inspection.getAiVideoUrl() !=null){
                vo.setAiVideoUrl(CommonConfig.videoURL + inspection.getAiVideoUrl());
            }
        }
    }
}
