package com.ningdatech.pmapi.projectlib.model.entity;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.constant.CommonConst;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import java.util.*;

/**
 * @author CMM
 * @since 2023/04/17 18:26
 */
@Data
public class AnalysisEventMonitor extends AnalysisEventListener<Map<Integer, String>> {

    private final List<Project> records = new ArrayList<>();

    /**
     * 存储Key
     */
    Map<Integer, String> key = new HashMap<>();
    /**
     * keyList
     */
    List<String> keyList = new ArrayList<>();

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        super.onException(exception, context);
        throw BizException.wrap("导入年度计划解析失败");
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        Set<Integer> integerSet = headMap.keySet();
        for (Integer integer : integerSet) {
            keyList.add(headMap.get(integer));
        }
        key.putAll(headMap);
    }

    @Override
    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext context) {
        //Project project = new Project();
        //project.setId(data.getProjectId());
        //project.setProjectName(data.getProjectName());
        //project.setProjectIntroduction(data.getProjectIntroduction());
        //// 建设依据忽略
        //project.setIsFirst(CommonConst.NEW_CONSTRUCTION.equals(data.getIsFirst()) ? 1 : 0);
        //String[] dataArr = data.getBuildCycle().split(CommonConst.ZHI);
        //project.setBeginTime(dataArr[0].trim());
        //project.setEndTime(dataArr[1].trim());
        //project.setDeclareAmount(data.getDeclaredAmount());
        //project.setAnnualPlanAmount(data.getAnnualPlanAmount());
        //project.setDeclareHaveAmount(data.getDeclareHaveAmount());
        //project.setDeclareGovOwnFinanceAmount(data.getDeclareGovOwnFinanceAmount());
        //project.setDeclareGovSuperiorFinanceAmount(data.getDeclareGovSuperiorFinanceAmount());
        //project.setDeclareBankLendingAmount(data.getDeclareBankLendingAmount());
        //project.setDeclareOtherAmount(data.getDeclareOtherAmount());
        //project.setEngineeringSpeedOne(data.getFirstQuarter());
        //project.setEngineeringSpeedTwo(data.getSecondQuarter());
        //project.setEngineeringSpeedThree(data.getThirdQuarter());
        //project.setEngineeringSpeedFour(data.getFourthQuarter());
        //project.setBuildOrgName(data.getBuildUnitName());
        //project.setContactName(data.getContactName());
        //project.setResponsibleMan(data.getResponsibleMan());
        //project.setProjectRemarks(data.getProjectRemarks());
        //records.add(project);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //if (records.isEmpty()) {
        //    throw BizException.wrap("导入年度计划为空");
        //}
        //List<Long> projectIds = CollUtils.fieldList(records, Project::getId);
        //long count = projectService.count(Wrappers.lambdaQuery(Project.class)
        //        .in(Project::getId, projectIds));
        //if (count != records.size()) {
        //    throw BizException.wrap("请确保所有项目都存在");
        //}
        //projectService.updateBatchById(records);
    }
}
