package com.rongwei.bscommon.sys.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.rongwei.bscommon.sys.excel.EquDieseloilUseManagementTemplate;
import com.rongwei.bsentity.domain.EquDieseloilUseManagementDo;
import com.rongwei.bsentity.domain.EquMaintenanceStandardDo;
import com.rongwei.bsentity.enums.MaintenanceTypeEnum;
import com.rongwei.bsentity.enums.PlantEnum;
import com.rongwei.rwcommon.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.h2.engine.User;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author shangmi
 * @title ExcelListener
 * @date 2023/12/7 16:09
 * @description EquMaintenanceStandard监听器
 */
@Slf4j
public class EquDieseloilUseManagementListener extends AnalysisEventListener<EquDieseloilUseManagementTemplate> {

    private static final String[] NON_SET = {"dieseldate","dieselnametype"};

    private List<EquDieseloilUseManagementDo> dieseloilUseManagementDoList = new ArrayList<>();

    private List<String> errorData = new ArrayList<>();

    @Override
    public void invoke(EquDieseloilUseManagementTemplate dieseloilUseManagementTemplate, AnalysisContext analysisContext) {


        try {
            log.info("解析到一条数据：{}", dieseloilUseManagementTemplate);
            for (Field field : dieseloilUseManagementTemplate.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                String name = field.getName();
                if (Objects.isNull(field.get(dieseloilUseManagementTemplate)) && Arrays.asList(NON_SET).contains(name)) {
                    errorData.add((analysisContext.readRowHolder().getRowIndex() + 1) + "行存在必填数据为空的情况，请填写完整！");
                }
            }
            EquDieseloilUseManagementDo e = migrateDate(dieseloilUseManagementTemplate);
            dieseloilUseManagementDoList.add(e);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }



    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public List<EquDieseloilUseManagementDo> getDieseloilUseManagementDoList() {
        return this.dieseloilUseManagementDoList;
    }


    public List<String> getErrorData() {
        return this.errorData;
    }


    /**
     * 数据转换
     *
     * @param equMaintenanceStandardTemplate
     * @return {@link EquMaintenanceStandardDo}
     * @date 2023/12/8 10:14
     * @author shangmi
     */

    private EquDieseloilUseManagementDo migrateDate(EquDieseloilUseManagementTemplate equMaintenanceStandardTemplate) {
        EquDieseloilUseManagementDo equDieseloilUseManagementDo = new EquDieseloilUseManagementDo();
        BeanUtils.copyProperties(equMaintenanceStandardTemplate, equDieseloilUseManagementDo);
        return equDieseloilUseManagementDo;
    }
}
