package com.rongwei.bscommon.sys.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.rongwei.bscommon.sys.excel.EquForkliftRepairRecordTemplate;
import com.rongwei.bsentity.domain.EquForkliftRepairRecordDo;
import com.rongwei.bsentity.enums.PlantEnum;
import com.rongwei.rwadmincommon.system.domain.SysDictDo;
import com.rongwei.rwadmincommon.system.domain.SysOrganizationDo;
import com.rongwei.rwadmincommon.system.service.SysDictService;
import com.rongwei.rwadmincommon.system.service.SysOrganizationService;
import com.rongwei.rwcommon.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

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

    private static final String[] NON_SET = {"equipmentnumber","rapairdate"};
    private List<EquForkliftRepairRecordDo> forkliftRepairRecordDoList = new ArrayList<>();

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





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


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public List<EquForkliftRepairRecordDo> getForkliftRepairRecordDoList() {
        return this.forkliftRepairRecordDoList;
    }


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


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

    private EquForkliftRepairRecordDo migrateDate(EquForkliftRepairRecordTemplate forkliftRepairRecordTemplate) {
        Date date = new Date();
        StringBuilder planMonth = new StringBuilder();
        // SysUserVo currentUser = CommonUtils.getCurrentUser();
        EquForkliftRepairRecordDo equForkliftRepairRecordDo = new EquForkliftRepairRecordDo();
        BeanUtils.copyProperties(forkliftRepairRecordTemplate, equForkliftRepairRecordDo);


        equForkliftRepairRecordDo.setCreatedate(date)
                .setModifydate(date)
                .setId(SecurityUtil.getUUID())
                .setTenantid(PlantEnum.idOf(forkliftRepairRecordTemplate.getOwnedfactory()))
                .setOwnedfactory(forkliftRepairRecordTemplate.getOwnedfactory())
                .setEquipmentname(forkliftRepairRecordTemplate.getEquipmentname())
                .setEquipmentnumber(forkliftRepairRecordTemplate.getEquipmentnumber())
                .setUsedept(forkliftRepairRecordTemplate.getUsedept())
                .setRapairname(forkliftRepairRecordTemplate.getRapairname())
                .setRapairdate(forkliftRepairRecordTemplate.getRapairdate())
                .setRapairdetail(forkliftRepairRecordTemplate.getRapairdetail())
                .setSpecificationmodel(forkliftRepairRecordTemplate.getSpecificationmodel())
                .setQuantity(forkliftRepairRecordTemplate.getQuantity())
               ;

        return equForkliftRepairRecordDo;
    }
}
