Commit a7a6b60b by zhuht

[chg]增加门店配置集团级配置的适配;

1 parent 02c382e1
package com.viontech.keliu.dao;
import com.viontech.keliu.entity.SConfigParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
......@@ -23,17 +22,46 @@ import java.util.stream.Collectors;
public class ConfigParamDao {
@Resource
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public String getConfigParamByMallIdAndConfigKey(Long mallId, String configKey) {
String sql = "select mall_id, config_key, config_value from s_config_params where (mall_id = ? or mall_id is null) and config_key = ? order by mall_id asc limit 1;";
SConfigParam configParam = null;
try {
configParam = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SConfigParam.class), mallId, configKey);
} catch (Exception e) {
log.error("获取配置异常", e);
List<SConfigParam> mallConfigParams = getMallConfigParams(mallId, Collections.singletonList(configKey));
if (mallConfigParams.isEmpty()) {
return null;
}
return mallConfigParams.get(0).getConfigValue();
}
public List<SConfigParam> getMallConfigParams(Long mallId, List<String> configKeys) {
Map<String, Object> params = new HashMap<>();
params.put("mallId", mallId);
params.put("configKeys", configKeys);
String sql = "SELECT * FROM s_config_params WHERE config_key IN (:configKeys) " +
"AND ((account_id IS NULL AND mall_id IS NULL) " +
"OR (account_id = (SELECT account_id FROM b_mall WHERE id = :mallId) AND mall_id IS NULL) " +
"OR (mall_id = :mallId));";
// 门店配置,集团配置,系统配置
List<SConfigParam> sConfigParams = namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<>(SConfigParam.class));
return new ArrayList<>(sConfigParams.stream().collect(Collectors.toMap(SConfigParam::getConfigKey, config -> config,
this::mergeByPriority)).values());
}
/**
* @param config 配置
* @return 配置的使用优先级。数字越小,优先级越高。门店级最高
*/
private int getLevel(SConfigParam config) {
if (config.getMallId() != null) {
return 1;
}
if (config.getAccountId() != null) {
return 2;
}
return configParam == null ? null : configParam.getConfigValue();
return 3;
}
private SConfigParam mergeByPriority(SConfigParam c1, SConfigParam c2) {
return getLevel(c1) <= getLevel(c2) ? c1 : c2;
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import lombok.Data;
@Data
public class SConfigParam {
private Long accountId;
private Long mallId;
private String configKey;
private String configValue;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!