package devops;

import cn.reghao.bnt.common.machine.Machine;
import cn.reghao.bnt.common.msg.constant.NodeStatus;
import cn.reghao.bnt.common.msg.event.EvtAgentStart;
import cn.reghao.bnt.web.WebApplication;
import cn.reghao.bnt.web.devops.machine.db.repository.MachineInfoRepository;
import cn.reghao.bnt.web.devops.machine.model.po.MachineInfo;
import cn.reghao.bnt.web.devops.machine.service.MachineQuery;
import cn.reghao.bnt.web.devops.machine.service.MachineService;
import cn.reghao.jutil.jdk.machine.data.detail.NetworkDetail;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-06-05 10:53:40
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = WebApplication.class)
public class MachineTest {
    @Autowired
    MachineService machineService;
    @Test
    public void addOssMachineTest() {
        Map<String, String> map = new HashMap<>();
        map.put("oss.dnkt-admin", "110.110.110.1");
        map.put("oss.dnkt", "110.110.110.2");
        map.put("oss.iquizoo-cloud", "110.110.110.3");
        map.put("oss.iquizoo-timuke", "110.110.110.4");
        map.put("oss.iquizoo-nztp", "110.110.110.5");
        map.put("oss.iquizoo-dnkt-parent", "110.110.110.6");
        map.put("oss.iquizoo-dnkt-open", "110.110.110.7");

        map.forEach((machineId, ipv4) -> {
            Machine machine = new Machine();
            EvtAgentStart evtAgentStart = machine.detail();
            NetworkDetail networkDetail = evtAgentStart.getNetworkDetails().get(0);
            networkDetail.setPubicIpv4(ipv4);
            networkDetail.setIpv4(ipv4);

            evtAgentStart.setMachineId(machineId);
            evtAgentStart.setNetworkDetails(List.of(networkDetail));
            //machineService.addOrUpdateMachine(evtAgentStart);
        });
    }

    @Autowired
    MachineQuery machineQuery;
    @Autowired
    MachineInfoRepository machineInfoRepository;
    @Test
    public void shutdownOssMachineTest() {
        List<MachineInfo> list = machineInfoRepository.findByStat(NodeStatus.Online.getCode()).stream()
                .filter(machineInfo -> machineInfo.getMachineId().startsWith("oss"))
                .peek(machineInfo -> machineInfo.setStat(NodeStatus.Offline.getCode()))
                .collect(Collectors.toList());
        //machineInfoRepository.saveAll(list);
    }
}
