package com.ruoyi.outpatientvisit.config; import com.ruoyi.common.core.service.IConfigService; import com.ruoyi.outpatientvisit.OuthospVisitModule; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 门急诊随访模块总开关:值存放在 {@code sys_config} 参数配置表中,运行时读取、可热切换。 * *

参数配置

*
 * 参数键名:outpatientvisit.enabled
 * 参数键值:true  = 启用(默认)
 *          false = 关闭(门诊随访不再生成子任务)
 * 
* *

与出入院模块的开关对齐

* 本类与 {@code InhospVisitSwitch} / {@code PublicitySwitch} 是同一套实现 * (10 秒进程内缓存、fail-open、运行时热读),刻意保持形状一致,便于统一理解与排查。 * *

为什么读不到配置也按 true(fail-open)

* 读 {@code sys_config} 要走数据库,数据库抖动时 fail-closed 会让当天门诊随访整体停摆; * 而要停随访的正确做法是显式把参数设为 false(或到「定时任务」里暂停对应 job)。 */ @Slf4j @Component public class OuthospVisitSwitch { /** * sys_config 里的参数键名(与 {@link OuthospVisitModule#SWITCH_KEY} 保持一致) */ public static final String CONFIG_KEY = OuthospVisitModule.SWITCH_KEY; /** * 进程内缓存有效期(毫秒) */ private static final long CACHE_TTL_MILLIS = 10_000L; private final OuthospVisitProperties properties; private final ObjectProvider configServiceProvider; private volatile Boolean cachedEnabled; private volatile long cachedAt; /** * Spring 使用的构造器。 *

* ⚠️ 必须显式标注 {@code @Autowired}:本类有两个公开构造器, * 不标注的话 Spring 会因"构造器不唯一"退回查找无参构造器,启动即报 * {@code No default constructor found}。—— 这是满意度模块实机踩到的坑。 */ @Autowired public OuthospVisitSwitch(OuthospVisitProperties properties, ObjectProvider configServiceProvider) { this.properties = properties; this.configServiceProvider = configServiceProvider; } /** * 供测试/非 Spring 环境直接构造:{@code configValue} 就是 sys_config 里的键值, * 传 {@code null} 表示"参数行不存在"(此时回落到 {@link OuthospVisitProperties})。 */ public OuthospVisitSwitch(OuthospVisitProperties properties, final String configValue) { this(properties, new FixedProvider(configValue)); this.cachedEnabled = resolve(); this.cachedAt = System.currentTimeMillis() + CACHE_TTL_MILLIS * 1000L; } /** * 门诊随访功能是否启用(运行时热读,最多 10 秒延迟) */ public boolean isEnabled() { long now = System.currentTimeMillis(); Boolean cached = this.cachedEnabled; if (cached != null && now - this.cachedAt < CACHE_TTL_MILLIS) { return cached; } boolean resolved = resolve(); this.cachedEnabled = resolved; this.cachedAt = now; return resolved; } /** * 清掉进程内缓存,下次调用立即重新读 sys_config(供测试/需要立即生效的场景) */ public void invalidateCache() { this.cachedEnabled = null; } private boolean resolve() { String value = readFromSysConfig(); if (value != null) { if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) { log.warn("【门急诊随访】sys_config 中 {} 的值不是 true/false(实际=「{}」),将按「不是 false 即启用」处理", CONFIG_KEY, value); } boolean enabled = !"false".equalsIgnoreCase(value); log.debug("【门急诊随访】开关取自 sys_config:{}={} -> enabled={}", CONFIG_KEY, value, enabled); return enabled; } boolean enabled = properties.isEnabled(); log.debug("【门急诊随访】sys_config 中未配置 {},回落 outpatientvisit.enabled={}", CONFIG_KEY, enabled); return enabled; } /** * 读 sys_config 的原始键值;没有实现、没有这一行、或读库失败都返回 {@code null} */ private String readFromSysConfig() { if (configServiceProvider == null) { return null; } try { IConfigService configService = configServiceProvider.stream().findFirst().orElse(null); if (configService == null) { return null; } String value = configService.selectConfigByKey(CONFIG_KEY); if (value == null || value.trim().isEmpty()) { return null; } return value.trim(); } catch (Exception e) { log.warn("【门急诊随访】读取 sys_config 的 {} 失败,本次按未配置处理(fail-open):{}", CONFIG_KEY, e.toString()); return null; } } /** * 固定值的 {@code ObjectProvider}:只用于非 Spring 环境/测试 */ private static final class FixedProvider implements ObjectProvider { private final IConfigService configService; private FixedProvider(final String configValue) { this.configService = configValue == null ? null : configKey -> configValue; } @Override public IConfigService getObject(Object... args) { return configService; } @Override public IConfigService getIfAvailable() { return configService; } @Override public IConfigService getIfUnique() { return configService; } @Override public IConfigService getObject() { return configService; } @Override public java.util.stream.Stream stream() { return configService == null ? java.util.stream.Stream.empty() : java.util.stream.Stream.of(configService); } } }