package com.tuoheng.admin.service.third.dsp.callback;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.domain.*;
import com.tuoheng.admin.entity.request.third.DspCallbackRequest;
import com.tuoheng.admin.entity.request.third.DspRecordingCallbackRequest;
import com.tuoheng.admin.entity.request.third.QuestionFile;
import com.tuoheng.admin.enums.AiAnalyseTypeEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.service.inspection.update.flyer.DeliverStreamingService;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * DSP回调 服务实现类
 *
 * @author WangHaoran
 * @since 2021-09-01
 */
@Slf4j
@Service
public class DspRecordingCallbackService {

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private InspectionMapper inspectionMapper;

    /**
     * 保存DSP回调数据
     *
     * @param request 回调请求体
     * @return
     */
    public JsonResult callback(DspRecordingCallbackRequest request) {
        if (ObjectUtil.isEmpty(request)) {
            log.info("DSP服务回调地址接收成功, 数据为空");
            return JsonResult.success("DSP服务回调地址接收成功, 数据为空");
        }
        if (StringUtils.isEmpty(request.getRecordingVideoUrl())) {
            log.info("DSP服务回调地址接收成功, 录屏视频地址为空");
            return JsonResult.success("DSP服务回调地址接收成功, 录屏视频地址为空");
        }

        // 查询业务表数据
        Business business = businessMapper.selectOne(new LambdaQueryWrapper<Business>()
                .eq(Business::getMsgId, request.getRequestId())
                .eq(Business::getType, 1)
                .last("limit 1"));
        if (StringUtils.isNull(business)) {
            log.info("业务关系数据不存在，requestId：{}", request.getRequestId());
            return JsonResult.error("业务数据不存在");
        }
        if (StringUtils.isNull(business.getTypeId())) {
            log.info("业务关系数据，巡检任务ID不存在，typeId：{}", business.getTypeId());
            return JsonResult.error("业务关系数据，巡检任务ID不存在");
        }
        log.info("业务关系数据, business：{}", business);

        // 查询任务
        Inspection inspection = inspectionMapper.selectOne(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getId, business.getTypeId())
                .eq(Inspection::getTenantId, business.getTenantId()));
        if (ObjectUtil.isNull(inspection)) {
            log.info("任务不存在");
            return JsonResult.error("巡检任务不存在");
        }
        String videoUrl = "";
        if (request.getRecordingVideoUrl().contains(CommonConfig.videoURL)) {
            videoUrl = request.getRecordingVideoUrl().replace(CommonConfig.videoURL, "");
        } else {
            videoUrl = request.getRecordingVideoUrl();
        }
        inspection.setVideoUrl(videoUrl);

        Integer count = inspectionMapper.update(inspection);
        if (count <= 0) {
            log.info("dsp回调，修改视频地址失败");
        } else {
            log.info("dsp回调，修改视频地址成功");
        }
        return JsonResult.success();
    }

}
