|
|
@@ -0,0 +1,99 @@
|
|
|
+package com.usoftchina.saas.commons.cache;
|
|
|
+
|
|
|
+
|
|
|
+import com.usoftchina.saas.base.Result;
|
|
|
+import com.usoftchina.saas.cache.RedisHashCache;
|
|
|
+import com.usoftchina.saas.commons.api.ConfigsApi;
|
|
|
+import com.usoftchina.saas.commons.po.Configs;
|
|
|
+import com.usoftchina.saas.context.BaseContextHolder;
|
|
|
+import com.usoftchina.saas.context.SpringContextHolder;
|
|
|
+import com.usoftchina.saas.exception.BizException;
|
|
|
+import com.usoftchina.saas.utils.JsonUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description 系统参数配置
|
|
|
+ * @Author chenwei
|
|
|
+ * @Date 2019/01/14
|
|
|
+ */
|
|
|
+public class ConfigsCache extends RedisHashCache<String, String, String> {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 企业ID
|
|
|
+ */
|
|
|
+ private Long id;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ConfigsApi configsApi;
|
|
|
+
|
|
|
+ private static final RedisTemplate<String, String> REDIS_TEMPLATE = SpringContextHolder.getBean("redisTemplate", RedisTemplate.class);
|
|
|
+
|
|
|
+ public ConfigsCache() {
|
|
|
+ super(() -> REDIS_TEMPLATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ConfigsCache of(Long id){
|
|
|
+ ConfigsCache cache = new ConfigsCache();
|
|
|
+ cache.id = id;
|
|
|
+ return cache;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前企业的缓存信息
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ConfigsCache current() {
|
|
|
+ return of(BaseContextHolder.getCompanyId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected String field() {
|
|
|
+ return String.valueOf("companyId:" + id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected String key() {
|
|
|
+ return generatePublicKey("common", "configs");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Supplier<String> getSupplier() {
|
|
|
+ return () -> {
|
|
|
+ if (null == configsApi) {
|
|
|
+ configsApi = SpringContextHolder.getBean(ConfigsApi.class);
|
|
|
+ }
|
|
|
+ Result<List<Configs>> result = null;
|
|
|
+ if (null != id && id > 0) {
|
|
|
+ result = configsApi.getConfigsByCompanyId(id);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ return JsonUtils.toJsonString(result.getData());
|
|
|
+ }
|
|
|
+ throw new BizException(result.getCode(), result.getMessage());
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Configs> getConfigs(){
|
|
|
+ Optional<String> value = get();
|
|
|
+ if (value.isPresent()){
|
|
|
+ return JsonUtils.fromJsonArray(value.get(), Configs.class);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否启用B2B
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean enableB2B(){
|
|
|
+ return getConfigs().stream().filter(configs -> "enableB2B".equals(configs.getCode()) && "1".equals(configs.getData())).count() > 0;
|
|
|
+ }
|
|
|
+}
|