doum
2026-05-26 f4d592f3626f94117d8a4eb22176a28290931980
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.device.model.ElectronicConstant;
import com.doumee.core.utils.Constants;
import com.doumee.dao.system.model.SystemDictData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
 
/**
 * 从数据字典 ELECTRICAL_PARAM 加载智能电表第三方平台连接参数。
 */
@Slf4j
@Service
public class ElectronicConfigService {
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @PostConstruct
    public void init() {
        refreshFromDict();
    }
 
    /** 重新从数据字典加载配置(修改字典后重启服务生效,或主动调用刷新) */
    public void refreshFromDict() {
        ElectronicConstant.auth_code = read(Constants.ELECTRICAL_AUTH_CODE, ElectronicConstant.DEFAULT_AUTH_CODE);
        ElectronicConstant.nonce = read(Constants.ELECTRICAL_NONCE, ElectronicConstant.DEFAULT_NONCE);
        ElectronicConstant.notify_url = read(Constants.ELECTRICAL_NOTIFY_URL, ElectronicConstant.DEFAULT_NOTIFY_URL);
        ElectronicConstant.api_url = read(Constants.ELECTRICAL_API_URL, ElectronicConstant.DEFAULT_API_URL);
        ElectronicConstant.api2_url = read(Constants.ELECTRICAL_API2_URL, ElectronicConstant.DEFAULT_API2_URL);
        log.info("electrical config loaded from dict {}, api_url={}, api2_url={}, notify_url={}",
                Constants.ELECTRICAL_PARAM, ElectronicConstant.api_url, ElectronicConstant.api2_url, ElectronicConstant.notify_url);
    }
 
    private String read(String label, String defaultValue) {
        try {
            SystemDictData data = systemDictDataBiz.queryByCode(Constants.ELECTRICAL_PARAM, label);
            if (data != null && StringUtils.isNotBlank(data.getCode())) {
                return data.getCode().trim();
            }
            log.warn("electrical config [{}] empty, use default", label);
        } catch (Exception e) {
            log.warn("electrical config [{}] load failed, use default", label, e);
        }
        return defaultValue;
    }
}