package com.yifu.cloud.plus.v1.yifu.common.security.util.dict;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.fastjson.JSON;
import com.yifu.cloud.plus.v1.yifu.archives.vo.EmpFamilyExcelVO;
import lombok.extern.log4j.Log4j2;

import java.util.List;
import java.util.Map;

/**
 * @Author fxj
 * @Date 2022/6/23
 * @Description
 * @Version 1.0
 */
@Log4j2
public class  ExcelListenerDiy extends AnalysisEventListener<EmpFamilyExcelVO> {
	List<EmpFamilyExcelVO> res;

	public ExcelListenerDiy(List<EmpFamilyExcelVO> res){
		this.res = res;
	}
	//解析异常调用
	@Override
	public void onException(Exception exception, AnalysisContext context) throws Exception {
		log.error("解析失败，但是继续解析下一行:{}", exception.getMessage());
		if (exception instanceof ExcelDataConvertException) {
			ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;
			log.error("第{}行，第{}列解析异常，数据为:{}", excelDataConvertException.getRowIndex(),
					excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
		}
	}
	//解析表头
	@Override
	public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {

	}
	//解析每行数据调用
	@Override
	public void invoke(EmpFamilyExcelVO  data, AnalysisContext context) {
		ReadRowHolder readRowHolder = context.readRowHolder();
		Integer rowIndex = readRowHolder.getRowIndex();
		data.setRowIndex(rowIndex+1);
		res.add(data);
	}



	@Override
	public void extra(CellExtra extra, AnalysisContext context) {

	}
	//所有数据解析完成后调用
	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		log.info("所有数据解析完成！");
		log.info("解析数据:{}", JSON.toJSONString(res));
	}

}
