package com.xdap.self_development.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.xdap.self_development.common.BusinessDatabase;
import com.xdap.self_development.controller.request.EngineParamDetailExcel;
import com.xdap.self_development.domain.enums.ParamOperationEnum;
import com.xdap.self_development.domain.enums.ParamValueVersionEnum;
import com.xdap.self_development.domain.pojo.DataEntryEngineModelPojo;
import com.xdap.self_development.domain.pojo.EngineParamDetailPojo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

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


@Slf4j
public class EngineParamDetailListener implements ReadListener<EngineParamDetailExcel> {
    private final BusinessDatabase bd;
    private final DataEntryEngineModelPojo modelPojo;
    private static final int BATCH_COUNT = 1000;
    private String userId;

    private List<EngineParamDetailExcel> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    public EngineParamDetailListener(
            BusinessDatabase bd,
            DataEntryEngineModelPojo modelPojo,
            String userId) {
        this.bd = bd;
        this.modelPojo = modelPojo;
        this.userId = userId;
    }

    @Override
    public void invoke(EngineParamDetailExcel engineModelDetailPojo, AnalysisContext analysisContext) {

        engineModelDetailPojo.setSheetName(analysisContext.readSheetHolder().getSheetName());
        engineModelDetailPojo.setRowIndex(analysisContext.readRowHolder().getRowIndex() + 1);
        // 数据校验
        cachedDataList.add(engineModelDetailPojo);

    }



    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 存储到数据库
        saveData();
        cachedDataList = new ArrayList<>();
        log.info("所有数据解析完成！");
    }


    public void saveData() {

        log.info("开始处理 处理数据条数{}", cachedDataList.size());

        for (EngineParamDetailExcel paramDetailExcel : cachedDataList) {
            if (ObjectUtils.isEmpty(paramDetailExcel.getParameterValue())) {
                //如果参数值为空直接返回
                continue;
            }

            List<EngineParamDetailPojo> detailPojos =
                    bd.getBusinessDatabase()
                            .eq("id",paramDetailExcel.getId())
                            .eq("engine_model_id",modelPojo.getId())
                            .eq("major_version",modelPojo.getVersionNumber())
                            .doQuery(EngineParamDetailPojo.class);
            //如果这个ID有数据则更新小版本为草稿
            if (ObjectUtils.isNotEmpty(detailPojos)) {
                bd.getBusinessDatabase()
                        .update("minor_version", paramDetailExcel.getParameterValue())
                        .update("version_status",ParamValueVersionEnum.DRAFT.getCode())
                        .update("last_updated_by", userId)
                        .update("operation",String.valueOf(ParamOperationEnum.UPDATE))
                        .rowid("id", detailPojos.get(0).getId())
                        .doUpdate(EngineParamDetailPojo.class);
            }
        }
    }
}
