package com.fs.live.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.spring.SpringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.live.vo.LiveWatchLogListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.live.mapper.LiveWatchLogMapper;
import com.fs.live.domain.LiveWatchLog;
import com.fs.live.service.ILiveWatchLogService;

/**
 * 直播看课记录Service业务层处理
 * 
 * @author fs
 * @date 2025-12-12
 */
@Service
public class LiveWatchLogServiceImpl extends ServiceImpl<LiveWatchLogMapper, LiveWatchLog> implements ILiveWatchLogService {

    @Autowired
    private LiveWatchLogMapper liveWatchLogMapper;
    
    private final RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
    
    private static final String LIVE_WATCH_LOG_CACHE_KEY = "live:watch:log:cache:%s"; // logId

    /**
     * 查询直播看课记录
     * 
     * @param logId 直播看课记录主键
     * @return 直播看课记录
     */
    @Override
    public LiveWatchLog selectLiveWatchLogByLogId(Long logId)
    {
        return baseMapper.selectLiveWatchLogByLogId(logId);
    }

    /**
     * 查询直播看课记录列表
     * 
     * @param liveWatchLog 直播看课记录
     * @return 直播看课记录
     */
    @Override
    public List<LiveWatchLog> selectLiveWatchLogList(LiveWatchLog liveWatchLog)
    {
        return baseMapper.selectLiveWatchLogList(liveWatchLog);
    }

    @Override
    public List<LiveWatchLogListVO> selectLiveWatchLogListInfo(LiveWatchLog liveWatchLog){

        return liveWatchLogMapper.selectLiveWatchLogListInfo(liveWatchLog);
    }

    /**
     * 新增直播看课记录
     * 
     * @param liveWatchLog 直播看课记录
     * @return 结果
     */
    @Override
    public int insertLiveWatchLog(LiveWatchLog liveWatchLog)
    {
        liveWatchLog.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertLiveWatchLog(liveWatchLog);
    }

    /**
     * 修改直播看课记录
     * 
     * @param liveWatchLog 直播看课记录
     * @return 结果
     */
    @Override
    public int updateLiveWatchLog(LiveWatchLog liveWatchLog)
    {
        liveWatchLog.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateLiveWatchLog(liveWatchLog);
    }

    /**
     * 批量删除直播看课记录
     * 
     * @param logIds 需要删除的直播看课记录主键
     * @return 结果
     */
    @Override
    public int deleteLiveWatchLogByLogIds(Long[] logIds)
    {
        return baseMapper.deleteLiveWatchLogByLogIds(logIds);
    }

    /**
     * 删除直播看课记录信息
     * 
     * @param logId 直播看课记录主键
     * @return 结果
     */
    @Override
    public int deleteLiveWatchLogByLogId(Long logId)
    {
        return baseMapper.deleteLiveWatchLogByLogId(logId);
    }

    /**
     * 批量更新直播看课记录
     * @param liveWatchLogs 需要更新的直播看课记录列表
     * @return 更新的记录数
     */
    @Override
    public int batchUpdateLiveWatchLog(List<LiveWatchLog> liveWatchLogs) {
        if (liveWatchLogs == null || liveWatchLogs.isEmpty()) {
            return 0;
        }
        Date now = DateUtils.getNowDate();
        // 设置统一的更新时间
        for (LiveWatchLog log : liveWatchLogs) {
            if (log.getUpdateTime() == null) {
                log.setUpdateTime(now);
            }
        }
        int result = baseMapper.batchUpdateLiveWatchLog(liveWatchLogs);
        
        // 更新后清除相关缓存
        for (LiveWatchLog log : liveWatchLogs) {
            if (log.getLogId() != null) {
                String cacheKey = String.format(LIVE_WATCH_LOG_CACHE_KEY, log.getLogId());
                redisCache.deleteObject(cacheKey);
            }
        }
        
        return result;
    }

    /**
     * 查询直播看课记录并缓存1小时
     * @param logId 直播看课记录主键
     * @return 直播看课记录
     */
    @Override
    public List<LiveWatchLog> selectLiveWatchLogByLogIdWithCache(LiveWatchLog logId) {
        if (logId == null) {
            return null;
        }
        
        // 先从缓存中获取
        String cacheKey = String.format(LIVE_WATCH_LOG_CACHE_KEY, logId);
        List<LiveWatchLog> cachedLog = redisCache.getCacheObject(cacheKey);
        if (cachedLog != null) {
            return cachedLog;
        }
        
        // 缓存中没有，从数据库查询
        List<LiveWatchLog> log = baseMapper.selectLiveWatchLogList(logId);
        if (log != null) {
            // 将查询结果缓存1小时
            redisCache.setCacheObject(cacheKey, log, 1, TimeUnit.HOURS);
        }
        
        return log;
    }
}
