diff --git a/airpower-core/src/main/java/cn/hamm/airpower/root/RootEntityController.java b/airpower-core/src/main/java/cn/hamm/airpower/root/RootEntityController.java index a53822c83e8f1670fff8b0c5a70a4c47c141c4af..9742d61d11b3f32e34c7e117eb8a7fc945264e06 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/root/RootEntityController.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/root/RootEntityController.java @@ -52,15 +52,17 @@ public class RootEntityController< @Description("添加") @RequestMapping("add") @Filter(WhenGetDetail.class) - public Json add(@RequestBody @Validated(WhenAdd.class) E entity) { + public Json add(@RequestBody @Validated(WhenAdd.class) E source) { checkApiAvailableStatus(Api.Add); - service.ignoreReadOnlyFields(entity); - long insertId = service.add(beforeAdd(entity)); + service.ignoreReadOnlyFields(source); + source = beforeAdd(source).copy(); + final E finalSource = source; + long id = service.add(source); execute( - () -> afterAdd(insertId, entity), - () -> afterSaved(insertId, entity) + () -> afterAdd(id, finalSource), + () -> afterSaved(id, finalSource) ); - return Json.entity(insertId, MessageConstant.SUCCESS_TO_ADD); + return Json.entity(id, MessageConstant.SUCCESS_TO_ADD); } /** @@ -74,15 +76,18 @@ public class RootEntityController< @Description("修改") @RequestMapping("update") @Filter(WhenGetDetail.class) - public Json update(@RequestBody @Validated(WhenUpdate.class) @NotNull E entity) { + public Json update(@RequestBody @Validated(WhenUpdate.class) @NotNull E source) { checkApiAvailableStatus(Api.Update); - long updateId = entity.getId(); - service.update(beforeUpdate(service.ignoreReadOnlyFields(entity))); + long id = source.getId(); + service.ignoreReadOnlyFields(source); + source = beforeUpdate(source).copy(); + final E finalSource = source; + service.update(source); execute( - () -> afterUpdate(updateId, entity), - () -> afterSaved(updateId, entity) + () -> afterUpdate(id, finalSource), + () -> afterSaved(id, finalSource) ); - return Json.entity(updateId, MessageConstant.SUCCESS_TO_UPDATE); + return Json.entity(id, MessageConstant.SUCCESS_TO_UPDATE); } /** @@ -94,15 +99,15 @@ public class RootEntityController< */ @Description("删除") @RequestMapping("delete") - public Json delete(@RequestBody @Validated(WhenIdRequired.class) @NotNull E entity) { + public Json delete(@RequestBody @Validated(WhenIdRequired.class) @NotNull E source) { checkApiAvailableStatus(Api.Delete); - long deleteId = entity.getId(); - beforeDelete(deleteId); - service.delete(deleteId); + long id = source.getId(); + beforeDelete(id); + service.delete(id); execute( - () -> afterDelete(deleteId) + () -> afterDelete(id) ); - return Json.entity(deleteId, MessageConstant.SUCCESS_TO_DELETE); + return Json.entity(id, MessageConstant.SUCCESS_TO_DELETE); } /** @@ -114,9 +119,9 @@ public class RootEntityController< @Description("查询详情") @RequestMapping("getDetail") @Filter(WhenGetDetail.class) - public Json getDetail(@RequestBody @Validated(WhenIdRequired.class) @NotNull E entity) { + public Json getDetail(@RequestBody @Validated(WhenIdRequired.class) @NotNull E source) { checkApiAvailableStatus(Api.GetDetail); - return Json.data(afterGetDetail(service.get(entity.getId()))); + return Json.data(afterGetDetail(service.get(source.getId()))); } /** @@ -128,14 +133,15 @@ public class RootEntityController< */ @Description("禁用") @RequestMapping("disable") - public Json disable(@RequestBody @Validated(WhenIdRequired.class) @NotNull E entity) { + public Json disable(@RequestBody @Validated(WhenIdRequired.class) @NotNull E source) { checkApiAvailableStatus(Api.Disable); - beforeDisable(entity.getId()); - service.disable(entity.getId()); + long id = source.getId(); + beforeDisable(id); + service.disable(id); execute( - () -> afterDisable(entity.getId()) + () -> afterDisable(id) ); - return Json.entity(entity.getId(), MessageConstant.SUCCESS_TO_DISABLE); + return Json.entity(source.getId(), MessageConstant.SUCCESS_TO_DISABLE); } /** @@ -147,14 +153,15 @@ public class RootEntityController< */ @Description("启用") @RequestMapping("enable") - public Json enable(@RequestBody @Validated(WhenIdRequired.class) @NotNull E entity) { + public Json enable(@RequestBody @Validated(WhenIdRequired.class) @NotNull E source) { checkApiAvailableStatus(Api.Enable); - beforeEnable(entity.getId()); - service.enable(entity.getId()); + long id = source.getId(); + beforeEnable(id); + service.enable(id); execute( - () -> afterEnable(entity.getId()) + () -> afterEnable(id) ); - return Json.entity(entity.getId(), MessageConstant.SUCCESS_TO_ENABLE); + return Json.entity(source.getId(), MessageConstant.SUCCESS_TO_ENABLE); } /** @@ -168,9 +175,10 @@ public class RootEntityController< @RequestMapping("getList") @Filter(WhenGetList.class) public Json getList(@RequestBody QueryRequest queryRequest) { - queryRequest = getQueryRequest(queryRequest); checkApiAvailableStatus(Api.GetList); - return Json.data(afterGetList(service.getList(beforeGetList(queryRequest)))); + queryRequest = getQueryRequest(queryRequest); + queryRequest = beforeGetList(queryRequest).copy(); + return Json.data(afterGetList(service.getList(queryRequest))); } /** @@ -184,9 +192,10 @@ public class RootEntityController< @RequestMapping("getPage") @Filter(WhenGetPage.class) public Json getPage(@RequestBody QueryPageRequest queryPageRequest) { - queryPageRequest = getQueryRequest(queryPageRequest); checkApiAvailableStatus(Api.GetPage); - return Json.data(afterGetPage(service.getPage(beforeGetPage(queryPageRequest)))); + queryPageRequest = getQueryRequest(queryPageRequest); + queryPageRequest = (QueryPageRequest) beforeGetPage(queryPageRequest).copy(); + return Json.data(afterGetPage(service.getPage(queryPageRequest))); } /** diff --git a/airpower-core/src/main/java/cn/hamm/airpower/root/RootService.java b/airpower-core/src/main/java/cn/hamm/airpower/root/RootService.java index c7be38bdaa1d0048c57c41c925379ea52a37f3e8..365d441abf14991bb868a098b7e404953c86c479 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/root/RootService.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/root/RootService.java @@ -73,14 +73,14 @@ public class RootService, R extends RootRepository> i * @see #afterSaved(long, E) */ public final long add(@NotNull E source) { - source = beforeAdd(source); + source = beforeAdd(source).copy(); ServiceError.SERVICE_ERROR.whenNull(source, MessageConstant.DATA_MUST_NOT_NULL); source.setId(null).setIsDisabled(false).setCreateTime(System.currentTimeMillis()); if (Objects.isNull(source.getRemark())) { source.setRemark(Constant.EMPTY_STRING); } - long id = saveToDatabase(source); E finalSource = source; + long id = saveToDatabase(source); execute(() -> afterAdd(id, finalSource)); return id; } @@ -115,13 +115,7 @@ public class RootService, R extends RootRepository> i * @see #updateWithNull(E) */ public final void update(@NotNull E source) { - source = beforeUpdate(source); - updateToDatabase(source); - E finalSource = source; - execute( - () -> afterUpdate(finalSource.getId(), finalSource), - () -> afterSaved(finalSource.getId(), finalSource) - ); + updateToDatabase(false, source); } /** @@ -136,17 +130,7 @@ public class RootService, R extends RootRepository> i */ @SuppressWarnings("unused") public final void updateWithNull(@NotNull E source) { - ServiceError.PARAM_MISSING.whenNull(source.getId(), String.format( - MessageConstant.MISSING_ID_WHEN_UPDATE, - Utils.getReflectUtil().getDescription(getEntityClass()) - )); - source = beforeUpdate(source); - updateToDatabase(source, true); - E finalSource = source; - execute( - () -> afterUpdate(finalSource.getId(), finalSource), - () -> afterSaved(finalSource.getId(), finalSource) - ); + updateToDatabase(true, source); } /** @@ -293,7 +277,7 @@ public class RootService, R extends RootRepository> i public final @NotNull List getList(QueryRequest queryRequest) { queryRequest = Objects.requireNonNullElse(queryRequest, new QueryPageRequest<>()); queryRequest.setFilter(Objects.requireNonNullElse(queryRequest.getFilter(), getNewInstance())); - queryRequest = beforeGetList(queryRequest); + queryRequest = beforeGetList(queryRequest).copy(); List list = repository.findAll( createSpecification(queryRequest.getFilter(), false), createSort(queryRequest.getSort()) ); @@ -415,7 +399,7 @@ public class RootService, R extends RootRepository> i public final @NotNull QueryPageResponse getPage(QueryPageRequest queryPageRequest) { queryPageRequest = Objects.requireNonNullElse(queryPageRequest, new QueryPageRequest<>()); queryPageRequest.setFilter(Objects.requireNonNullElse(queryPageRequest.getFilter(), getNewInstance())); - queryPageRequest = beforeGetPage(queryPageRequest); + queryPageRequest = (QueryPageRequest) beforeGetPage(queryPageRequest).copy(); org.springframework.data.domain.Page pageData = repository.findAll( createSpecification(queryPageRequest.getFilter(), false), createPageable(queryPageRequest) ); @@ -477,10 +461,9 @@ public class RootService, R extends RootRepository> i } /** - *

🔴更新到数据库

+ *

🔴更新到数据库(不触发前后置)

* * @param source 原始实体 - * @apiNote 🔴请注意,此方法不会触发前后置 * @see #update(E) * @see #updateWithNull(E) */ @@ -489,7 +472,7 @@ public class RootService, R extends RootRepository> i } /** - *

🔴更新到数据库

+ *

🔴更新到数据库(触发前后置)

* * @param source 原始实体 * @param withNull 是否更新空值 @@ -506,6 +489,24 @@ public class RootService, R extends RootRepository> i saveToDatabase(source, withNull); } + + /** + *

🔴更新到数据库

+ * + * @param withNull 是否更新 null 属性 + * @param source 原始数据 + */ + private void updateToDatabase(boolean withNull, @NotNull E source) { + long id = source.getId(); + source = beforeUpdate(source).copy(); + updateToDatabase(source, withNull); + E finalSource = source; + execute( + () -> afterUpdate(id, finalSource), + () -> afterSaved(id, finalSource) + ); + } + /** *

根据ID查询对应的实体

* @@ -603,7 +604,7 @@ public class RootService, R extends RootRepository> i private long saveAndFlush(@NotNull E entity) { E target = getNewInstance(); BeanUtils.copyProperties(entity, target); - target = beforeSaveToDatabase(target); + target = beforeSaveToDatabase(target).copy(); target = repository.saveAndFlush(target); // 新增完毕,清掉查询缓存,避免查询到旧数据 Utils.getEntityManager().clear();