package cn.iocoder.yudao.module.member.controller.admin.excelImport.vo;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.function.Consumer;


/**
 * 读取excel数据
 */
public class UserExcelListener extends AnalysisEventListener<UserExcel> {

    /**临时存储正常数据集合,最大存储100*/
    private List<UserExcel> successDataList = Lists.newArrayListWithExpectedSize(300);

    /**自定义消费者函数接口用于自定义监听器中数据组装*/
    private final Consumer<List<UserExcel>> successConsumer;

    public UserExcelListener(Consumer<List<UserExcel>> successConsumer) {
        this.successConsumer = successConsumer;
    }

    @Override
    public void invoke(UserExcel userExcel, AnalysisContext analysisContext) {
        successDataList.add(userExcel);
        System.out.println("数据："+userExcel);
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (CollectionUtils.isNotEmpty(successDataList)) {
            successConsumer.accept(successDataList);
        }
    }
}

