package com.fs.course.service.impl;

import java.sql.Timestamp;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsUserVideo;
import com.fs.course.mapper.FsUserVideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserVideoViewMapper;
import com.fs.course.domain.FsUserVideoView;
import com.fs.course.service.IFsUserVideoViewService;

/**
 * 课堂视频观看记录Service业务层处理
 *
 * @author fs
 * @date 2024-05-15
 */
@Service
public class FsUserVideoViewServiceImpl implements IFsUserVideoViewService
{
    @Autowired
    private FsUserVideoViewMapper fsUserVideoViewMapper;
    @Autowired
    private FsUserVideoMapper fsUserVideoMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String WATCH_KEY_PREFIX = "watch:";

    /**
     * 查询课堂视频观看记录
     *
     * @param viewId 课堂视频观看记录主键
     * @return 课堂视频观看记录
     */
    @Override
    public FsUserVideoView selectFsUserVideoViewByViewId(Long viewId)
    {
        return fsUserVideoViewMapper.selectFsUserVideoViewByViewId(viewId);
    }

    /**
     * 查询课堂视频观看记录列表
     *
     * @param fsUserVideoView 课堂视频观看记录
     * @return 课堂视频观看记录
     */
    @Override
    public List<FsUserVideoView> selectFsUserVideoViewList(FsUserVideoView fsUserVideoView)
    {
        return fsUserVideoViewMapper.selectFsUserVideoViewList(fsUserVideoView);
    }

    /**
     * 新增课堂视频观看记录
     *
     * @param fsUserVideoView 课堂视频观看记录
     * @return 结果
     */
    @Override
    public int insertFsUserVideoView(FsUserVideoView fsUserVideoView)
    {
        fsUserVideoView.setCreateTime(DateUtils.getNowDate());
        return fsUserVideoViewMapper.insertFsUserVideoView(fsUserVideoView);
    }

    /**
     * 修改课堂视频观看记录
     *
     * @param fsUserVideoView 课堂视频观看记录
     * @return 结果
     */
    @Override
    public int updateFsUserVideoView(FsUserVideoView fsUserVideoView)
    {
        fsUserVideoView.setUpdateTime(DateUtils.getNowDate());
        return fsUserVideoViewMapper.updateFsUserVideoView(fsUserVideoView);
    }

    /**
     * 批量删除课堂视频观看记录
     *
     * @param viewIds 需要删除的课堂视频观看记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoViewByViewIds(Long[] viewIds)
    {
        return fsUserVideoViewMapper.deleteFsUserVideoViewByViewIds(viewIds);
    }

    /**
     * 删除课堂视频观看记录信息
     *
     * @param viewId 课堂视频观看记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserVideoViewByViewId(Long viewId)
    {
        return fsUserVideoViewMapper.deleteFsUserVideoViewByViewId(viewId);
    }

    @Override
    public R startWatchVideo(Long videoId, Long userId) {
        String key = WATCH_KEY_PREFIX + userId + ":" + videoId;
        redisTemplate.opsForValue().set(key, System.currentTimeMillis());
        return R.ok("开始观看视频");
    }

    @Override
    public R endWatchVideo(Long videoId, Long userId) {
        String key = WATCH_KEY_PREFIX + userId + ":" + videoId;
        Long startTime = (Long) redisTemplate.opsForValue().get(key);
        if (startTime != null) {
            long endTime = System.currentTimeMillis();
            long duration = (endTime - startTime) / 1000;

            FsUserVideoView videoView = new FsUserVideoView();
            videoView.setUserId(userId);
            videoView.setVideoId(videoId);
//            videoView.setStartTime(new Timestamp(startTime));
//            videoView.setEndTime(new Timestamp(endTime));
            videoView.setDuration(duration);
            videoView.setCreateTime(new Timestamp(System.currentTimeMillis()));
            fsUserVideoViewMapper.insertFsUserVideoView(videoView);
            fsUserVideoMapper.updateViews(videoId);
            redisTemplate.delete(key);
            return R.ok("观看视频结束").put("data", videoView);
        }
        return R.error("未开始观看视频");
    }
}
