package io.renren.modules.dataSet.upload;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 直接用map接收数据
 *
 * @author Jiaju Zhuang
 */
@Data
public class NoModelDataListener_pre extends AnalysisEventListener<Map<Integer, String>> {
    private static final Logger LOGGER = LoggerFactory.getLogger(NoModelDataListener_pre.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();

    private String[]  filedList;
    private Map<Integer,String> filedMap = new LinkedHashMap<>();

    private String id = "S686";
    // needList 保存需要的数据
    private List<Map<String,Object>> needList = new LinkedList<Map<String,Object>>();
    // allList 存放所有数据
    private List<Map<String,Object>> allList = new LinkedList<Map<String,Object>>();


    public NoModelDataListener_pre(String[] filedList,String id) {
        this.filedList = filedList;
        this.id = id;
    }

    public NoModelDataListener_pre(String[] filedList) {
        this.filedList = filedList;
    }

    public NoModelDataListener_pre() {
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
//        LOGGER.info("解析到一条头数据：{}, currentRowHolder: {}", headMap.toString(), context.readRowHolder().getRowIndex());
//        System.out.println(context.readSheetHolder().getSheetNo());
//        System.out.println(context.readSheetHolder().getSheetName());
//        {0=日期标题, 1=字符串标题, 2=数字标题}
        for (Map.Entry<Integer, String> integerStringEntry : headMap.entrySet()) {
            for (String s : filedList) {
                if(s.equals(integerStringEntry.getValue().toString()) ){
//                    System.out.println(s);
//                    System.out.println(integerStringEntry.getKey());
                    filedMap.put(integerStringEntry.getKey(),integerStringEntry.getValue());
                }
            }
        }




    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
//        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));


        Map<String,Object> needdata = new LinkedHashMap<>();
        for (Map.Entry<Integer, String> integerStringEntry : filedMap.entrySet()) {
//            System.out.println(integerStringEntry);
            String s = data.get(integerStringEntry.getKey());
            String key_value = integerStringEntry.getValue();
            if( s ==null || s.length()<=0 )
            {
                s = "null";
            }
            if(this.id != "S686"){
                key_value = this.id + "_" + key_value;
            }
            needdata.put(key_value, s);
        }
        allList.add(needdata);
        if (list.size() <= BATCH_COUNT) {
            needList.add(needdata);
//            list.clear();
        }
//        needList.add(needdata);
//        System.out.println(data.get(1));
//        if (list.size() >= BATCH_COUNT) {
//            saveData();
////            list.clear();
//        }

    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
//        LOGGER.info("{}条数据，开始存储数据库！", list.size());
//        for (Map<Integer, String> integerStringMap : list) {
//
//            needList.add(integerStringMap.get(this.needDataIndex));
//        }
//        LOGGER.info("存储数据库成功！");
    }
}
