From 3642f33f90ec70ccb2afd1ec006f8bd70cc2c68a Mon Sep 17 00:00:00 2001 From: Hamm Date: Thu, 6 Jun 2024 17:01:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(BUG):=20=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B7=B2=E7=9F=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../airpower/root/RootEntityController.java | 73 +++++++++++-------- .../cn/hamm/airpower/root/RootService.java | 53 +++++++------- 2 files changed, 68 insertions(+), 58 deletions(-) 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 a53822c..14410ee 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); } /** @@ -170,7 +177,8 @@ public class RootEntityController< public Json getList(@RequestBody QueryRequest queryRequest) { queryRequest = getQueryRequest(queryRequest); checkApiAvailableStatus(Api.GetList); - return Json.data(afterGetList(service.getList(beforeGetList(queryRequest)))); + queryRequest = beforeGetList(queryRequest).copy(); + return Json.data(afterGetList(service.getList(queryRequest))); } /** @@ -186,7 +194,8 @@ public class RootEntityController< public Json getPage(@RequestBody QueryPageRequest queryPageRequest) { queryPageRequest = getQueryRequest(queryPageRequest); checkApiAvailableStatus(Api.GetPage); - return Json.data(afterGetPage(service.getPage(beforeGetPage(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 c7be38b..365d441 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(); -- Gitee From b178d6e2eb409365e84a93cea5c4d899b2fda131 Mon Sep 17 00:00:00 2001 From: Hamm Date: Thu, 6 Jun 2024 17:07:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(BUG):=20=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B7=B2=E7=9F=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hamm/airpower/root/RootEntityController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 14410ee..9742d61 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 @@ -175,8 +175,8 @@ public class RootEntityController< @RequestMapping("getList") @Filter(WhenGetList.class) public Json getList(@RequestBody QueryRequest queryRequest) { - queryRequest = getQueryRequest(queryRequest); checkApiAvailableStatus(Api.GetList); + queryRequest = getQueryRequest(queryRequest); queryRequest = beforeGetList(queryRequest).copy(); return Json.data(afterGetList(service.getList(queryRequest))); } @@ -192,8 +192,8 @@ public class RootEntityController< @RequestMapping("getPage") @Filter(WhenGetPage.class) public Json getPage(@RequestBody QueryPageRequest queryPageRequest) { - queryPageRequest = getQueryRequest(queryPageRequest); checkApiAvailableStatus(Api.GetPage); + queryPageRequest = getQueryRequest(queryPageRequest); queryPageRequest = (QueryPageRequest) beforeGetPage(queryPageRequest).copy(); return Json.data(afterGetPage(service.getPage(queryPageRequest))); } -- Gitee