package com.zcxk.rmcp.web.excel.resolver.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.fastjson.JSON;
import com.zcxk.core.common.pojo.ResultMessage;
import com.zcxk.rmcp.core.entity.InstallPlan;
import com.zcxk.rmcp.web.excel.model.ExcelData;
import com.zcxk.rmcp.web.excel.model.InstallMeterData;
import com.zcxk.rmcp.web.excel.model.InstallPlanExcelData;
import com.zcxk.rmcp.web.excel.model.InstallPlanInfoData;
import com.zcxk.rmcp.web.service.install.InstallPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @author linqingweidd
 * @date 2021-07-30 11:39
 */
@Slf4j
public class InstallPlanListener extends AnalysisEventListener<InstallPlanExcelData> {
    private List<InstallPlanExcelData> list = new ArrayList<>();


    private InstallPlanService installPlanService;
    private ExcelData excelData;

    public InstallPlanListener(InstallPlanService installPlanService,ExcelData excelData) {
        this.installPlanService = installPlanService;
        this.excelData = excelData;
    }
    @Override
    public void invoke(InstallPlanExcelData data, AnalysisContext analysisContext) {
        if (
                data.getBuildingName() == null || data.getFloorName() == null || data.getDoorNo() == null
        ) {
            data.setRemark("必填项不能为空");
            list.add(data);
            excelData.getFailNum().incrementAndGet();
            return;
        }
        excelData.getSuccessNum().incrementAndGet();
        ResultMessage resultMessage = installPlanService.handleExcelData(data,excelData);
        if(resultMessage.getStatus()){
            excelData.getSuccessNum().incrementAndGet();
        }else {
            data.setRemark(resultMessage.getMessage());
            list.add(data);
            excelData.getFailNum().incrementAndGet();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("批量导出设备完成 successNum = {},failNum = {}",excelData.getSuccessNum().get(),excelData.getFailNum().get());
        installPlanService.saveExcelData(excelData);
        // TODO: 2021/8/2 生成失败文件
        // TODO: 2021/8/2 生成消息
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("解析到一条头数据:{}", JSON.toJSONString(headMap));

        String headName = headMap.get(2);
        if(StringUtils.equals(headName,"公司*")) excelData.getInstallPlanInfoData().setCompanyName(headMap.get(3));
        if(StringUtils.equals(headName,"部门")) excelData.getInstallPlanInfoData().setDeptName(headMap.get(3));
        if(StringUtils.equals(headName,"小区名称*")) excelData.getInstallPlanInfoData().setCommunityName(headMap.get(3));
        if(StringUtils.equals(headName,"设备型号*")) excelData.getInstallPlanInfoData().setDeviceTypeName(headMap.get(3));
        if(StringUtils.equals(headName,"施工单位")) excelData.getInstallPlanInfoData().setUnit(headMap.get(3));
        if(StringUtils.equals(headName,"联系电话")) excelData.getInstallPlanInfoData().setUnit(headMap.get(3));
        if(StringUtils.equals(headName,"联系电话")){
            excelData.setInstallPlan(installPlanService.createPlanExcel(excelData));
        }

    }



    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        log.info("InstallPlanListener 解析异常:{}", JSON.toJSONString(exception));
        // TODO: 2021/8/2 生成异常消息
    }

    public List<InstallPlanExcelData> getData(){
        return list;
    }
}
