package com.fzm.mall.server.admin.system.service.impl;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.system.entity.dto.SysSensitiveWordsDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysSensitiveWordsSaveDTO;
import com.fzm.mall.server.admin.system.manager.SysSensitiveWordsManager;
import com.fzm.mall.server.admin.system.mapper.SysSensitiveWordsMapper;
import com.fzm.mall.server.admin.system.model.SysSensitiveWords;
import com.fzm.mall.server.admin.system.service.ISysSensitiveWordsService;
import com.fzm.mall.server.admin.util.CopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * <p>
 * 系统中心-敏感词 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-29
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysSensitiveWordsServiceImpl extends ServiceImpl<SysSensitiveWordsMapper, SysSensitiveWords> implements ISysSensitiveWordsService {

    private final SysSensitiveWordsManager sysSensitiveWordsManager;

    @Override
    public void save(SysSensitiveWordsSaveDTO sensitiveWordsSaveDTO) {
        SysSensitiveWords sensitiveWords = CopyUtil.to(sensitiveWordsSaveDTO, SysSensitiveWords.class);
        sensitiveWords.init();

        sysSensitiveWordsManager.insert(sensitiveWords);

        /**
         * 异步刷新缓存
         */
        sysSensitiveWordsManager.flushSensitiveWords();
    }

    @Override
    public void delete(String sensitiveWordsId) {
        UpdateWrapper<SysSensitiveWords> wrapper = new UpdateWrapper<>();
        wrapper.eq("sensitive_words_id", sensitiveWordsId)
                .set("status", true);

        sysSensitiveWordsManager.update(null, wrapper);

        /**
         * 异步刷新缓存
         */
        sysSensitiveWordsManager.flushSensitiveWords();
    }

    @Override
    public SysSensitiveWordsDTO read(String sensitiveWordsId) {
        SysSensitiveWords sensitiveWords = sysSensitiveWordsManager.selectOne(new QueryWrapper<SysSensitiveWords>().eq("sensitive_words_id", sensitiveWordsId));
        return CopyUtil.to(sensitiveWords, SysSensitiveWordsDTO.class);
    }

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

    /**
     * 系统中心-敏感词-测试
     * @param text
     * @return
     */
    @Override
    public Set<String> test(String text) {
        return sysSensitiveWordsManager.test(text);
    }
}
