package com.rongwei.bscommon.sys.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.rongwei.bscommon.sys.excel.EquMaintenanceStandardTemplate;
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.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 EquMaintenanceStandardListener extends AnalysisEventListener<EquMaintenanceStandardTemplate> {

    private static final String[] NON_SET = {"plantName","equipmentName","maintenancetype", "maintenancesite", "maintenancecontext", "isoutsource"};

    private List<EquMaintenanceStandardDo> maintenanceStandardList = new ArrayList<>();

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

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


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public List<EquMaintenanceStandardDo> getMaintenanceStandardList() {
        return this.maintenanceStandardList;
    }


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


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

    private EquMaintenanceStandardDo migrateDate(EquMaintenanceStandardTemplate equMaintenanceStandardTemplate) {
        Date date = new Date();
        StringBuilder planMonth = new StringBuilder();
        // SysUserVo currentUser = CommonUtils.getCurrentUser();
        EquMaintenanceStandardDo equMaintenanceStandardDo = new EquMaintenanceStandardDo();
        BeanUtils.copyProperties(equMaintenanceStandardTemplate, equMaintenanceStandardDo);
        equMaintenanceStandardDo.setMaintenancetype(MaintenanceTypeEnum.messageOf(equMaintenanceStandardTemplate.getMaintenancetype()).toString())
                // .setEquipmenttype(EquipmentTypeEnum.messageOf(equMaintenanceStandardTemplate.getEquipmenttype()).toString())
                .setCreatedate(date)
                .setModifydate(date)
                .setId(SecurityUtil.getUUID())
                // .setTenantid(currentUser.getOrganizationDoList().get(0).getFullpid().split(",")[1])
                .setTenantid(PlantEnum.idOf(equMaintenanceStandardTemplate.getPlantName()))
                .setPlant(equMaintenanceStandardTemplate.getPlantName())
                //                .setTenantid("0001S31000000000J0TR")
                // .setCreateusername(currentUser.getName())
                // .setCreateuserid(currentUser.getId())
                // .setModifyusername(currentUser.getName())
                // .setModifyuserid(currentUser.getId())
                .setEquipmentNumber(equMaintenanceStandardTemplate.getEquipmentnumber())
                .setEquipmentName(equMaintenanceStandardTemplate.getEquipmentName())
                .setIsoutsource(String.valueOf("是".equals(equMaintenanceStandardTemplate.getIsoutsource()) ? 0 : 1));
        if("是".equals(equMaintenanceStandardTemplate.getJan()) ){
            equMaintenanceStandardDo.setJan(1);
            planMonth.append("JAN");
        }
        if("是".equals(equMaintenanceStandardTemplate.getFeb()) ){
            equMaintenanceStandardDo.setFeb(1);
            planMonth.append(",FEB");
        }
        if("是".equals(equMaintenanceStandardTemplate.getMar()) ){
            equMaintenanceStandardDo.setMar(1);
            planMonth.append(",MAR");
        }
        if("是".equals(equMaintenanceStandardTemplate.getApr()) ){
            equMaintenanceStandardDo.setApr(1);
            planMonth.append(",APR");
        }
        if("是".equals(equMaintenanceStandardTemplate.getMay()) ){
            equMaintenanceStandardDo.setMay(1);
            planMonth.append(",MAY");
        }
        if("是".equals(equMaintenanceStandardTemplate.getJun()) ){
            equMaintenanceStandardDo.setJun(1);
            planMonth.append(",JUN");
        }
        if("是".equals(equMaintenanceStandardTemplate.getJul()) ){
            equMaintenanceStandardDo.setJul(1);
            planMonth.append(",JUL");
        }
        if("是".equals(equMaintenanceStandardTemplate.getAug()) ){
            equMaintenanceStandardDo.setAug(1);
            planMonth.append(",AUG");
        }
        if("是".equals(equMaintenanceStandardTemplate.getSep()) ){
            equMaintenanceStandardDo.setSep(1);
            planMonth.append(",SEP");
        }
        if("是".equals(equMaintenanceStandardTemplate.getOct()) ){
            equMaintenanceStandardDo.setOct(1);
            planMonth.append(",OCT");
        }
        if("是".equals(equMaintenanceStandardTemplate.getNov()) ){
            equMaintenanceStandardDo.setNov(1);
            planMonth.append(",NOV");
        }
        if("是".equals(equMaintenanceStandardTemplate.getDecember()) ){
            equMaintenanceStandardDo.setDecember(1);
            planMonth.append(",DEC");
        }
        equMaintenanceStandardDo.setPlanMonth(planMonth.toString());
        return equMaintenanceStandardDo;
    }
}
