package com.tuoheng.miniprogram.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import com.tuoheng.miniprogram.dao.InspectionMapper;
import com.tuoheng.miniprogram.dao.LiveChannelMapper;
import com.tuoheng.miniprogram.entity.Inspection;
import com.tuoheng.miniprogram.entity.LiveChannel;
import com.tuoheng.miniprogram.enums.InspectionStatusEnum;
import com.tuoheng.miniprogram.enums.MarkEnum;
import com.tuoheng.miniprogram.enums.code.inspection.QueryVideoServiceEnum;
import com.tuoheng.miniprogram.utils.CurrentUserUtil;
import com.tuoheng.miniprogram.vo.InspectionVideoVo;
import com.tuoheng.miniprogram.vo.LiveChannelVo;
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.List;

/**
 * 根据任务id查询视频直播和回放
 *
 * @Author ChengWang
 * @Date 2022/11/29
 */
@Service
@Slf4j
public class QueryVideoService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private LiveChannelMapper liveChannelMapper;

    public JsonResult getVideoById(String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryVideoServiceEnum.INSPECTION_ID_IS_NULL.getCode(), QueryVideoServiceEnum.INSPECTION_ID_IS_NULL.getMsg());
        }

        //获取租户id
        String tenantId = CurrentUserUtil.getTenantId();
        if (StringUtils.isEmpty(tenantId)) {
            return JsonResult.error("租户id为空");
        }

        //根据id查询对应的任务
        Inspection inspection = inspectionMapper.selectById(id);
        if (ObjectUtil.isNull(inspection)) {
            return JsonResult.error(QueryVideoServiceEnum.INSPECTION_IS_NOT_EXIST.getCode(), QueryVideoServiceEnum.INSPECTION_IS_NOT_EXIST.getMsg());

        }
        LiveChannelVo liveChannelVo = new LiveChannelVo();
        BeanUtils.copyProperties(inspection, liveChannelVo);
        //任务状态为任务飞行中10视频直播
        if (10 == inspection.getStatus()) {
            LiveChannel liveChannel = liveChannelMapper.selectOne(new LambdaQueryWrapper<LiveChannel>()
                    .eq(LiveChannel::getMark, 1)
                    .eq(LiveChannel::getInspectionId, inspection.getId())
                    .eq(LiveChannel::getStatus, 1));
            if (ObjectUtil.isNull(liveChannel)) {
                return JsonResult.error(QueryVideoServiceEnum.LIVE_CHANNEL_IS_NOT_EXIST.getCode(), QueryVideoServiceEnum.LIVE_CHANNEL_IS_NOT_EXIST.getMsg());
            }
            if (StringUtils.isEmpty(liveChannel.getAipullUrl())) {
                return JsonResult.error(QueryVideoServiceEnum.AIPULL_URL_IS_NOT_NULL.getCode(), QueryVideoServiceEnum.AIPULL_URL_IS_NOT_NULL.getMsg());
            }
            liveChannelVo.setAipullUrl(liveChannel.getAipullUrl());

        }

        return JsonResult.success(liveChannelVo);

    }
}
