浏览代码

优化属性设置逻辑

zhouhao 7 年之前
父节点
当前提交
48636aeeb8

+ 10 - 8
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java

@@ -53,16 +53,10 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
      */
     protected abstract IDGenerator<PK> getIDGenerator();
 
-//    @Override
-//    public abstract CrudDao<E, PK> getDao();
-
     @Override
     public int deleteByPk(PK pk) {
         Assert.notNull(pk, "parameter can not be null");
         return getDao().deleteByPk(pk);
-//        return createDelete()
-//                .where(GenericEntity.id, pk)
-//                .exec();
     }
 
     @Override
@@ -93,7 +87,7 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
 
     @Override
     public PK saveOrUpdate(E entity) {
-        if (null != entity.getId() && null != selectByPk(entity.getId())) {
+        if (dataExisted(entity)) {
             updateByPk(entity);
         } else {
             insert(entity);
@@ -101,6 +95,10 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
         return entity.getId();
     }
 
+    protected boolean dataExisted(E entity) {
+        return null != entity.getId() && null != selectByPk(entity.getId());
+    }
+
     @Override
     public PK insert(E entity) {
         if (entity.getId() != null) {
@@ -109,10 +107,12 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
             }
             tryValidateProperty(selectByPk(entity.getId()) == null, "id", entity.getId() + "已存在");
         }
-
         if (entity.getId() == null && getIDGenerator() != null) {
             entity.setId(getIDGenerator().generate());
         }
+        if (entity instanceof RecordCreationEntity) {
+            ((RecordCreationEntity) entity).setCreateTimeNow();
+        }
         tryValidate(entity, CreateGroup.class);
         getDao().insert(entity);
         return entity.getId();
@@ -128,10 +128,12 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
     }
 
     @Override
+    @Transactional(readOnly = true)
     public List<E> selectByPk(List<PK> id) {
         if (id == null || id.isEmpty()) {
             return new ArrayList<>();
         }
         return createQuery().where().in(GenericEntity.id, id).listNoPaging();
     }
+
 }

+ 1 - 0
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-api/src/main/java/org/hswebframework/web/dashboard/DashBoardConfigEntity.java

@@ -17,6 +17,7 @@ public class DashBoardConfigEntity extends SimpleGenericEntity<String> implement
     @NotBlank(groups = CreateGroup.class)
     private String name;
 
+    @NotBlank(groups = CreateGroup.class)
     private String type;
 
     private String template;

+ 6 - 1
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/DashBoardConfigController.java

@@ -35,10 +35,15 @@ public class DashBoardConfigController implements SimpleGenericEntityController<
     @Override
     public ResponseMessage<String> add(@RequestBody DashBoardConfigEntity data) {
         Authentication.current().ifPresent(a -> data.setCreatorId(a.getUser().getId()));
-        data.setCreateTimeNow();
         return SimpleGenericEntityController.super.add(data);
     }
 
+    @Override
+    public ResponseMessage<String> saveOrUpdate(DashBoardConfigEntity data) {
+        Authentication.current().ifPresent(a -> data.setCreatorId(a.getUser().getId()));
+        return SimpleGenericEntityController.super.saveOrUpdate(data);
+    }
+
     @GetMapping("{id}/execute")
     @Authorize
     @ApiOperation("执行仪表盘配置")