package com.pqs9900.listener;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.njcn.pojo.configuration.DicData;
import com.njcn.service.configuration.DeviceService;
import com.njcn.utils.UserUtil;
import com.pqs9900.controller.business.AreaController;
import com.pqs9900.controller.linedata.LineMarkController;
import com.pqs9900.pojo.linedata.MarkExcel;
import com.pqs9900.service.linedata.LineMarkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 监测点批量评级导入excel监听
 *
 * @author cdf
 * @date 2021/10/27
 */

public class LineMarkExcelListener extends AnalysisEventListener<MarkExcel> {

    private static final Logger logger = LoggerFactory.getLogger(LineMarkExcelListener.class);

    private LineMarkService lineMarkService;



    private List<String> deFlag = new ArrayList<>(Arrays.asList("极重要","重要","普通","不重要"));



    private Integer ding = 0;

    List<MarkExcel> list = new ArrayList<>();

    @Override
    public void invoke(MarkExcel object, AnalysisContext context){
        ding++;
        String msg ="第"+ding+"行,";
        if(StrUtil.isBlank(object.getProvince())){
            throw new ExcelAnalysisException(msg+"省份不可为空");
        }
        if(StrUtil.isBlank(object.getGdName())){
            throw new ExcelAnalysisException(msg+"供电公司不可为空");
        }
        if(StrUtil.isBlank(object.getSubName())){
            throw new ExcelAnalysisException(msg+"变电站不可为空");
        }
        if(StrUtil.isBlank(object.getDevName())){
            throw new ExcelAnalysisException(msg+"设备名称不可为空");
        }
        if(StrUtil.isBlank(object.getLineName())){
            throw new ExcelAnalysisException(msg+"监测点名称不可为空");
        }
        if(StrUtil.isBlank(object.getMark())){
            throw new ExcelAnalysisException(msg+"监测点评级不可为空");
        }

        list.add(object);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //进行
        if(CollectionUtil.isNotEmpty(list)){

            String res = lineMarkService.dealData(list);
            if(!"操作成功".equals(res)){
                throw new ExcelAnalysisException(res);
            }




        }else {

        }


    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        //进行表头判断
        if(Objects.nonNull(headMap)){
            if(headMap.size()!=6){
                throw new ExcelAnalysisException("excel表头出错");
            }else {
                if(!"省份".equals(headMap.get(0)) || !"供电公司".equals(headMap.get(1)) ||!"所属变电站".equals(headMap.get(2))
                || !"装置名称".equals(headMap.get(3))||!"监测点名称".equals(headMap.get(4))|| !"监测点评级".equals(headMap.get(5))){
                    throw new ExcelAnalysisException("excel表头出错");
                }
            }
        }else {
            throw new ExcelAnalysisException("excel表头出错");
        }
    }

    public LineMarkExcelListener(LineMarkService lineMarkService){
        this.lineMarkService = lineMarkService;
        //监测点等级匹配


    }
}
