
package cn.orionsec.ops.handler.importer.impl;

import cn.orionsec.kit.spring.SpringHolder;
import cn.orionsec.ops.dao.MachineInfoDAO;
import cn.orionsec.ops.entity.domain.MachineInfoDO;
import cn.orionsec.ops.entity.importer.DataImportDTO;
import cn.orionsec.ops.service.api.MachineEnvService;

import java.util.Optional;

public class MachineInfoDataImporter extends AbstractDataImporter<MachineInfoDO> {

    private static final MachineInfoDAO machineInfoDAO = SpringHolder.getBean(MachineInfoDAO.class);

    private static final MachineEnvService machineEnvService = SpringHolder.getBean(MachineEnvService.class);

    public MachineInfoDataImporter(DataImportDTO importData) {
        super(importData, machineInfoDAO);
    }

    @Override
    protected void updateFiller(MachineInfoDO row) {
        // 填充忽略的字段
        Optional.ofNullable(row.getId())
                .map(machineInfoDAO::selectById)
                .ifPresent(m -> {
                    row.setProxyId(m.getProxyId());
                    row.setKeyId(m.getKeyId());
                });
    }

    @Override
    protected void insertCallback(MachineInfoDO row) {
        // 初始化新增机器环境变量
        machineEnvService.initEnv(row.getId());
    }

}
