package com.cyksj.common;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/*
 *项目名: netflix
 *文件名: EnvCommon
 *创建者: JavaZou
 *创建时间:2022/10/26 17:10
 */
@Component
@Getter
@Setter
public class EnvCommonService {

	public static final String active = "dev";

	public static final String active_prd = "prd";

	public static final String active_pre = "pre";

	public static String DISTRIBUTE_DEFAULT_URL_PREFIX;

	@Value(value = "${spring.profiles.active}")
	private String env;

	@Value(value = "${wechat.domain}")
	private String domain;

	@Value(value = "${wechat.notifyDomain}")
	private String notifyDomain;

	public String getHost() {
		if (isPreEnv()) {
			return domain.replaceAll("8081", "8082");
		}
		return domain;
	}

	public String getNotifyHost() {
		if (isPreEnv()) {
			return notifyDomain.replaceAll("8081", "8082");
		}
		return notifyDomain;
	}

	public String getDomain() {
		return this.getHost().substring(0, this.getHost().indexOf("80"));
	}

	public String getDomain2() {
		return this.getHost().substring(0, this.getHost().indexOf("/80"));
	}

	public Boolean isPrdEnv() {
		return EnvCommonService.active_prd.equals(getEnv());
	}

	public Boolean isPreEnv() {
		return EnvCommonService.active_pre.equals(getEnv());
	}

	@PostConstruct
	public void getDistributeUrl() {
		DISTRIBUTE_DEFAULT_URL_PREFIX = getDomain();
		if (!DISTRIBUTE_DEFAULT_URL_PREFIX.endsWith("/")) {
			DISTRIBUTE_DEFAULT_URL_PREFIX += "/";
		}
		DISTRIBUTE_DEFAULT_URL_PREFIX += "%s";
	}
}
