From 48563663e1fed93154f63f6c9a6c07ea79d741da Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 17 Nov 2024 00:09:57 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=BF=BD=E7=95=A5=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/MyBatisPlusMetaObjectHandler.java | 61 ++++++++----------- .../common/context/UserContextHolder.java | 6 +- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/continew-common/src/main/java/top/continew/admin/common/config/mybatis/MyBatisPlusMetaObjectHandler.java b/continew-common/src/main/java/top/continew/admin/common/config/mybatis/MyBatisPlusMetaObjectHandler.java index ed0d0b7d1..fc67499ae 100644 --- a/continew-common/src/main/java/top/continew/admin/common/config/mybatis/MyBatisPlusMetaObjectHandler.java +++ b/continew-common/src/main/java/top/continew/admin/common/config/mybatis/MyBatisPlusMetaObjectHandler.java @@ -20,7 +20,6 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import top.continew.admin.common.context.UserContextHolder; -import top.continew.starter.core.exception.BusinessException; import top.continew.starter.extension.crud.model.entity.BaseDO; import java.time.LocalDateTime; @@ -57,23 +56,19 @@ public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler { */ @Override public void insertFill(MetaObject metaObject) { - try { - if (null == metaObject) { - return; - } - Long createUser = UserContextHolder.getUserId(); - LocalDateTime createTime = LocalDateTime.now(); - if (metaObject.getOriginalObject() instanceof BaseDO baseDO) { - // 继承了 BaseDO 的类,填充创建信息字段 - baseDO.setCreateUser(ObjectUtil.defaultIfNull(baseDO.getCreateUser(), createUser)); - baseDO.setCreateTime(ObjectUtil.defaultIfNull(baseDO.getCreateTime(), createTime)); - } else { - // 未继承 BaseDO 的类,如存在创建信息字段则进行填充 - this.fillFieldValue(metaObject, CREATE_USER, createUser, false); - this.fillFieldValue(metaObject, CREATE_TIME, createTime, false); - } - } catch (Exception e) { - throw new BusinessException("插入数据时自动填充异常:" + e.getMessage()); + if (null == metaObject) { + return; + } + Long createUser = UserContextHolder.getUserId(); + LocalDateTime createTime = LocalDateTime.now(); + if (metaObject.getOriginalObject() instanceof BaseDO baseDO) { + // 继承了 BaseDO 的类,填充创建信息字段 + baseDO.setCreateUser(ObjectUtil.defaultIfNull(baseDO.getCreateUser(), createUser)); + baseDO.setCreateTime(ObjectUtil.defaultIfNull(baseDO.getCreateTime(), createTime)); + } else { + // 未继承 BaseDO 的类,如存在创建信息字段则进行填充 + this.fillFieldValue(metaObject, CREATE_USER, createUser, false); + this.fillFieldValue(metaObject, CREATE_TIME, createTime, false); } } @@ -84,23 +79,19 @@ public void insertFill(MetaObject metaObject) { */ @Override public void updateFill(MetaObject metaObject) { - try { - if (null == metaObject) { - return; - } - Long updateUser = UserContextHolder.getUserId(); - LocalDateTime updateTime = LocalDateTime.now(); - if (metaObject.getOriginalObject() instanceof BaseDO baseDO) { - // 继承了 BaseDO 的类,填充修改信息 - baseDO.setUpdateUser(updateUser); - baseDO.setUpdateTime(updateTime); - } else { - // 未继承 BaseDO 的类,根据类中拥有的修改信息字段进行填充,不存在修改信息字段不进行填充 - this.fillFieldValue(metaObject, UPDATE_USER, updateUser, true); - this.fillFieldValue(metaObject, UPDATE_TIME, updateTime, true); - } - } catch (Exception e) { - throw new BusinessException("修改数据时自动填充异常:" + e.getMessage()); + if (null == metaObject) { + return; + } + Long updateUser = UserContextHolder.getUserId(); + LocalDateTime updateTime = LocalDateTime.now(); + if (metaObject.getOriginalObject() instanceof BaseDO baseDO) { + // 继承了 BaseDO 的类,填充修改信息 + baseDO.setUpdateUser(updateUser); + baseDO.setUpdateTime(updateTime); + } else { + // 未继承 BaseDO 的类,根据类中拥有的修改信息字段进行填充,不存在修改信息字段不进行填充 + this.fillFieldValue(metaObject, UPDATE_USER, updateUser, true); + this.fillFieldValue(metaObject, UPDATE_TIME, updateTime, true); } } diff --git a/continew-common/src/main/java/top/continew/admin/common/context/UserContextHolder.java b/continew-common/src/main/java/top/continew/admin/common/context/UserContextHolder.java index d460d7bae..8ae375bf5 100644 --- a/continew-common/src/main/java/top/continew/admin/common/context/UserContextHolder.java +++ b/continew-common/src/main/java/top/continew/admin/common/context/UserContextHolder.java @@ -23,8 +23,6 @@ import top.continew.starter.core.util.ExceptionUtils; import top.continew.starter.extension.crud.service.CommonUserService; -import java.util.Optional; - /** * 用户上下文 Holder * @@ -142,7 +140,7 @@ public static void clearContext() { * @return 用户 ID */ public static Long getUserId() { - return Optional.ofNullable(getContext()).map(UserContext::getId).orElse(null); + return ExceptionUtils.exToNull(() -> getContext().getId()); } /** @@ -151,7 +149,7 @@ public static Long getUserId() { * @return 用户名 */ public static String getUsername() { - return Optional.ofNullable(getContext()).map(UserContext::getUsername).orElse(null); + return ExceptionUtils.exToNull(() -> getContext().getUsername()); } /**