package com.fs.live.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.base.BaseException;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.live.domain.LiveAnchor;
import com.fs.live.domain.LiveVideo;
import com.fs.live.mapper.LiveAnchorMapper;
import com.fs.live.service.ILiveVideoService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.live.mapper.LiveMapper;
import com.fs.live.domain.Live;
import com.fs.live.service.ILiveService;

/**
 * 直播Service业务层处理
 *
 * @author fs
 * @date 2025-01-17
 */
@Service
@AllArgsConstructor
public class LiveServiceImpl extends ServiceImpl<LiveMapper, Live> implements ILiveService
{
    private final LiveMapper liveMapper;
    private final ILiveVideoService liveVideoService;

    /**
     * 查询直播
     *
     * @param liveId 直播主键
     * @return 直播
     */
    @Override
    public Live selectLiveByLiveId(Long liveId){
        Live byId = getById(liveId);
        List<LiveVideo> videos = liveVideoService.listByLiveId(liveId, 1);
        if(!videos.isEmpty()){
            LiveVideo liveVideo = videos.get(0);
            byId.setVideoUrl(liveVideo.getVideoUrl());
            byId.setDuration(liveVideo.getDuration());
        }
        return byId;
    }

    /**
     * 查询直播列表
     *
     * @param live 直播
     * @return 直播
     */
    @Override
    public List<Live> selectLiveList(Live live)
    {
        return liveMapper.selectLiveList(live);
    }

    /**
     * 新增直播
     *
     * @param live 直播
     * @return 结果
     */
    @Override
    public int insertLive(Live live){
        if(live.getLiveType() == 2 && StringUtils.isEmpty(live.getVideoUrl())) {
            throw new BaseException("录播必须上传视屏");
        }
        live.setCreateTime(DateUtils.getNowDate());
        boolean save = save(live);
        LiveVideo liveVideo = new LiveVideo();
        liveVideo.setLiveId(live.getLiveId());
        liveVideo.setVideoUrl(live.getVideoUrl());
        liveVideo.setVideoType(1);
        liveVideo.setDuration(live.getDuration());
        liveVideoService.save(liveVideo);
        return save ? 1 : 0;
    }

    /**
     * 修改直播
     *
     * @param live 直播
     * @return 结果
     */
    @Override
    public int updateLive(Live live){
        live.setUpdateTime(DateUtils.getNowDate());
        List<LiveVideo> videos = liveVideoService.listByLiveId(live.getLiveId(), 1);
        if(!videos.isEmpty()){
            LiveVideo liveVideo = videos.get(0);
            liveVideo.setVideoUrl(live.getVideoUrl());
            liveVideo.setUpdateTime(DateUtils.getNowDate());
            liveVideo.setDuration(live.getDuration());
            liveVideoService.updateById(liveVideo);
        }
        return liveMapper.updateLive(live);
    }

    /**
     * 批量删除直播
     *
     * @param liveIds 需要删除的直播主键
     * @return 结果
     */
    @Override
    public int deleteLiveByLiveIds(Long[] liveIds){
        return liveMapper.deleteLiveByLiveIds(liveIds);
    }

    /**
     * 删除直播信息
     *
     * @param liveId 直播主键
     * @return 结果
     */
    @Override
    public int deleteLiveByLiveId(Long liveId)
    {
        return liveMapper.deleteLiveByLiveId(liveId);
    }
}
