package com.ruoyi.message.config;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import java.util.List;
/**
* 配置绑定校验程序(不依赖 JUnit,可直接用模块的运行时 classpath 执行)。
*
* 存在的意义:把散落的 {@code @Value} 改成 {@code @ConfigurationProperties} 后,
* 前缀/层级写错编译期发现不了,只会在线上发短信时表现为参数为 null。
* 本程序用「真实的扁平 yml」跑一次绑定,把这些 key 锁住。
*
* 执行方式见 {@code ruoyi-message/README-配置绑定校验.md}。
*/
public final class MessagePropertiesBindingCheck {
private static int failed = 0;
private MessagePropertiesBindingCheck() {
}
public static void main(String[] args) throws Exception {
StandardEnvironment environment = new StandardEnvironment();
// 去掉 systemProperties / systemEnvironment:Windows 下它们的名字是大写驼峰(如 XHSMSPATH),
// ConfigurationPropertyName 不接受大写,会干扰绑定诊断。生产用 SpringApplication 时名字会被归一化。
environment.getPropertySources()
.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
environment.getPropertySources()
.remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
List> sources = new YamlPropertySourceLoader()
.load("message-binding-check.yml", new ClassPathResource("message-binding-check.yml"));
environment.getPropertySources().addLast(sources.get(0));
Binder binder = new Binder(ConfigurationPropertySources.get(environment));
RootMessageProperties root = binder
.bind(ConfigurationPropertyName.EMPTY, Bindable.of(RootMessageProperties.class))
.orElseGet(RootMessageProperties::new);
MessageProperties properties = new MessageProperties(root);
// ---- 新华短信网关 ----
check("xhsmsPath", "http://192.16.4.220:8092/sendSms", properties.getSms().getXhsms().getPath());
check("xhsmsAccount", "911124", properties.getSms().getXhsms().getAccount());
check("xhsmsPwd", "zW5eXe", properties.getSms().getXhsms().getPwd());
check("xhsmsjrm", "1069055", properties.getSms().getXhsms().getExtno());
// ---- 南华附一 MT(用户特别指出的那批配置)----
check("sms_accountName", "S200232", properties.getSms().getMt().getAccountName());
check("sms_password", "lD94Yo", properties.getSms().getMt().getPassword());
check("sms_url", "http://open.nhyfy.cn/kapi/gw-api/hntypt/ucpmsg/send", properties.getSms().getMt().getUrl());
check("sms_sourceAddr", "1069100020341", properties.getSms().getMt().getSourceAddr());
check("sms_serviceCode", "PUSH", properties.getSms().getMt().getServiceCode());
check("sms_appkey", "ak-BFq4NN0TMf92J7KAioNYGz74", properties.getSms().getMt().getAppkey());
check("sms_flag", "【南华大学附属第一医院】", properties.getSms().getMt().getFlag());
// ---- 阿里云 ----
check("accessKeyId", "LTAI5tTestKeyId", properties.getSms().getAliyun().getAccessKeyId());
check("accessKeySecret", "testSecret", properties.getSms().getAliyun().getAccessKeySecret());
check("signName", "杭州利湖科技", properties.getSms().getAliyun().getSignName());
// ---- 新增项应回落到默认值 ----
check("message.sms.shiyi.ws-url 默认值", "http://192.178.0.104:7790/MediInfoHis.svc",
properties.getSms().getShiyi().getWsUrl());
check("message.sms.messaging-insert.ws-url 默认值", "http://218.108.11.29:8081/MessagingInsert.asmx",
properties.getSms().getMessagingInsert().getWsUrl());
check("message.wechat.shiyi.url 默认值", "http://192.200.54.14:5003/sfjk/SendMessage",
properties.getWechat().getShiyi().getUrl());
check("message.wechat.xinhua.service 默认值", "smarthos.pat.notice",
properties.getWechat().getXinhua().getService());
System.out.println();
if (failed == 0) {
System.out.println("===== 配置绑定校验全部通过 =====");
} else {
System.out.println("===== 配置绑定校验失败 " + failed + " 项 =====");
System.exit(1);
}
}
private static void check(String key, String expected, String actual) {
boolean ok = expected == null ? actual == null : expected.equals(actual);
if (!ok) {
failed++;
}
System.out.println((ok ? "[OK] " : "[FAIL] ") + key
+ " 期望=" + expected + " 实际=" + actual);
}
}