package cn.reghao.tnb.sb.config;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import jakarta.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author reghao
 * @date 2025-07-14 16:53:19
 */
//@Endpoint(id="myEndpoint")
//@Component
public class MyEndpoint {
    private String STATUS = "up";
    private String DETAIL = "ok";

    @ReadOperation
    public Map<String, String> getStatus(){
        Map<String, String> map = new HashMap<>();
        map.put("status", STATUS);
        map.put("detail", DETAIL);
        return map;
    }

    @ReadOperation
    public Map<String, String> getStatusByName(@Selector String name){
        Map<String, String> map = new HashMap<>();
        if ("status".equals(name)) {
            map.put("status", STATUS);
        } else if ("detail".equals(name)){
            map.put("detail", DETAIL);
        }

        return map;
    }

    // 动态修改指标
    @WriteOperation
    public void updateStatus(@Selector String name, @Nullable String value){
        if (!StringUtils.isEmpty(value)){
            if ("status".equals(name)){
                STATUS = value;
            }else if ("detail".equals(name)){
                DETAIL = value;
            }
        }
    }
}
