package cn.reghao.autodop.dmaster.machine.service;

import cn.reghao.autodop.common.machine.Machine;
import cn.reghao.autodop.common.msg.pub.dto.node.NodeLogDTO;
import cn.reghao.autodop.common.msg.pub.dto.node.constant.NodeStatus;
import cn.reghao.autodop.dmaster.machine.db.crud.MachineHostCrud;
import cn.reghao.autodop.dmaster.machine.db.crud.MachineInfoCrud;
import cn.reghao.autodop.dmaster.machine.db.query.MachineHostQuery;
import cn.reghao.autodop.dmaster.machine.db.query.MachineInfoQuery;
import cn.reghao.autodop.dmaster.machine.model.po.MachineHost;
import cn.reghao.autodop.dmaster.machine.model.po.MachineInfo;
import cn.reghao.autodop.dmaster.machine.db.crud.NodeLogCrud;
import cn.reghao.autodop.dmaster.machine.model.po.NodeLog;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2021-10-15 16:39:48
 */
@Service
public class NodePubClazzImpl {
    private final MachineInfoQuery infoQuery;
    private final MachineInfoCrud infoCrud;
    private final MachineHostQuery hostQuery;
    private final MachineHostCrud hostCrud;
    private final NodeLogCrud nodeLogCrud;

    public NodePubClazzImpl(MachineInfoQuery infoQuery, MachineInfoCrud infoCrud,
                            MachineHostQuery hostQuery, MachineHostCrud hostCrud,
                            NodeLogCrud nodeLogCrud) {
        this.infoQuery = infoQuery;
        this.infoCrud = infoCrud;
        this.hostQuery = hostQuery;
        this.hostCrud = hostCrud;
        this.nodeLogCrud = nodeLogCrud;
    }

    public void start(Machine.MachineDetail machineDetail) {
        String machineId = machineDetail.getMachineId();
        MachineInfo machineInfo = infoQuery.findByMachineId(machineId);
        if (machineInfo == null) {
            machineInfo = new MachineInfo(machineDetail);
            infoCrud.save(machineInfo);
            hostCrud.save(new MachineHost(machineInfo));
        }

        MachineHost machineHost = hostQuery.findByMachineId(machineId);
        if (machineHost != null) {
            machineHost.setLastCheck(System.currentTimeMillis());
            machineHost.setStatus(NodeStatus.Online.name());
            hostCrud.update(machineHost);
        }
    }

    public void shutdown(Machine.MachineStat machineStat) {
        String machineId = machineStat.getMachineId();
        MachineHost machineHost = hostQuery.findByMachineId(machineId);
        if (machineHost != null) {
            machineHost.setLastCheck(System.currentTimeMillis());
            machineHost.setStatus(NodeStatus.Offline.name());
            hostCrud.update(machineHost);
        }
    }

    // TODO 根据 MachineStat 中的信息检查系统负载，CPU，内存，磁盘等使用情况
    public void heartbeat(Machine.MachineStat machineStat) {
        String machineId = machineStat.getMachineId();
        MachineHost machineHost = hostQuery.findByMachineId(machineId);
        if (machineHost != null) {
            machineHost.setLastCheck(System.currentTimeMillis());
            hostCrud.update(machineHost);
        }
    }

    public void log(NodeLogDTO nodeLogDTO) {
        nodeLogCrud.save(new NodeLog(nodeLogDTO));
    }
}
