From dcae3adfd8390040f6f6e4ba7cb9245e9393b38e Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Mon, 25 Nov 2024 18:31:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20=E5=A2=9E=E5=8A=A0=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=AE=9E=E4=BD=93=E7=9A=84=E6=96=B9=E4=BE=BF=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gringotts/web/helper/EntityHelper.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 gringotts-web/src/main/java/com/eshore/gringotts/web/helper/EntityHelper.java diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/helper/EntityHelper.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/helper/EntityHelper.java new file mode 100644 index 0000000..9234f4e --- /dev/null +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/helper/EntityHelper.java @@ -0,0 +1,32 @@ +package com.eshore.gringotts.web.helper; + +import cn.hutool.core.util.ObjectUtil; +import com.eshore.gringotts.web.domain.base.entity.SimpleEntity; +import com.eshore.gringotts.web.domain.user.entity.User; +import com.eshore.gringotts.web.domain.user.service.UserService; +import java.util.function.Supplier; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * JPA工具 + * + * @author lanyuanxiaoyao + * @date 2024-11-25 + */ +public class EntityHelper { + public static void deleteIfUpdateNeeded(JpaRepository repository, Supplier getter, E target) { + E old = getter.get(); + if (ObjectUtil.notEqual(old, target)) { + repository.delete(old); + } + } + + public static E fillCreatorAndModifier(E entity, UserService service) { + User user = service.currentLoginUser(); + if (ObjectUtil.isNull(entity.getCreatedUser())) { + entity.setCreatedUser(user); + } + entity.setModifiedUser(user); + return entity; + } +}