package com.fs.live.service.impl;

import java.util.Collections;
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.live.vo.LiveUserFirstProfit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.live.mapper.LiveUserFirstEntryMapper;
import com.fs.live.domain.LiveUserFirstEntry;
import com.fs.live.service.ILiveUserFirstEntryService;

/**
 * 用户每日首次进入直播间记录Service业务层处理
 *
 * @author fs
 * @date 2025-09-04
 */
@Service
public class LiveUserFirstEntryServiceImpl implements ILiveUserFirstEntryService {
    @Autowired
    private LiveUserFirstEntryMapper baseMapper;
    
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询用户每日首次进入直播间记录
     *
     * @param id 用户每日首次进入直播间记录主键
     * @return 用户每日首次进入直播间记录
     */
    @Override
    public LiveUserFirstEntry selectLiveUserFirstEntryById(Long id)
    {
        return baseMapper.selectLiveUserFirstEntryById(id);
    }

    /**
     * 查询用户每日首次进入直播间记录列表
     *
     * @param liveUserFirstEntry 用户每日首次进入直播间记录
     * @return 用户每日首次进入直播间记录
     */
    @Override
    public List<LiveUserFirstEntry> selectLiveUserFirstEntryList(LiveUserFirstEntry liveUserFirstEntry)
    {
        return baseMapper.selectLiveUserFirstEntryList(liveUserFirstEntry);
    }

    /**
     * 新增用户每日首次进入直播间记录
     *
     * @param liveUserFirstEntry 用户每日首次进入直播间记录
     * @return 结果
     */
    @Override
    public int insertLiveUserFirstEntry(LiveUserFirstEntry liveUserFirstEntry)
    {
        liveUserFirstEntry.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertLiveUserFirstEntry(liveUserFirstEntry);
    }

    /**
     * 修改用户每日首次进入直播间记录
     *
     * @param liveUserFirstEntry 用户每日首次进入直播间记录
     * @return 结果
     */
    @Override
    public int updateLiveUserFirstEntry(LiveUserFirstEntry liveUserFirstEntry)
    {
        liveUserFirstEntry.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateLiveUserFirstEntry(liveUserFirstEntry);
    }

    /**
     * 批量删除用户每日首次进入直播间记录
     *
     * @param ids 需要删除的用户每日首次进入直播间记录主键
     * @return 结果
     */
    @Override
    public int deleteLiveUserFirstEntryByIds(Long[] ids)
    {
        return baseMapper.deleteLiveUserFirstEntryByIds(ids);
    }

    /**
     * 删除用户每日首次进入直播间记录信息
     *
     * @param id 用户每日首次进入直播间记录主键
     * @return 结果
     */
    @Override
    public int deleteLiveUserFirstEntryById(Long id)
    {
        return baseMapper.deleteLiveUserFirstEntryById(id);
    }

    @Override
    public int selectTodayEntry(long userId) {
        Date now = DateUtils.getNowDate();
        return baseMapper.selectTodayEntry(userId, now);
    }

    @Override
    public List<LiveUserFirstProfit> selectLiveProfitList() {
        return baseMapper.selectLiveProfitList();
    }

    @Override
    public LiveUserFirstEntry selectEntityByLiveIdUserId(long liveId,long userId) {
        return baseMapper.selectEntityByLiveIdUserId(liveId, userId);
    }

    @Override
    public LiveUserFirstEntry selectEntityByLiveIdUserIdWithCache(long liveId, long userId) {
        // Redis缓存键
        String cacheKey = "live:user:first:entry:" + liveId + ":" + userId;
        
        // 先从缓存中获取
        LiveUserFirstEntry cachedEntry = redisCache.getCacheObject(cacheKey);
        if (cachedEntry != null) {
            return cachedEntry;
        }
        
        // 缓存未命中，从数据库查询
        LiveUserFirstEntry entry = baseMapper.selectEntityByLiveIdUserId(liveId, userId);
        
        // 将查询结果存入缓存，缓存时间1小时
        if (entry != null) {
            redisCache.setCacheObject(cacheKey, entry, 1, TimeUnit.HOURS);
        }
        
        return entry;
    }
}
