package com.cm.plugin.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: SensitiveExcelListener
 * @Description: 敏感词导入
 * @Author: WangGeng
 * @Date: 2020/6/17 21:03
 * @Version: 1.0
 **/
public abstract class SensitiveWordsExcelListener extends AnalysisEventListener<SensitiveWordsExcel> {
    private static final Logger LOG = LoggerFactory.getLogger(SensitiveWordsExcelListener.class);
    private List<SensitiveWordsExcel> sensitiveWordsExcels = new ArrayList<>(0);

    @Override
    public void invoke(SensitiveWordsExcel sensitiveWordsExcel, AnalysisContext analysisContext) {
        sensitiveWordsExcels.add(sensitiveWordsExcel);
        if (sensitiveWordsExcels.size() > 500) {
            try {
                listSensitiveWordsExcel(sensitiveWordsExcels);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
            sensitiveWordsExcels.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        try {
            listSensitiveWordsExcel(sensitiveWordsExcels);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        sensitiveWordsExcels.clear();
    }

    /**
     * 获取敏感词
     *
     * @param sensitiveWordsExcels
     * @throws Exception
     */
    public abstract void listSensitiveWordsExcel(List<SensitiveWordsExcel> sensitiveWordsExcels) throws Exception;
}
