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 DishExcelListener extends AnalysisEventListener<DishExcel> {

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

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

    public DishExcelListener(Consumer<List<DishExcel>> successConsumer) {
        this.successConsumer = successConsumer;
    }

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


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

