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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.domain.DspServiceInst;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.entity.domain.LiveChannel;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionDetailsByIdCodeEnum;
import com.tuoheng.admin.enums.code.inspection.QueryVideoServiceEnum;
import com.tuoheng.admin.mapper.DspServiceInstMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.LiveChannelMapper;
import com.tuoheng.admin.entity.vo.inspection.InspectionDetailsByIdVo;
import com.tuoheng.admin.entity.vo.inspection.InspectionMessageVo;
import com.tuoheng.common.core.config.common.CommonConfig;
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;

/**
 * @Author ChengWang
 * @Date 2023/4/17
 */
@Service
@Slf4j
public class QueryInspectionDetailsService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private LiveChannelMapper liveChannelMapper;

    @Autowired
    private DspServiceInstMapper dspServiceInstMapper;

    public JsonResult detailsById(String inspectionId) {
        if(StringUtils.isEmpty(inspectionId)){
            return JsonResult.error(
                    QueryInspectionDetailsByIdCodeEnum.INSPECTION_ID_IS_NULL.getCode(),
                    QueryInspectionDetailsByIdCodeEnum.INSPECTION_ID_IS_NULL.getMsg());
        }
        //根据id查询对应的任务
        Inspection inspection = inspectionMapper.selectById(inspectionId);
        if(ObjectUtil.isNull(inspection)){
            return JsonResult.error(
                    QueryVideoServiceEnum.INSPECTION_IS_NOT_EXIST.getCode(),
                    QueryVideoServiceEnum.INSPECTION_IS_NOT_EXIST.getMsg());
        }
        InspectionDetailsByIdVo vo = new InspectionDetailsByIdVo();
        if(InspectionStatusEnum.IN_FLIGHT.getCode() == inspection.getStatus()){
            //任务飞行中
            LiveChannel liveChannel = liveChannelMapper.selectOne(new LambdaQueryWrapper<LiveChannel>()
                    .eq(LiveChannel::getInspectionId, inspectionId)
                    .eq(LiveChannel::getMark, MarkEnum.VALID.getCode()));
            if(ObjectUtil.isNull(liveChannel)){
                return JsonResult.error("直播通道数据为空");
            }
            if(StringUtils.isNotEmpty(liveChannel.getAipullUrl())){
                vo.setAipullUrl(liveChannel.getAipullUrl());
            }
        }
        if(InspectionStatusEnum.FLIGHT_COMPLETED.getCode() == inspection.getStatus()){
            if(StringUtils.isEmpty(inspection.getAiVideoUrl())){
                return JsonResult.success("回放视频url为空");
            }
            vo.setAiVideoUrl(CommonConfig.videoURL + inspection.getAiVideoUrl());
        }
        List<InspectionMessageVo> messageList = new ArrayList<>();
        InspectionMessageVo messageVo = new InspectionMessageVo();
        BeanUtils.copyProperties(inspection,messageVo);
        //算法实例名称
        if(StringUtils.isNotEmpty(inspection.getDspServiceInstId())){
            DspServiceInst dspServiceInst = dspServiceInstMapper.selectOne(Wrappers.<DspServiceInst>lambdaQuery()
                    .eq(DspServiceInst::getDspServiceInstId,inspection.getDspServiceInstId())
                    .eq(DspServiceInst::getMark,MarkEnum.VALID.getCode()));
            messageVo.setDspServiceName(dspServiceInst.getName());
        }
        messageList.add(messageVo);
        vo.setInspectionMegList(messageList);
        return JsonResult.success(vo);
    }
}
