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.pqs9900.pojo.linedata.LineMarkWeight;
import com.pqs9900.pojo.linedata.MarkExcel;
import com.pqs9900.pojo.linedata.MarkWeightExcel;
import com.pqs9900.service.linedata.LineMarkService;
import com.pqs9900.service.linedata.LineMarkWeightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

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

public class LineMarkWeightExcelListener extends AnalysisEventListener<MarkWeightExcel> {

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

    private LineMarkWeightService lineMarkWeightService;


    private Integer ding = 0;

    private List<MarkWeightExcel> list = new ArrayList<>();

    @Override
    public void invoke(MarkWeightExcel 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(Objects.isNull(object.getCommunFeeMark())){
            throw new ExcelAnalysisException(msg+"通信费用评分不可为空");
        }

        if(Objects.isNull(object.getServiceMark())){
            throw new ExcelAnalysisException(msg+"服务条款评分不可为空");
        }
        if(Objects.isNull(object.getAgentMark())){
            throw new ExcelAnalysisException(msg+"中间户数据需求评分不可为空");
        }
        if(Objects.isNull(object.getCompanyMark())){
            throw new ExcelAnalysisException(msg+"公司数据需求评分不可为空");
        }
        if(Objects.isNull(object.getUserMark())){
            throw new ExcelAnalysisException(msg+"用户数据需求评分不可为空");
        }
        if(Objects.isNull(object.getLineTypeMark())){
            throw new ExcelAnalysisException(msg+"监测点类型评分不可为空");
        }
        if(Objects.isNull(object.getBusinessMark())){
            throw new ExcelAnalysisException(msg+"行业重要度评分不可为空");
        }
        list.add(object);
    }

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

            String res = lineMarkWeightService.dealData(list);
            if(!"操作成功".equals(res)){
                throw new ExcelAnalysisException(res);
            }
        }else {
            throw new ExcelAnalysisException("导入数据为空");
        }


    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        //进行表头判断
        if(Objects.nonNull(headMap)){
            if(headMap.size()!=15){
                throw new ExcelAnalysisException("excel表头出错");
            }else {
                if(!"省份".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 LineMarkWeightExcelListener(LineMarkWeightService lineMarkWeightService){
        this.lineMarkWeightService = lineMarkWeightService;
    }
}
