package com.prolog.eis.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.prolog.eis.dto.shelves.PalletImportDTO;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghan
 */
public class PalletListener extends AnalysisEventListener<PalletImportDTO> {

    private static final Logger logger = LoggerFactory.getLogger(PalletListener.class);

    private final List<PalletImportDTO> data = new ArrayList<>();


    @Override
    public void invoke(PalletImportDTO palletImportDto, AnalysisContext analysisContext) {
        logger.info("解析到一条数据:{}", JSON.toJSONString(palletImportDto));
        data.add(palletImportDto);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        logger.info("所有数据解析完成");
    }

    @SneakyThrows
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        logger.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
        List<String> heads = new ArrayList<>();
        heads.add("货架号");
        heads.add("模板类型");
        heads.add("点位");
        heads.add("角度");
        for (String str : headMap.values()) {
            if (!heads.contains(str)) {
                throw new IOException("当前文档与模板不匹配!");
            }
        }
    }
}