package com.fzm.mall.server.admin.system.manager;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.config.Redis;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.system.entity.dto.SysSensitiveWordsDTO;
import com.fzm.mall.server.admin.system.mapper.SysSensitiveWordsMapper;
import com.fzm.mall.server.admin.system.model.SysSensitiveWords;
import com.fzm.mall.server.admin.util.BadWordUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wulixian
 * @since 2021/3/30
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysSensitiveWordsManager {

    private final SysSensitiveWordsMapper sensitiveWordsMapper;
    private final Redis redis;
    private final BadWordUtil badWordUtil;

    /**
     * 刷新redis中的敏感词
     */
    @Async
    public void flushSensitiveWords() {
        redis.del(MALLGlobalConfig.SENSITIVE_WORDS_REDIS_KEY);

        Set<String> wordsSet = new HashSet<>();
        List<SysSensitiveWords> list = sensitiveWordsMapper.selectList(new QueryWrapper<SysSensitiveWords>().eq("status", false));
        List<SysSensitiveWords> distinctList = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(o -> o.getSensitiveWords()))), ArrayList::new));
        distinctList.stream().forEach(item -> {
            wordsSet.add(item.getSensitiveWords());
        });
        badWordUtil.init(wordsSet);

        redis.setKey(MALLGlobalConfig.SENSITIVE_WORDS_REDIS_KEY, JSON.toJSONString(badWordUtil.sensitiveWordMap));
    }

    public void insert(SysSensitiveWords sensitiveWords) {
        sensitiveWordsMapper.insert(sensitiveWords);
    }

    public void update(SysSensitiveWords sensitiveWords, UpdateWrapper<SysSensitiveWords> wrapper) {
        sensitiveWordsMapper.update(sensitiveWords, wrapper);
    }

    public SysSensitiveWords selectOne(QueryWrapper<SysSensitiveWords> wrapper) {
        return sensitiveWordsMapper.selectOne(wrapper);
    }

    public IPage<SysSensitiveWordsDTO> page(Page<SysSensitiveWordsDTO> page, String sensitiveWords) {
        return sensitiveWordsMapper.page(page, sensitiveWords);
    }

    /**
     * 系统中心-敏感词-测试
     * @param text
     * @return
     */
    public Set<String> test(String text) {
        String redisStr = redis.getKey(MALLGlobalConfig.SENSITIVE_WORDS_REDIS_KEY);
        badWordUtil.setSensitiveWordMap(JSON.parseObject(redisStr, Map.class));
        return badWordUtil.getBadWord(text, BadWordUtil.maxMatchType);
    }
}
