
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.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.order.vo.PurchaseOrderInboundExcelVO;

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 PurchaseOrderInboundExcelListener extends AnalysisEventListener<PurchaseOrderInboundExcelVO> {

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

    public PurchaseOrderInboundExcelListener() {
    }

    public PurchaseOrderInboundExcelListener(Map<String, List<String>> errorMap, List<PurchaseOrderInboundExcelVO> 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;
    }

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

    @Override
    public void invoke(PurchaseOrderInboundExcelVO userExcelDTO, AnalysisContext analysisContext) {
        if (StrUtil.isBlank(userExcelDTO.getPartyCode()) && Objects.isNull(userExcelDTO.getPurchaseStock())) {
            return;
        }
        if (StrUtil.isBlank(userExcelDTO.getSpuName())) {
            userExcelDTO.setSpuName("");
        }
        if (Objects.isNull(userExcelDTO.getActualStock())) {
            userExcelDTO.setActualStock(0);
        }
        list.add(userExcelDTO);
    }

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

}
