package com.sckw.excel.easyexcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.sckw.excel.utils.ValidDataUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

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

/**
 * @author LengFaQiang
 * @create 2021-11-02
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ExcelImportListener extends AnalysisEventListener<Object> {
    /**
     * 成功的数据
     */
    private List<Object> dataList = new ArrayList<>();

    /**
     * 错误信息列表
     */
    private List<String> errorList = new ArrayList<>();

    @Override
    public void invoke(Object data, AnalysisContext context) {
        String validMsg = ValidDataUtil.valid(data);
        if (validMsg != null) {
            //+1行(实际中excel中第几行的数据)
            int rowIndex = context.readRowHolder().getRowIndex() + 1;
            String errorMsg = "第" + rowIndex + "行数据有误," + validMsg;
            errorList.add(errorMsg);
        } else {
            dataList.add(data);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}
