package com.tuoheng.admin.service.inspection.update.status;

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.Business;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.entity.domain.LiveChannel;
import com.tuoheng.admin.entity.domain.Report;
import com.tuoheng.admin.entity.vo.DspCallbackRecordVo;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.entity.request.inspection.MissionStatusRequest;
import com.tuoheng.admin.service.third.dsp.IDspService;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JacksonUtil;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UpdateFlightStatusService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private LiveChannelMapper liveChannelMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private IDspService dspService;

    public JsonResult updateFlightStatus(Inspection inspection, MissionStatusRequest missionStatusRequest) {
        // 处理通道数据
        this.liveChannelHandle(inspection, missionStatusRequest);

        Inspection inspectionUpdate = new Inspection();
        inspectionUpdate.setId(inspection.getId());
        JsonResult result = dspService.startRecord(missionStatusRequest);
        if (0 != result.getCode()) {
            //如果调用DSP失败就直接失败！
            log.info("修改任务状态业务接口：调用DSP开始录制失败：{}", result.getMsg());
            inspectionUpdate.setStatus(InspectionStatusEnum.FLIGHT_FAILED.getCode());
            inspectionUpdate.setExecutionStartTime(DateUtils.now());
            inspectionMapper.updateById(inspectionUpdate);

            // 修改报告状态和开始时间
            this.updateReport(inspection);

            return result;
        }
        log.info("调用DSP开始录制, 返回：result={}", result);

        DspCallbackRecordVo vo = (DspCallbackRecordVo) result.getData();

        log.info("修改inspection status 开始");
        inspectionUpdate.setStatus(InspectionStatusEnum.IN_FLIGHT.getCode());
        inspectionUpdate.setExecutionStartTime(DateUtils.now());
        inspectionUpdate.setUpdateTime(DateUtils.now());
        log.info("inspectionUpdate：{}", inspectionUpdate);
        Integer count = inspectionMapper.updateStatus(inspectionUpdate);
        if (count <= 0) {
            log.info("修改inspection status 失败");
        } else {
            log.info("修改inspection status 成功");
        }
        log.info("修改inspection status 结束");

        // 将请求id和业务id消息存入数据库，用于监听获取任务信息
        this.businessHandle(inspection, vo);

        // 修改报告状态和开始时间
        this.updateReport(inspection);

        return JsonResult.success();
    }

    /**
     * 处理通道数据
     *
     * @param inspection
     * @param missionStatusRequest
     */
    private void liveChannelHandle(Inspection inspection, MissionStatusRequest missionStatusRequest) {
        log.info("进入处理通道数据方法");
        //  通道表新增
        LiveChannel liveChannel = this.buildLiveChannel(inspection, missionStatusRequest);
        // 查验是否存在
        LiveChannel liveChannelQuery = liveChannelMapper.selectOne(new LambdaQueryWrapper<LiveChannel>()
                .eq(LiveChannel::getInspectionId, inspection.getId())
                .eq(LiveChannel::getMark, MarkEnum.VALID.getCode()));
        if (liveChannelQuery == null) {
            // 添加通道使用记录
            liveChannel.setCreateTime(DateUtils.now());
            liveChannelMapper.insert(liveChannel);
        } else {
            // 更新通道使用记录
            liveChannel.setId(liveChannelQuery.getId());
            liveChannelMapper.updateById(liveChannel);
        }
    }

    private LiveChannel buildLiveChannel(Inspection inspection, MissionStatusRequest missionStatusRequest) {
        LiveChannel liveChannel = new LiveChannel();
        liveChannel.setTenantId(inspection.getTenantId());
        liveChannel.setInspectionId(inspection.getId());
        liveChannel.setChannelCode("0");
        liveChannel.setAipushUrl("");
        liveChannel.setAipullUrl("");
        liveChannel.setPushUrl(missionStatusRequest.getPushUrl());
        liveChannel.setPullUrl(missionStatusRequest.getPlayUrl());
        liveChannel.setPlayUrl(missionStatusRequest.getPullUrl());
        return liveChannel;
    }

    private void businessHandle(Inspection inspection, DspCallbackRecordVo dspCallbackRecordVo) {
        // 将请求id和业务id消息存入数据库，用于监听获取任务信息
        Business business = new Business();
        business.setTenantId(inspection.getTenantId());
        business.setMsgId(dspCallbackRecordVo.getRequestId());
        business.setType(1);
        business.setTypeId(inspection.getId());
        business.setCreateTime(DateUtils.now());
        businessMapper.insert(business);
    }

    private void updateReport(Inspection inspection) {
        Report report = reportMapper.selectOne(Wrappers.<Report>lambdaQuery()
                .eq(Report::getInspectionId, inspection.getId())
                .eq(Report::getMark, MarkEnum.VALID.getCode()));
        if (ObjectUtil.isNull(report)) {
            log.info("该任务还没有生成报告，不需要修改报告的完成时间和状态");
            return;
        }
        report.setStatus(inspection.getStatus());
        report.setExecutionStartTime(inspection.getExecutionStartTime());
        reportMapper.updateById(report);
    }
}
