
package com.tmerclub.cloud.order.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.util.ConverterUtils;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.order.vo.PurchaseSpuExcelVO;

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

/**
 * 商品excel导入监听
 * 有个很重要的点 SpuExcelListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
 *
 * @author cl
 * @date 2021/5/14
 */

public class PurchaseExcelListener extends AnalysisEventListener<PurchaseSpuExcelVO> {

    private List<PurchaseSpuExcelVO> list;
    private List<String> partyCodeList;
    public static final String OTHER = "other";
    public static final String ERROR_ROWS = "error_rows";
    public static final String IMPORT_ROWS = "import_rows";

    public PurchaseExcelListener() {
    }

    public PurchaseExcelListener(Map<String, List<String>> errorMap, List<PurchaseSpuExcelVO> list) {
        errorMap.put(OTHER, new ArrayList<>(Constant.INITIAL_CAPACITY));
        errorMap.put(ERROR_ROWS, new ArrayList<>(Constant.INITIAL_CAPACITY));
        errorMap.put(IMPORT_ROWS, new ArrayList<>(Constant.INITIAL_CAPACITY));
        this.list = list;
        this.partyCodeList = new ArrayList<>(Constant.INITIAL_CAPACITY);
    }

    /**
     * 这个每一条数据解析都会来调用
     */

    @Override
    public void invoke(PurchaseSpuExcelVO userExcelDTO, AnalysisContext analysisContext) {
        if (StrUtil.isBlank(userExcelDTO.getPartyCode()) || partyCodeList.contains(userExcelDTO.getPartyCode())) {
            return;
        }
        partyCodeList.add(userExcelDTO.getPartyCode());
        if (StrUtil.isBlank(userExcelDTO.getSpuName())) {
            userExcelDTO.setSpuName("");
        }
        if (Objects.isNull(userExcelDTO.getPurchaseStock())) {
            userExcelDTO.setPurchaseStock(0);
        }
        list.add(userExcelDTO);
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

    /**
     * 获取excel表头数据
     *
     * @param headMap 表头数据
     * @param context 上下文
     */
    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        Map<Integer, String> strHeadMap = ConverterUtils.convertToStringMap(headMap, context);
        int row = context.readRowHolder().getRowIndex();
        // 合并表头检验
        if (Objects.equals(row, 0)) {
            if (!Objects.equals(strHeadMap.get(0), PurchaseSpuExcelVO.HEADER)) {
                throw new LuckException("导入模板错误，请下载最新模板！");
            }
            return;
        }
        // 表头检验
        if (Objects.equals(row, 1)) {
            if (!Objects.equals(strHeadMap.size(), PurchaseSpuExcelVO.HEADER_ARRAY.length)) {
                throw new LuckException("导入模板错误，请下载最新模板！");
            }
            for (Map.Entry<Integer, String> entry : strHeadMap.entrySet()) {
                if (!Objects.equals(entry.getValue(), PurchaseSpuExcelVO.HEADER_ARRAY[entry.getKey()])) {
                    throw new LuckException("导入模板错误，请下载最新模板！");
                }
            }
        }
    }

}
