package com.fuyuanshen.equipment.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;

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

/**
 * 读取表头 监听器
 *
 * @author: 默苍璃
 * @date: 2025-11-1809:36
 */
@Slf4j
public class HeadValidateListener extends AnalysisEventListener<Map<Integer, String>> {

    private List<String> headNames = new ArrayList<>();


    /**
     * When analysis one row trigger invoke function.
     *
     * @param headMap one row value. It is same as {@link AnalysisContext#readRowHolder()}
     * @param context analysis context
     */
    @Override
    public void invoke(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("解析到一条数据: {}", JSON.toJSONString(headMap));
        // 按顺序收集表头名称
        for (int i = 0; i < headMap.size(); i++) {
            headNames.add(headMap.get(i));
        }
    }

    /**
     * if have something to do after all analysis
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 读取完成后不需要额外操作
    }


    public List<String> getHeadNames() {
        return headNames;
    }

}
