Browse Source

优化数据字典

zhouhao 6 years ago
parent
commit
02e82401a4
11 changed files with 205 additions and 22 deletions
  1. 7 2
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/DictionaryItemService.java
  2. 5 0
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/DictionaryService.java
  3. 23 8
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/entity/SimpleDictionaryItemEntity.java
  4. 14 0
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/events/ClearDictionaryCacheEvent.java
  5. 23 2
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/java/org/hswebframework/web/dictionary/simple/BoostDictDefineRepository.java
  6. 44 0
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/java/org/hswebframework/web/dictionary/simple/SimpleDictionaryItemService.java
  7. 29 1
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/java/org/hswebframework/web/dictionary/simple/SimpleDictionaryService.java
  8. 2 4
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/dictionary/DictionaryItemMapper.xml
  9. 4 4
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-starter/src/main/resources/hsweb-starter.js
  10. 5 1
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-web/src/main/java/org/hswebframework/web/controller/dictionary/DictionaryController.java
  11. 49 0
      hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-web/src/main/java/org/hswebframework/web/controller/dictionary/DictionaryItemController.java

+ 7 - 2
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/DictionaryItemService.java

@@ -18,12 +18,17 @@ package org.hswebframework.web.dictionary.api;
 
 import org.hswebframework.web.dictionary.api.entity.DictionaryItemEntity;
 import org.hswebframework.web.service.CrudService;
+import org.hswebframework.web.service.TreeService;
+
+import java.util.List;
 
 /**
- *  数据字典选项 服务类
+ * 数据字典选项 服务类
  *
  * @author hsweb-generator-online
  */
-public interface DictionaryItemService extends CrudService<DictionaryItemEntity, String> {
+public interface DictionaryItemService extends TreeService<DictionaryItemEntity, String>
+        , CrudService<DictionaryItemEntity, String> {
 
+    List<DictionaryItemEntity> selectByDictId(String dictId);
 }

+ 5 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/DictionaryService.java

@@ -17,8 +17,11 @@
 package org.hswebframework.web.dictionary.api;
 
 import org.hswebframework.web.dictionary.api.entity.DictionaryEntity;
+import org.hswebframework.web.dictionary.api.entity.DictionaryItemEntity;
 import org.hswebframework.web.service.CrudService;
 
+import java.util.List;
+
 /**
  *  数据字典 服务类
  *
@@ -26,4 +29,6 @@ import org.hswebframework.web.service.CrudService;
  */
 public interface DictionaryService extends CrudService<DictionaryEntity, String> {
 
+
+
 }

+ 23 - 8
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/entity/SimpleDictionaryItemEntity.java

@@ -16,9 +16,13 @@
  */
 package org.hswebframework.web.dictionary.api.entity;
 
+import com.alibaba.fastjson.JSONObject;
 import lombok.Getter;
 import lombok.Setter;
+import org.hibernate.validator.constraints.Length;
+import org.hibernate.validator.constraints.NotBlank;
 import org.hswebframework.web.commons.entity.SimpleTreeSortSupportEntity;
+import org.hswebframework.web.validator.group.CreateGroup;
 
 import java.util.List;
 
@@ -31,6 +35,7 @@ import java.util.List;
 @Setter
 public class SimpleDictionaryItemEntity extends SimpleTreeSortSupportEntity<String> implements DictionaryItemEntity {
     //字典id
+    @NotBlank(groups = CreateGroup.class)
     private String dictId;
     //名称
     private String name;
@@ -41,21 +46,31 @@ public class SimpleDictionaryItemEntity extends SimpleTreeSortSupportEntity<Stri
     //字典值类型
     private String valueType;
     //是否启用
-    private Byte status;
+    private Byte   status;
     //说明
     private String describe;
     //快速搜索码
     private String searchCode;
 
-    // 使用表达式拼接text
-    // #value+'('+#context.otherVal+')'
-    private String textExpression;
-
-    private String valueExpression;
-
     private Integer ordinal;
 
     private List<DictionaryItemEntity> children;
 
-
+    @Override
+    public Object getWriteJSONObject() {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("id", getId());
+        jsonObject.put("name", getName());
+        jsonObject.put("dictId", getDictId());
+        jsonObject.put("value", getValue());
+        jsonObject.put("text", getText());
+        jsonObject.put("ordinal", getOrdinal());
+        jsonObject.put("sortIndex", getSortIndex());
+        jsonObject.put("path", getPath());
+        jsonObject.put("mask", getMask());
+        jsonObject.put("searchCode", getSearchCode());
+        jsonObject.put("status", getStatus());
+        jsonObject.put("describe", getDescribe());
+        return jsonObject;
+    }
 }

+ 14 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-api/src/main/java/org/hswebframework/web/dictionary/api/events/ClearDictionaryCacheEvent.java

@@ -0,0 +1,14 @@
+package org.hswebframework.web.dictionary.api.events;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author zhouhao
+ * @since 3.0.0-RC
+ */
+@AllArgsConstructor
+@Getter
+public class ClearDictionaryCacheEvent {
+    private String dictionaryId;
+}

+ 23 - 2
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/java/org/hswebframework/web/dictionary/simple/BoostDictDefineRepository.java

@@ -1,15 +1,22 @@
 package org.hswebframework.web.dictionary.simple;
 
+import lombok.extern.slf4j.Slf4j;
+import org.hswebframework.web.commons.entity.DataStatus;
 import org.hswebframework.web.dict.DictDefine;
 import org.hswebframework.web.dict.EnumDict;
 import org.hswebframework.web.dict.defaults.DefaultDictDefine;
 import org.hswebframework.web.dict.defaults.DefaultDictDefineRepository;
+import org.hswebframework.web.dictionary.api.DictionaryItemService;
 import org.hswebframework.web.dictionary.api.DictionaryService;
 import org.hswebframework.web.dictionary.api.entity.DictionaryEntity;
+import org.hswebframework.web.dictionary.api.events.ClearDictionaryCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.event.TransactionalEventListener;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -21,19 +28,33 @@ import java.util.stream.Collectors;
  */
 @Service
 @CacheConfig(cacheNames = "dictionary")
+@Slf4j
+@SuppressWarnings("all")
 public class BoostDictDefineRepository extends DefaultDictDefineRepository {
 
     @Autowired
     private DictionaryService dictionaryService;
 
+    @Autowired
+    private DictionaryItemService itemService;
+
+    @TransactionalEventListener
+    @CacheEvict(key = "'dictDefineById:'+#event.dictionaryId", condition = "#event!=null")
+    public void clearCache(ClearDictionaryCacheEvent event) {
+        log.info("clear dictionary define cache:{}", event.getDictionaryId());
+    }
+
     @Override
-    @Cacheable(key = "'DictDefineById:'+#id")
+    @Cacheable(key = "'dictDefineById:'+#id")
     public DictDefine getDefine(String id) {
         DictionaryEntity entity = dictionaryService.selectByPk(id);
         if (entity == null) {
             return super.getDefine(id);
         }
-        List<EnumDict<Object>> items = (List) new ArrayList<>(entity.getItems());
+        List<EnumDict<Object>> items = (List) itemService.selectByDictId(id)
+                .stream()
+                .filter(e -> DataStatus.STATUS_ENABLED.equals(e.getStatus()))
+                .collect(Collectors.toList());
 
         return DefaultDictDefine.builder()
                 .id(id)

+ 44 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/java/org/hswebframework/web/dictionary/simple/SimpleDictionaryItemService.java

@@ -16,14 +16,20 @@
  */
 package org.hswebframework.web.dictionary.simple;
 
+import org.hswebframework.utils.StringUtils;
 import org.hswebframework.web.dictionary.api.DictionaryItemService;
 import org.hswebframework.web.dictionary.api.entity.DictionaryItemEntity;
+import org.hswebframework.web.dictionary.api.events.ClearDictionaryCacheEvent;
 import org.hswebframework.web.dictionary.simple.dao.DictionaryItemDao;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.AbstractTreeSortService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 
+import java.util.Collections;
+import java.util.List;
+
 /**
  * 默认的服务实现
  *
@@ -35,6 +41,9 @@ public class SimpleDictionaryItemService extends AbstractTreeSortService<Diction
     @Autowired
     private DictionaryItemDao dictionaryItemDao;
 
+    @Autowired
+    private ApplicationEventPublisher eventPublisher;
+
     @Override
     protected IDGenerator<String> getIDGenerator() {
         return IDGenerator.MD5;
@@ -45,4 +54,39 @@ public class SimpleDictionaryItemService extends AbstractTreeSortService<Diction
         return dictionaryItemDao;
     }
 
+    @Override
+    public String insert(DictionaryItemEntity entity) {
+        clearDictCache(entity.getDictId());
+        return super.insert(entity);
+    }
+
+    @Override
+    public int updateByPk(String id, DictionaryItemEntity entity) {
+        clearDictCache(entity.getDictId());
+        return super.updateByPk(id, entity);
+    }
+
+    @Override
+    public int deleteByPk(String id) {
+        DictionaryItemEntity entity = selectByPk(id);
+        if (null != entity) {
+            clearDictCache(entity.getDictId());
+        }
+        return super.deleteByPk(id);
+    }
+
+    @Override
+    public List<DictionaryItemEntity> selectByDictId(String dictId) {
+        if (StringUtils.isNullOrEmpty(dictId)) {
+            return Collections.emptyList();
+        }
+        return createQuery()
+                .where(DictionaryItemEntity.dictId, dictId)
+                .orderByAsc(DictionaryItemEntity.sortIndex)
+                .listNoPaging();
+    }
+
+    private void clearDictCache(String dictId) {
+        eventPublisher.publishEvent(new ClearDictionaryCacheEvent(dictId));
+    }
 }

+ 29 - 1
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/java/org/hswebframework/web/dictionary/simple/SimpleDictionaryService.java

@@ -18,12 +18,19 @@ package org.hswebframework.web.dictionary.simple;
 
 import org.hswebframework.web.dictionary.api.DictionaryService;
 import org.hswebframework.web.dictionary.api.entity.DictionaryEntity;
+import org.hswebframework.web.dictionary.api.events.ClearDictionaryCacheEvent;
 import org.hswebframework.web.dictionary.simple.dao.DictionaryDao;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.EnableCacheAllEvictGenericEntityService;
+import org.hswebframework.web.service.EnableCacheGenericEntityService;
+import org.hswebframework.web.service.GenericEntityService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Caching;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.event.TransactionalEventListener;
 
 /**
  * 默认的服务实现
@@ -32,11 +39,15 @@ import org.springframework.stereotype.Service;
  */
 @Service("dictionaryService")
 @CacheConfig(cacheNames = "dictionary")
-public class SimpleDictionaryService extends EnableCacheAllEvictGenericEntityService<DictionaryEntity, String>
+public class SimpleDictionaryService extends GenericEntityService<DictionaryEntity, String>
         implements DictionaryService {
+
     @Autowired
     private DictionaryDao dictionaryDao;
 
+    @Autowired
+    private ApplicationEventPublisher eventPublisher;
+
     @Override
     protected IDGenerator<String> getIDGenerator() {
         return IDGenerator.MD5;
@@ -47,5 +58,22 @@ public class SimpleDictionaryService extends EnableCacheAllEvictGenericEntitySer
         return dictionaryDao;
     }
 
+    @Override
+    public String insert(DictionaryEntity entity) {
+        String id = super.insert(entity);
+        eventPublisher.publishEvent(new ClearDictionaryCacheEvent(id));
+        return id;
+    }
+
+    @Override
+    public int updateByPk(String id, DictionaryEntity entity) {
+        eventPublisher.publishEvent(new ClearDictionaryCacheEvent(id));
+        return super.updateByPk(id, entity);
+    }
 
+    @Override
+    public int deleteByPk(String id) {
+        eventPublisher.publishEvent(new ClearDictionaryCacheEvent(id));
+        return super.deleteByPk(id);
+    }
 }

+ 2 - 4
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-local/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/dictionary/DictionaryItemMapper.xml

@@ -32,10 +32,8 @@
         <result property="path" column="path" javaType="String" jdbcType="VARCHAR"/>
         <result property="searchCode" column="search_code" javaType="String" jdbcType="VARCHAR"/>
         <result property="sortIndex" column="sort_index" javaType="Long" jdbcType="DECIMAL"/>
-        <result property="level" column="level" javaType="Long" jdbcType="DECIMAL"/>
-        <result property="properties" column="properties" javaType="Map" jdbcType="CLOB"/>
-        <result property="valueExpression" column="value_expression" javaType="String" jdbcType="CLOB"/>
-        <result property="textExpression" column="text_expression" javaType="String" jdbcType="CLOB"/>
+        <result property="level" column="level_" javaType="Long" jdbcType="DECIMAL"/>
+        <result property="ordinal" column="ordinal" javaType="Integer" jdbcType="DECIMAL"/>
         <result property="properties" column="properties" javaType="Map" jdbcType="CLOB"/>
     </resultMap>
 

+ 4 - 4
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-starter/src/main/resources/hsweb-starter.js

@@ -34,6 +34,7 @@ var versions = [
     // }
 ];
 var JDBCType = java.sql.JDBCType;
+
 function install(context) {
     var database = context.database;
     database.createOrAlter("s_dictionary")
@@ -53,16 +54,15 @@ function install(context) {
         .addColumn().name("value").alias("value").comment("字典值").jdbcType(java.sql.JDBCType.VARCHAR).length(64).commit()
         .addColumn().name("text").alias("text").comment("字典文本").jdbcType(java.sql.JDBCType.VARCHAR).length(128).commit()
         .addColumn().name("value_type").alias("valueType").comment("字典值类型").jdbcType(java.sql.JDBCType.VARCHAR).length(64).commit()
-        .addColumn().name("is_status").alias("status").comment("状态").jdbcType(java.sql.JDBCType.DECIMAL).length(4, 0).commit()
+        .addColumn().name("status").alias("status").comment("状态").jdbcType(java.sql.JDBCType.DECIMAL).length(4, 0).commit()
         .addColumn().name("describe").alias("describe").comment("说明").jdbcType(java.sql.JDBCType.VARCHAR).length(128).commit()
         .addColumn().name("parent_id").alias("parentId").comment("父级选项").jdbcType(java.sql.JDBCType.VARCHAR).length(32).commit()
         .addColumn().name("path").alias("path").comment("树编码").jdbcType(java.sql.JDBCType.VARCHAR).length(3000).commit()
         .addColumn().name("search_code").alias("searchCode").comment("快速搜索码").jdbcType(java.sql.JDBCType.VARCHAR).length(128).commit()
         .addColumn().name("sort_index").alias("sortIndex").comment("排序索引").jdbcType(java.sql.JDBCType.DECIMAL).length(32, 0).commit()
-        .addColumn().name("level").alias("level").comment("树结构层级").jdbcType(java.sql.JDBCType.DECIMAL).length(32, 0).commit()
+        .addColumn().name("level_").alias("level").comment("树结构层级").jdbcType(java.sql.JDBCType.DECIMAL).length(32, 0).commit()
+        .addColumn().name("ordinal").alias("ordinal").comment("识别码").jdbcType(java.sql.JDBCType.DECIMAL).length(32, 0).commit()
         .addColumn().name("properties").alias("properties").comment("其他自定义属性").jdbcType(java.sql.JDBCType.CLOB).commit()
-        .addColumn().name("text_expression").alias("textExpression").comment("文本表达式").jdbcType(java.sql.JDBCType.CLOB).commit()
-        .addColumn().name("value_expression").alias("valueExpression").comment("值表达式").jdbcType(java.sql.JDBCType.CLOB).commit()
         .comment("数据字典解析配置").commit();
 
     database.createOrAlter("s_dict_parser")

+ 5 - 1
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-web/src/main/java/org/hswebframework/web/controller/dictionary/DictionaryController.java

@@ -27,9 +27,14 @@ import org.hswebframework.web.dict.DictDefine;
 import org.hswebframework.web.dict.DictDefineRepository;
 import org.hswebframework.web.dict.EnumDict;
 import org.hswebframework.web.dict.ItemDefine;
+import org.hswebframework.web.dictionary.api.DictionaryItemService;
 import org.hswebframework.web.dictionary.api.DictionaryService;
 import org.hswebframework.web.dictionary.api.entity.DictionaryEntity;
+import org.hswebframework.web.dictionary.api.events.ClearDictionaryCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.context.event.EventListener;
+import org.springframework.transaction.event.TransactionalEventListener;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -70,7 +75,6 @@ public class DictionaryController implements SimpleGenericEntityController<Dicti
         return ok(repository.getAllDefine());
     }
 
-
     @GetMapping("/define/{id:.+}")
     @Authorize(merge = false)
     @ApiOperation("获取数据字典定义信息")

+ 49 - 0
hsweb-system/hsweb-system-dictionary/hsweb-system-dictionary-web/src/main/java/org/hswebframework/web/controller/dictionary/DictionaryItemController.java

@@ -0,0 +1,49 @@
+/*
+ *  Copyright 2016 http://www.hswebframework.org
+ *  
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *  
+ */
+
+package org.hswebframework.web.controller.dictionary;
+
+import io.swagger.annotations.Api;
+import org.hswebframework.web.authorization.annotation.Authorize;
+import org.hswebframework.web.commons.entity.param.QueryParamEntity;
+import org.hswebframework.web.controller.SimpleGenericEntityController;
+import org.hswebframework.web.dictionary.api.DictionaryItemService;
+import org.hswebframework.web.dictionary.api.entity.DictionaryItemEntity;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 数据字典
+ *
+ * @author hsweb-generator-online
+ */
+@RestController
+@RequestMapping("${hsweb.web.mappings.dictionary-item:dictionary-item}")
+@Authorize(permission = "dictionary", description = "数据字典管理")
+@Api(value = "数据字典", tags = "数据字典-字典配置")
+public class DictionaryItemController implements SimpleGenericEntityController<DictionaryItemEntity, String, QueryParamEntity> {
+
+    @Autowired
+    private DictionaryItemService dictionaryService;
+
+    @Override
+    public DictionaryItemService getService() {
+        return dictionaryService;
+    }
+
+}