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; + } +}