package com.fs.live.service.impl;

import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.config.cloud.CloudHostProper;
import com.fs.hisStore.enums.LiveEnum;
import com.fs.live.domain.LiveVideo;
import com.fs.live.mapper.LiveVideoMapper;
import com.fs.live.service.ILiveVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 直播视频Service业务层处理
 *
 * @author fs
 * @date 2025-01-17
 */
@Service
public class LiveVideoServiceImpl implements ILiveVideoService
{
    @Autowired
    private LiveVideoMapper liveVideoMapper;

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CloudHostProper cloudHostProper;
    /**
     * 查询直播视频
     *
     * @param videoId 直播视频主键
     * @return 直播视频
     */
    @Override
    public LiveVideo selectLiveVideoByVideoId(Long videoId)
    {
        return liveVideoMapper.selectLiveVideoByVideoId(videoId);
    }

    /**
     * 查询企业直播视频
     * @param videoId       视频ID
     * @param companyId     企业ID
     * @param companyUserId 企业用户ID
     * @return LiveVideo
     */
    @Override
    public LiveVideo selectLiveVideoByVideoIdAndCompanyIdAndCompanyUserId(Long videoId, Long companyId, Long companyUserId)
    {
        return liveVideoMapper.selectLiveVideoByVideoIdAndCompanyIdAndCompanyUserId(videoId, companyId, companyUserId);
    }

    /**
     * 查询直播视频列表
     *
     * @param liveVideo 直播视频
     * @return 直播视频
     */
    @Override
    public List<LiveVideo> selectLiveVideoList(LiveVideo liveVideo)
    {
        return liveVideoMapper.selectLiveVideoList(liveVideo);
    }

    /**
     * 新增直播视频
     *
     * @param liveVideo 直播视频
     * @return 结果
     */
    @Override
    public int insertLiveVideo(LiveVideo liveVideo)
    {
//        if (LiveEnum.contains(cloudHostProper.getCompanyName())) {
//            liveVideo.setVideoUrl(liveVideo.getLineOne());
//            liveVideo.setFinishStatus(1);
//        }else {
            // 直播ID为-1，则新增 直播视频库
            liveVideo.setFinishStatus(0);
//        }

        if (liveVideo.getLiveId() == -1) {
            liveVideo.setCreateTime(DateUtils.getNowDate());
            return liveVideoMapper.insertLiveVideo(liveVideo);
        }
        List<LiveVideo> liveVideos = liveVideoMapper.selectByLiveIdAndType(liveVideo.getLiveId(),liveVideo.getVideoType());
        if (liveVideos!=null && !liveVideos.isEmpty()) {
            Date nowDate = DateUtils.getNowDate();
            liveVideos.forEach(liveVideoEntity -> {
                liveVideoEntity.setVideoType(liveVideo.getVideoType());
                liveVideoEntity.setDuration(liveVideo.getDuration());
                liveVideoEntity.setVideoUrl(liveVideo.getVideoUrl());
                liveVideoEntity.setUpdateTime(nowDate);
                liveVideoMapper.updateLiveVideo(liveVideoEntity);
            });
            return liveVideos.size();
        } else {
            liveVideo.setCreateTime(DateUtils.getNowDate());
            return liveVideoMapper.insertLiveVideo(liveVideo);
        }
    }

    /**
     * 修改直播视频
     *
     * @param liveVideo 直播视频
     * @return 结果
     */
    @Override
    public int updateLiveVideo(LiveVideo liveVideo)
    {
        liveVideo.setUpdateTime(DateUtils.getNowDate());
        return liveVideoMapper.updateLiveVideo(liveVideo);
    }

    /**
     * 批量删除直播视频
     *
     * @param videoIds 需要删除的直播视频主键
     * @return 结果
     */
    @Override
    public int deleteLiveVideoByVideoIds(Long[] videoIds)
    {
        return liveVideoMapper.deleteLiveVideoByVideoIds(videoIds);
    }

    /**
     * 删除直播视频信息
     *
     * @param videoId 直播视频主键
     * @return 结果
     */
    @Override
    public int deleteLiveVideoByVideoId(Long videoId)
    {
        return liveVideoMapper.deleteLiveVideoByVideoId(videoId);
    }

    @Override
    public List<LiveVideo> listByLiveId(Long liveId, Integer type) {
        return liveVideoMapper.selectByIdAndType(liveId, type);
    }

    /**
     * 根据直播ID查询视频
     * @param liveId 直播ID
     * @return liveVideo
     */
    @Override
    public LiveVideo selectLiveVideoByLiveId(Long liveId) {

        return liveVideoMapper.selectOne(liveId);
    }

    @Override
    public void saveOrUpdate(LiveVideo liveVideo) {
        if (liveVideo.getVideoId() == null) {
            liveVideoMapper.insertLiveVideo(liveVideo);
        } else {
            liveVideoMapper.updateLiveVideo(liveVideo);
        }
    }

    @Override
    public List<LiveVideo> selectLiveVideosByLiveId(Long existLiveId) {
        return liveVideoMapper.selectLiveVideosByLiveId(existLiveId);
    }

    @Override
    public LiveVideo selectLiveVideoByLiveIdAndType(Long id, int i) {
        List<LiveVideo> liveVideos = liveVideoMapper.selectByIdAndType(id, i);
        if (liveVideos != null && !liveVideos.isEmpty()) {
            return liveVideos.get(0);
        }
        return null;
    }

    @Override
    public void updateFinishStatus(String string) {
        liveVideoMapper.updateFinishStatus(string);
    }

    @Override
    public List<LiveVideo> listByLiveIdWithCache(Long liveId, Integer type) {
        // Redis缓存键
        String cacheKey = "live:video:list:" + liveId + ":" + type;

        // 先从缓存中获取
        List<LiveVideo> cachedVideos = redisCache.getCacheObject(cacheKey);
        if (cachedVideos != null && !cachedVideos.isEmpty()) {
            return cachedVideos;
        }

        // 缓存未命中，从数据库查询
        List<LiveVideo> videos = liveVideoMapper.selectByIdAndType(liveId, type);

        // 将查询结果存入缓存，缓存时间1小时
        if (videos != null) {
            redisCache.setCacheObject(cacheKey, videos, 1, TimeUnit.HOURS);
        }

        return videos;
    }

}
