package jnpf.personnels.listeners;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import jnpf.model.personnels.bo.FtbRosterImportConstants;
import jnpf.model.personnels.bo.FtbRosterImportHeadRuleBO;
import jnpf.model.personnels.vo.roster.FtbRosterCategoryVO;
import jnpf.personnels.service.FtbPersonnelsRosterValidService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 动态导入分析事件侦听器
 *
 * @author fantaibao
 * @date 2024/02/18
 */
@Slf4j
public class DynamicHeadAnalysisNewEventListener extends AnalysisEventListener<Map<Integer, String>> {

    private final FtbPersonnelsRosterValidService ftbPersonnelsRosterValidService;

    /**
     * 最大行数
     */
    private static final int MAX_ERROR_COUNT = 500;
    /**
     * 表头校验规则
     */
    private Map<Integer, FtbRosterImportHeadRuleBO> ftbRosterImportHeadRuleBOMap;
    /**
     * sheet对应正常内容
     */
    private final FtbRosterCategoryVO normalData = new FtbRosterCategoryVO();
    /**
     * sheet对应的异常内容
     */
    private final FtbRosterCategoryVO errorData = new FtbRosterCategoryVO();

    public DynamicHeadAnalysisNewEventListener(FtbPersonnelsRosterValidService ftbPersonnelsRosterValidService) {
        this.ftbPersonnelsRosterValidService = ftbPersonnelsRosterValidService;
    }


    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("解析到一条头数据：{}, currentRowHolder: {}", headMap.toString(), context.readRowHolder().getRowIndex());
        // 最大行数校验
        if (context.readSheetHolder().getApproximateTotalRowNumber() - 1 > MAX_ERROR_COUNT) {
            throw new RuntimeException("导入文件中sheet名为" + context.readSheetHolder().getSheetName() + "最大导入行数不能超过" + MAX_ERROR_COUNT);
        }
        Map<String, String> stringMap = headMap.entrySet().stream().filter(entry -> entry.getValue() != null)
                .collect(Collectors.toMap((k) -> k.getKey().toString(), Map.Entry::getValue));
        // 表头校验
        ftbRosterImportHeadRuleBOMap = ftbPersonnelsRosterValidService.headerVerification(context.readSheetHolder().getSheetName(), headMap);
        normalData.setHead(stringMap);
        normalData.setSheetIndex(context.readSheetHolder().getSheetNo());
        normalData.setClassificationName(context.readSheetHolder().getSheetName());
        normalData.setFtbRosterPageVOS(new ArrayList<>());

        errorData.setHead(new HashMap<>(stringMap));
        // 手动加错误选项
        errorData.getHead().put(String.valueOf(errorData.getHead().size() + 1), FtbRosterImportConstants.ERROR_REMARKS);
        errorData.setSheetIndex(context.readSheetHolder().getSheetNo());
        errorData.setClassificationName(context.readSheetHolder().getSheetName());
        errorData.setFtbRosterPageVOS(new ArrayList<>());
    }


    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        log.info("解析到一条数据：{}, currentRowIndex: {}----", data.toString(), context.readRowHolder().getRowIndex());
        ftbPersonnelsRosterValidService.dataValidation(context, ftbRosterImportHeadRuleBOMap, data, normalData, errorData);
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("本次sheet解析完成，存入Redis中");
        // 存入Redis
        ftbPersonnelsRosterValidService.saveDataToRedis(context, normalData, errorData);
    }

}

