package cn.estsh.i3plus.core.apiservice.controller;

import cn.estsh.impp.framework.boot.eureka.EurekaClientHealthCheck;
import cn.estsh.impp.framework.boot.util.ResultBean;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Applications;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description :
 * @Reference :
 * @Author :  Alwaysfrin
 * @CreateDate : 2018-11-19 19:56
 * @Modify:
 **/
@RestController
@Api(description="微服功能demo")
@RequestMapping("/demo-eureka-service")
@ConditionalOnExpression("'${eureka.client.enabled}' == 'true'")
public class DemoEurekaController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoEurekaController.class);

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private ApplicationInfoManager applicationInfoManager;

    @Autowired
    private EurekaClientHealthCheck healthCheck;

    /**
     * 停止微服
     * @return
     */
    @GetMapping(value="/stop")
    @ApiOperation(value="停止微服",notes = "停止当前微服")
    public ResultBean stopServer() {
        LOGGER.info("请求停止微服。。。");
        eurekaClient.shutdown();
        return new ResultBean(true);
    }

    /**
     * 显示微服信息
     * @return
     */
    @GetMapping(value="/show")
    @ApiOperation(value="显示微服",notes = "显示微服")
    public ResultBean show() {
        LOGGER.info("【加载微服配置】");
        LOGGER.info("applicationInfoManager:{}",applicationInfoManager);
        LOGGER.info("getEurekaInstanceConfig:{}",applicationInfoManager.getEurekaInstanceConfig());

        EurekaInstanceConfig config = applicationInfoManager.getEurekaInstanceConfig();
        LOGGER.info(config.getAppname());

        Applications app = eurekaClient.getApplications();
        LOGGER.info("spring dc : "+eurekaClient + ",app：" + app);

        EurekaClientConfig eurekaClientConfig = eurekaClient.getEurekaClientConfig();
        LOGGER.info("eurekaClientConfig:{}",eurekaClientConfig);

        return new ResultBean(true);
    }

    /**
     * 改变微服状态
     * @return
     */
    @GetMapping(value="/health-status")
    @ApiOperation(value="微服健康状态",notes = "查看微服健康状态")
    public ResultBean healthStatus() {
        LOGGER.info("【微服状态】{}",healthCheck.isHealth());

        return new ResultBean(true,"状态:" + healthCheck.isHealth());
    }

    /**
     * 改变微服状态
     * @return
     */
    @GetMapping(value="/change-status")
    @ApiOperation(value="改变微服状态",notes = "改变微服状态(true,正常使用.false,停止使用)")
    public ResultBean changeStatus(boolean status) {
        LOGGER.info("【改变微服状态】{}",status);

        healthCheck.setHealth(status);

        return new ResultBean(true);
    }
}
