package com.ruixuan.salesLeads.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.ruixuan.common.utils.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * ***************************************************
 *
 * @Auther: zianY
 * @Descipion: 销售线索excel 监听解析
 * @CreateDate: 2022-05-24
 * ****************************************************
 */

public class ImportExcelListener extends AnalysisEventListener<Map<Integer, String>> {


    private static final Logger log = LoggerFactory.getLogger(ImportExcelListener.class);

    public static List<Map<String,String>> importDataList = new ArrayList<>();

    public static Map<Integer, String> importHeadList = new HashMap<>();



    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext analysisContext) {

        Map<String, String> ObjectMap = new HashMap<>();

        String dataStr = JSON.toJSONString(data);

        //log.info("解析到的一条数据=====excelRow = {}", JSON.toJSONString(data));

        if (!dataStr.isEmpty() && !StringUtils.equals(dataStr,"{}")){
            data.forEach((key,vlue) ->{
                Object mapKey = importHeadList.get(key);
                if (StringUtils.isNotNull(mapKey)){

                    String keyStr = (String)mapKey;
                    keyStr.replace(" ","");

                    if (StringUtils.isNotNull(vlue)){
                        vlue = vlue.replace(" ","");
                    }

                    if (keyStr.contains("家ID")){
                        ObjectMap.put("sellerId",vlue);
                    }
                    if (keyStr.contains("昵称")){
                        ObjectMap.put("sellerName",vlue);
                    }
                    if (keyStr.contains("粉丝")){
                        ObjectMap.put("fans",vlue);
                    }
                    if (keyStr.contains("行业")){
                        ObjectMap.put("industryId",vlue);
                    }
                    if (keyStr.contains("省份")){
                        ObjectMap.put("provinceId",vlue);
                    }
                    if (keyStr.contains("城市")){
                        ObjectMap.put("cityId",vlue);
                    }
                    if (keyStr.contains("联系")){
                        ObjectMap.put("telephone",vlue);
                    }
                    if (keyStr.contains("绑定服务商")){
                        //ObjectMap.put("providerFlag",StringUtils.isEmpty(vlue) ? "否" : vlue);
                        ObjectMap.put("providerFlag",vlue);
                    }
                    if (keyStr.contains("跃迁")){
                        //ObjectMap.put("transitionFlag",StringUtils.isEmpty(vlue) ? "否" : vlue);
                        ObjectMap.put("transitionFlag",vlue);
                    }
                    if ((keyStr).contains("30天")){
                        ObjectMap.put("NearlyThirtyDays",vlue);
                    }

                    if ((keyStr).contains("gmv") || (keyStr).contains("GMV")){
                        ObjectMap.put("gmvMonth",keyStr.substring(0,keyStr.length()-3));
                        ObjectMap.put("gmv",vlue);
                    }

                    if (keyStr.contains("mtd") || keyStr.contains("MTD")){
                        ObjectMap.put("mtdDate",keyStr.substring(0,keyStr.length()-3));
                        ObjectMap.put("mtd",vlue);
                    }

                }
            });

        }



        importDataList.add(ObjectMap);


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("解析完所有数据=总条数==={}",importDataList.size());

    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("表头数据解析完成===excelHead-->>>{}", headMap);
        importHeadList.putAll(headMap);
    }


}
