package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.LiveRoomVideoDao;
import com.yonge.cooleshow.biz.dal.entity.LiveRoom;
import com.yonge.cooleshow.biz.dal.entity.LiveRoomVideo;
import com.yonge.cooleshow.biz.dal.entity.RecordNotify;
import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
import com.yonge.cooleshow.biz.dal.service.LiveRoomVideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 直播视频记录(LiveRoomVideo)表服务实现类
 *
 * @author hgw
 * @since 2022-03-18 15:41:17
 */
@Service("liveRoomVideoService")
public class LiveRoomVideoServiceImpl extends ServiceImpl<LiveRoomVideoDao, LiveRoomVideo> implements LiveRoomVideoService {

    private final static Logger log = LoggerFactory.getLogger(LiveRoomVideoServiceImpl.class);

    @Autowired
    private LiveRoomService liveRoomService;

    @Override
    public LiveRoomVideoDao getDao() {
        return this.baseMapper;
    }

    @Override
    public void recordSync(RecordNotify recordNotify) {
        if (recordNotify.getCode().equals(200)) {
            //云端录制文件地址
            String fileUrl = recordNotify.getOutput().getFileUrl();
            //房间uid
            String roomUid = recordNotify.getRoomId();
            //融云唯一id标识
            String recordId = recordNotify.getRecordId();
            //回调事件类型
            Integer notifyType = recordNotify.getType();
            if (Objects.nonNull(notifyType)) {
                Date now = new Date();
                //notifyType 1: 录制开始；4: 文件上传-融云回调完成
                if (notifyType == 1) {
                    LiveRoomVideo video = new LiveRoomVideo();
                    LiveRoom room = liveRoomService.getOne(new QueryWrapper<LiveRoom>().lambda()
                            .eq(LiveRoom::getRoomUid, roomUid));
                    video.setCourseGroupId(room.getCourseGroupId());
                    video.setCourseId(room.getCourseId());
                    video.setRoomUid(roomUid);
                    video.setRecordId(recordId);
                    video.setUrl(fileUrl);
                    video.setStartTime(now);
                    video.setType(notifyType);
                    video.setCreatedTime(now);
                    this.save(video);
                }
                if (notifyType == 4) {
                    LiveRoomVideo video = this.getOne(Wrappers.<LiveRoomVideo>lambdaQuery()
                            .eq(LiveRoomVideo::getRoomUid, roomUid)
                            .eq(LiveRoomVideo::getRecordId, recordId));
                    video.setEndTime(now);
                    video.setType(notifyType);
                    this.updateById(video);
                }
            }
        }
    }

}

