package com.xhkjedu.slive.service.recordserver;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.slive.mapper.liveplay.CourseSectionPlaybackMapper;
import com.xhkjedu.slive.mapper.recordserver.RecordserverMapper;
import com.xhkjedu.slive.model.liveplay.LCourseSectionPlayback;
import com.xhkjedu.slive.model.recordserver.LRecordserver;
import com.xhkjedu.slive.vo.liveplay.RecordserverVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname RecordserverService
 * @description todo
 * @date 2022/12/2 9:53
 **/
@Service
public class RecordserverService {
    @Resource
    private RecordserverMapper recordserverMapper;
    @Resource
    private CourseSectionPlaybackMapper courseSectionPlaybackMapper;

    //添加
    public void save(LRecordserver rs) {
        int timestamp = N_Utils.getSecondTimestamp();
        rs.setCreatetime(timestamp);
        rs.setHandleid(rs.getCreateid());
        rs.setHandletime(timestamp);
        recordserverMapper.insertSelective(rs);
    }

    //修改
    public void update(LRecordserver rs) {
        rs.setHandletime(N_Utils.getSecondTimestamp());
        recordserverMapper.update(rs);
    }

    //删除
    public void deleteById(Integer rsid) {
        recordserverMapper.deleteByPrimaryKey(rsid);
    }

    //列表
    public List<Map> findAll(LRecordserver rs) {
        return recordserverMapper.findAll(rs);
    }

    //更改状态
    public void updateStateById(LRecordserver rs) {
        rs.setHandletime(N_Utils.getSecondTimestamp());
        recordserverMapper.updateStateById(rs);
    }

    //开始录制
    public ResultVo start(RecordserverVo rs) {
        Integer sectionid = rs.getSectionid();
        LRecordserver server = recordserverMapper.getRspathBySectionid(sectionid);
        if (server == null) {
            server = recordserverMapper.getPath();
            if (server == null) return new ResultVo(1, "暂无可用录制服务");
        }
        RecordserverVo recordserverVo = recordserverMapper.getCloudCodeAndSchoolIdByTid(rs.getTid());
        rs.setCloudcode(recordserverVo.getCloudcode());
        rs.setSchoolid(recordserverVo.getSchoolid());
        String rspath = server.getRspath();
        String param = JSON.toJSONString(rs);
        //服务是否可用
        ResultVo vo = sendJsonPost(rspath + "/recorder/v1/status", "");
        if (vo.getCode() != 0) {
            //服务不可用关闭服务地址
            server.setRsstate(0);
            updateStateById(server);

            //重新获取可用地址
            server = recordserverMapper.getPath();
            if (server == null) return new ResultVo(1, "暂无可用录制服务");
            rspath = server.getRspath();

            //服务是否可用
            vo = sendJsonPost(rspath + "/recorder/v1/status", "");
            if (vo.getCode() != 0) {
                server.setRsstate(0);
                updateStateById(server);
                return new ResultVo(1, "服务不可用");
            }

            //服务可用开始录制
            vo = sendJsonPost(rspath + "/recorder/v1/start", param);
        } else {
            //服务可用开始录制
            vo = sendJsonPost(rspath + "/recorder/v1/start", param);
        }
        //开始录制失败直接返回信息
        if (vo.getCode() != 0) return vo;

        //保存回放基础信息
        LCourseSectionPlayback playback = new LCourseSectionPlayback();
        playback.setSectionid(sectionid);
        int timestamp = N_Utils.getSecondTimestamp();
        playback.setBegintime(timestamp);
        playback.setCreatetime(timestamp);
        playback.setRn(0);
        playback.setDn(0);
        Integer rsid = server.getRsid();
        playback.setRsid(rsid);
        playback.setSid(vo.getObj().toString());
        courseSectionPlaybackMapper.insertUseGeneratedKeys(playback);

        return vo;
    }

    //发送请求
    private ResultVo sendJsonPost(String url, String param) {
        String result = N_Utils.sendJsonPost(url, param);
        if (N_Utils.isEmpty(result)) return new ResultVo(1, "发送请求失败");
        ResultVo vo = JSON.parseObject(result, ResultVo.class);
        return vo;
    }

    //停止录制
    public ResultVo stop(RecordserverVo rs) {
        String rspath = recordserverMapper.getPathById(rs.getSid());//根据sid获取地址
        if (N_Utils.isEmpty(rspath)) return new ResultVo(1, "获取服务地址出错");
        String param = JSON.toJSONString(rs);
        //停止录制
        ResultVo vo = sendJsonPost(rspath + "/recorder/v1/stop", param);
        return vo;
    }

    //是否有录制
    public ResultVo hasRecorder(RecordserverVo rs) {
        String rspath = recordserverMapper.getPathById(rs.getSid());//根据sid获取地址
        if (N_Utils.isEmpty(rspath)) return new ResultVo(1, "获取服务地址出错");
        String param = JSON.toJSONString(rs);
        //是否有录制
        ResultVo vo = sendJsonPost(rspath + "/recorder/v1/has_recorder", param);
        return vo;
    }
}
