package jnpf.personnels.listeners;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class BaseEasyExcelReadListener<T, R> implements ReadListener<T> {

    private BaseEasyExcelCommonListener<T, R> baseEasyExcelCommonListener;

    public BaseEasyExcelReadListener(BaseEasyExcelCommonListener<T, R> baseEasyExcelCommonListener) {
        this.baseEasyExcelCommonListener = baseEasyExcelCommonListener;
    }

    /**
     * 最大行数
     */
    private static final int MAX_ERROR_COUNT = 1000;

    /**
     * sheet对应正常内容
     */
    private final List<T> normalData = new ArrayList<>();
    /**
     * sheet对应的异常内容
     */
    private final List<R> errorData = new ArrayList();

    private StringBuilder errorBuilder = new StringBuilder();

    @Override
    public void invoke(T data, AnalysisContext context) {
        baseEasyExcelCommonListener.dataValidation(context, data, normalData, errorData);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        baseEasyExcelCommonListener.saveDataToRedis(normalData, errorData);
    }

}
