package com.softwarebr.nunu.service;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @author ljr
 * @date 2020/2/28 17:10
 * @description: 处理查询关键词
 */
@Service
@Slf4j
public class SearchService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private static final String KEY_SEARCH_KEY_WORD = "KEY_SEARCH_KEY_WORD";

    /**
     * 增加关键词的搜索次数
     * @param keyWord
     */
    public void increasingScore(String keyWord){
        stringRedisTemplate.opsForZSet().incrementScore(KEY_SEARCH_KEY_WORD, keyWord, 1);
    }

    /**
     * 想要获取的关键词数量
     * @param length 5则为5个;输入0为所有
     */
    public List<String> getHotKeyWord(long length){
        Set<String> hotKeyWordSet = stringRedisTemplate.opsForZSet().reverseRange(KEY_SEARCH_KEY_WORD, 0, length - 1);
        List<String> keyWordList = Lists.newArrayList(hotKeyWordSet);
        return keyWordList;
    }
}
