| | |
| | | import com.zy.asrs.service.BasStationPathProfileService; |
| | | import com.zy.asrs.service.BasStationPathRuleService; |
| | | import com.zy.asrs.service.StationPathPolicyService; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | private BasStationPathProfileService basStationPathProfileService; |
| | | @Autowired |
| | | private BasStationPathRuleService basStationPathRuleService; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | private volatile CacheSnapshot cacheSnapshot = new CacheSnapshot(); |
| | | private volatile long cacheTime = 0L; |
| | |
| | | public StationPathResolvedPolicy resolvePolicy(Integer startStationId, Integer endStationId) { |
| | | StationPathResolvedPolicy resolved = new StationPathResolvedPolicy(); |
| | | resolved.setScoreMode("twoStage"); |
| | | resolved.setDefaultProfileCode(getSystemConfig("stationPathDefaultProfileCode", "default")); |
| | | |
| | | CacheSnapshot snapshot = getCacheSnapshot(); |
| | | resolved.setDefaultProfileCode(snapshot.defaultProfileCode); |
| | | BasStationPathRule matchedRule = matchRule(snapshot.ruleList, startStationId, endStationId); |
| | | BasStationPathProfile matchedProfile = null; |
| | | if (matchedRule != null && notBlank(matchedRule.getProfileCode())) { |
| | |
| | | try { |
| | | List<BasStationPathProfile> profiles = basStationPathProfileService.list(new QueryWrapper<BasStationPathProfile>() |
| | | .eq("status", 1) |
| | | .orderByDesc("is_default") |
| | | .orderByAsc("priority", "id")); |
| | | if (profiles != null) { |
| | | snapshot.profileList.addAll(profiles); |
| | |
| | | snapshot.profileMap.put(profile.getProfileCode(), profile); |
| | | } |
| | | } |
| | | snapshot.defaultProfileCode = resolveDefaultProfileCode(profiles); |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("加载站点路径模板失败,回退默认配置: {}", e.getMessage()); |
| | |
| | | return config; |
| | | } |
| | | |
| | | private String getSystemConfig(String code, String defaultValue) { |
| | | try { |
| | | Object mapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key); |
| | | if (mapObj instanceof Map) { |
| | | Object value = ((Map<?, ?>) mapObj).get(code); |
| | | if (value != null) { |
| | | String text = String.valueOf(value).trim(); |
| | | if (!text.isEmpty()) { |
| | | return text; |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ignore) { |
| | | private String resolveDefaultProfileCode(List<BasStationPathProfile> profiles) { |
| | | if (profiles == null || profiles.isEmpty()) { |
| | | return "default"; |
| | | } |
| | | return defaultValue; |
| | | for (BasStationPathProfile profile : profiles) { |
| | | if (profile != null |
| | | && Short.valueOf((short) 1).equals(profile.getIsDefault()) |
| | | && notBlank(profile.getProfileCode())) { |
| | | return profile.getProfileCode(); |
| | | } |
| | | } |
| | | for (BasStationPathProfile profile : profiles) { |
| | | if (profile != null && "default".equals(profile.getProfileCode())) { |
| | | return profile.getProfileCode(); |
| | | } |
| | | } |
| | | for (BasStationPathProfile profile : profiles) { |
| | | if (profile != null && notBlank(profile.getProfileCode())) { |
| | | return profile.getProfileCode(); |
| | | } |
| | | } |
| | | return "default"; |
| | | } |
| | | |
| | | private boolean notBlank(String value) { |
| | |
| | | |
| | | private static class CacheSnapshot { |
| | | private boolean loaded = false; |
| | | private String defaultProfileCode = "default"; |
| | | private final List<BasStationPathProfile> profileList = new ArrayList<>(); |
| | | private final List<BasStationPathRule> ruleList = new ArrayList<>(); |
| | | private final Map<String, BasStationPathProfile> profileMap = new HashMap<>(); |