package com.hys.app.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.util.PageConvert;
import com.hys.app.mapper.system.SensitiveWordsMapper;
import com.hys.app.model.base.message.SensitiveWordsMsg;
import com.hys.app.model.base.dos.SensitiveWords;
import com.hys.app.framework.redis.RedisChannel;
import com.hys.app.framework.util.DateUtil;
import com.hys.app.service.system.SensitiveWordsManager;
import com.hys.app.model.errorcode.SystemErrorCode;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.framework.util.JsonUtil;
import com.hys.app.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 敏感词业务类
 *
 * @author fk
 * @version v1.0
 * @since v7.0.0
 * 2018-08-02 11:30:59
 */
@Service
public class SensitiveWordsManagerImpl implements SensitiveWordsManager {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SensitiveWordsMapper sensitiveWordsMapper;

    /**
     * 查询敏感词列表
     * @param page 页码
     * @param pageSize 每页数量
     * @param keyword
     * @return WebPage
     */
    @Override
    public WebPage list(long page, long pageSize, String keyword) {

        QueryWrapper<SensitiveWords> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",1);
        wrapper.like(keyword != null,"word_name",keyword);
        IPage<SensitiveWords> iPage = sensitiveWordsMapper.selectPage(new Page<>(page,pageSize), wrapper);
        return PageConvert.convert(iPage);

    }

    /**
     * 添加敏感词
     * @param sensitiveWords 敏感词
     * @return SensitiveWords 敏感词
     */
    @Override
    @Transactional(value = "systemTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SensitiveWords add(SensitiveWords sensitiveWords) {

        //不能重复添加
        QueryWrapper<SensitiveWords> wrapper = new QueryWrapper<>();
        wrapper.eq("word_name", sensitiveWords.getWordName()).eq("is_delete", 1);
        List list = sensitiveWordsMapper.selectList(wrapper);

        if (StringUtil.isNotEmpty(list)) {
            throw new ServiceException(SystemErrorCode.E928.code(), "敏感词语不能重复");
        }

        sensitiveWords.setIsDelete(1);
        sensitiveWords.setCreateTime(DateUtil.getDateline());

        sensitiveWordsMapper.insert(sensitiveWords);

        //将敏感词发送消息
        SensitiveWordsMsg msg = new SensitiveWordsMsg(sensitiveWords.getWordName(), SensitiveWordsMsg.ADD);
        redisTemplate.convertAndSend(RedisChannel.SENSITIVE_WORDS, JsonUtil.objectToJson(msg));

        return sensitiveWords;
    }

    /**
     * 修改敏感词
     * @param sensitiveWords 敏感词
     * @param id 敏感词主键
     * @return SensitiveWords 敏感词
     */
    @Override
    @Transactional(value = "systemTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SensitiveWords edit(SensitiveWords sensitiveWords, Long id) {
        sensitiveWordsMapper.updateById(sensitiveWords);
        return sensitiveWords;
    }

    /**
     * 删除敏感词
     * @param id 敏感词主键
     */
    @Override
    @Transactional(value = "systemTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void delete(Long id) {

        SensitiveWords model = this.getModel(id);

        QueryWrapper<SensitiveWords> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        model.setIsDelete(0);
        sensitiveWordsMapper.updateById(model);

        //将删除敏感词发送消息
        SensitiveWordsMsg msg = new SensitiveWordsMsg(model.getWordName(), SensitiveWordsMsg.DELETE);
        redisTemplate.convertAndSend(RedisChannel.SENSITIVE_WORDS, JsonUtil.objectToJson(msg));

    }

    /**
     * 获取敏感词
     * @param id 敏感词主键
     * @return SensitiveWords  敏感词
     */
    @Override
    public SensitiveWords getModel(Long id) {
        return sensitiveWordsMapper.selectById(id);
    }

    /**
     * 查询需要过滤的敏感词汇
     * @return
     */
    @Override
    public List<String> listWords() {

        QueryWrapper<SensitiveWords> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 1);
        List<SensitiveWords> list = sensitiveWordsMapper.selectList(wrapper);

        List<String> words = new ArrayList<>();
        if (list != null) {

            for (SensitiveWords word : list) {
                words.add(word.getWordName());
            }
        }
        return words;
    }

}
