package com.tmerclub.cloud.product.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.product.constant.SpuExportError;
import com.tmerclub.cloud.product.service.SpuExcelService;
import com.tmerclub.cloud.product.vo.SpuExcelVO;
import com.tmerclub.cloud.product.vo.SupplierProdExcelVO;

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

/**
 * @author lanhai
 */
public class SupplierProdExcelListener extends AnalysisEventListener<SupplierProdExcelVO> {
    private SpuExcelService spuExcelService;
    private Map<Integer, List<String>> errorMap;
    private List<SupplierProdExcelVO> list;


    /**
     * 每隔1000条数据存储数据库，然后清理map ，方便内存回收
     */
    private static final int BATCH_COUNT = 3000;

    public SupplierProdExcelListener() {
    }

    public SupplierProdExcelListener(SpuExcelService spuExcelService, Map<Integer, List<String>> errorMap) {
        this.spuExcelService = spuExcelService;
        this.errorMap = errorMap;
        this.list = new ArrayList<>(Constant.INITIAL_CAPACITY);
    }

    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(SupplierProdExcelVO supplierProdExcelVO, AnalysisContext analysisContext) {

        boolean isSave = list.size() > BATCH_COUNT;
        if (isSave) {
            saveData();
        }
        list.add(supplierProdExcelVO);
    }

    private void saveData() {
        List<SpuExcelVO> listSpuExcelVO = new ArrayList<>(Constant.INITIAL_CAPACITY);
        try {
            if (CollUtil.isNotEmpty(list)) {
                listSpuExcelVO = list.stream().map(supplierProdExcelVO -> {
                    SpuExcelVO spuExcelVO = new SpuExcelVO();
                    spuExcelVO.setSeq(supplierProdExcelVO.getSeq());
                    spuExcelVO.setNameZh(supplierProdExcelVO.getNameZh());
                    spuExcelVO.setImgs(supplierProdExcelVO.getImgs());
                    spuExcelVO.setVideo(supplierProdExcelVO.getVideo());
                    spuExcelVO.setSellingPointZh(supplierProdExcelVO.getSellingPointZh());
                    spuExcelVO.setDetailZh(supplierProdExcelVO.getDetailZh());
                    spuExcelVO.setBrandName(supplierProdExcelVO.getBrandName());
                    spuExcelVO.setCategoryName(supplierProdExcelVO.getCategoryName());
                    spuExcelVO.setShopCategoryName(supplierProdExcelVO.getShopCategoryName());
                    spuExcelVO.setDeliveryMode(supplierProdExcelVO.getDeliveryMode());
                    spuExcelVO.setDeliveryTemplate(supplierProdExcelVO.getDeliveryTemplate());
                    spuExcelVO.setDeliveryAmount(supplierProdExcelVO.getDeliveryAmount());
                    spuExcelVO.setStatus(supplierProdExcelVO.getStatus());
                    spuExcelVO.setWeight(Objects.isNull(supplierProdExcelVO.getWeight()) ? 0.0 : supplierProdExcelVO.getWeight());
                    spuExcelVO.setVolume(Objects.isNull(supplierProdExcelVO.getVolume()) ? 0.0 : supplierProdExcelVO.getVolume());
                    spuExcelVO.setPriceFee(supplierProdExcelVO.getPriceFee());
                    spuExcelVO.setMarketPriceFee(supplierProdExcelVO.getMarketPriceFee());
                    spuExcelVO.setStock(supplierProdExcelVO.getStock());
                    spuExcelVO.setSpuCode(Objects.nonNull(supplierProdExcelVO.getSpuCode()) ? supplierProdExcelVO.getSpuCode() : null);
                    spuExcelVO.setPropertiesZh(Objects.nonNull(supplierProdExcelVO.getPropertiesZh()) ? supplierProdExcelVO.getPropertiesZh() : null);
                    spuExcelVO.setSkuNameZh(Objects.nonNull(supplierProdExcelVO.getSkuNameZh()) ? supplierProdExcelVO.getSkuNameZh() : null);
                    spuExcelVO.setStockWarning(supplierProdExcelVO.getStockWarning());
                    return spuExcelVO;
                }).collect(Collectors.toList());
            }

            spuExcelService.exportExcel(listSpuExcelVO, errorMap);
        } catch (Exception e) {
            List<String> list = errorMap.get(SpuExportError.OTHER.value());
            if (CollUtil.isEmpty(list)) {
                list = new ArrayList<>(0);
                errorMap.put(SpuExportError.OTHER.value(), list);
            }
            list.add(e.getMessage());
        }
        list.clear();
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //确保所有数据都能入库
        saveData();
    }
}
