
package com.tmerclub.cloud.order.listener;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.order.constant.OrderExportError;
import com.tmerclub.cloud.order.service.PurchaseOrderExcelService;
import com.tmerclub.cloud.order.vo.UnDeliveryPurchaseOrderExcelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 待发货订单导入监听，对数据进行校验
 *
 * @author Pineapple
 * @date 2021/7/20 11:11
 */
public class PurchaseOrderExcelListener extends AnalysisEventListener<UnDeliveryPurchaseOrderExcelVO> {

    private static final Logger LOGGER = LoggerFactory.getLogger(PurchaseOrderExcelListener.class);
    private PurchaseOrderExcelService orderExcelService;
    private Map<Integer, List<String>> errorMap;
    private List<UnDeliveryPurchaseOrderExcelVO> list;
    private UnDeliveryPurchaseOrderExcelVO unDeliveryOrderExcel;

    private static final int BATCH_COUNT = 1000;
    private static String seq;

    public PurchaseOrderExcelListener() {

    }

    public PurchaseOrderExcelListener(PurchaseOrderExcelService orderExcelService, Map<Integer, List<String>> errorMap) {
        this.orderExcelService = orderExcelService;
        this.errorMap = errorMap;
        this.list = new ArrayList<>(Constant.INITIAL_CAPACITY);
    }

    /**
     * 每条数据解析时均会调用
     */
    @Override
    public void invoke(UnDeliveryPurchaseOrderExcelVO unDeliveryOrderExcelVO, AnalysisContext analysisContext) {
        if (Objects.isNull(unDeliveryOrderExcelVO.getSeq())) {
            if (Objects.isNull(unDeliveryOrderExcel)) {
                return;
            }
            unDeliveryOrderExcelVO.setSeq(unDeliveryOrderExcel.getSeq());
            unDeliveryOrderExcelVO.setPurchaseOrderId(unDeliveryOrderExcel.getPurchaseOrderId());
            unDeliveryOrderExcelVO.setDeliveryType(unDeliveryOrderExcel.getDeliveryType());
            unDeliveryOrderExcelVO.setDeliveryCompanyName(unDeliveryOrderExcel.getDeliveryCompanyName());
            unDeliveryOrderExcelVO.setDeliveryNo(unDeliveryOrderExcel.getDeliveryNo());
        }
        boolean isSave = Objects.nonNull(seq) && !Objects.equals(seq, unDeliveryOrderExcelVO.getSeq()) && list.size() > BATCH_COUNT;
        if (isSave) {
            saveData();
        }
        seq = unDeliveryOrderExcelVO.getSeq();
        unDeliveryOrderExcel = unDeliveryOrderExcelVO;
        list.add(unDeliveryOrderExcelVO);
    }

    /**
     * 所有数据解析完成调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        if (CollUtil.isEmpty(list)) {
            return;
        }
        try {
            orderExcelService.exportOrderExcel(list, errorMap);
        } catch (Exception e) {
            List<String> errorList = errorMap.get(OrderExportError.OTHER.value());
            if (CollUtil.isEmpty(errorList)) {
                errorList = new ArrayList<>(Constant.INITIAL_CAPACITY);
                errorMap.put(OrderExportError.OTHER.value(), errorList);
            }
            errorList.add(e.getMessage());
        }
        list.clear();
    }
}
