package cn.reghao.tnb.content.app.vod.service.rcmd;

import cn.reghao.tnb.content.app.util.redis.ds.RedisSetStr;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 基于用户兴趣的推荐
 *
 * @author reghao
 * @date 2023-02-21 21:39:17
 */
@Service
public class UserInterestBased {
    private final RedisSetStr redisSetStr;

    public UserInterestBased(RedisSetStr redisSetStr) {
        this.redisSetStr = redisSetStr;
    }

    public void addUserInterests(long userId, String category) {
        redisSetStr.sadd(RedisKeys.getUserInterestKey(userId), category);
    }

    public void addCategoryItems(String category, String itemId) {
        redisSetStr.sadd(RedisKeys.getCategoryKey(category), itemId);
    }

    public void addCategoryItems1(String category, List<String> itemIds) {
        redisSetStr.sadd(RedisKeys.getCategoryKey(category), itemIds.toArray(new String[0]));
    }

    public Set<String> getUserInterestCategory(long userId) {
        return redisSetStr.smembers(RedisKeys.getUserInterestKey(userId));
    }

    public Set<String> getUserInterestItems(long userId) {
        Set<String> categories = getUserInterestCategory(userId);
        Set<String> keys = categories.stream()
                .map(RedisKeys::getCategoryKey)
                .collect(Collectors.toSet());
        return redisSetStr.sunion(keys);
    }
}
