| | |
| | | package com.ruoyi.common.handler; |
| | | |
| | | import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import org.apache.ibatis.reflection.MetaObject; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.time.OffsetDateTime; |
| | | |
| | | /** |
| | | * @Author: zhangxy |
| | | * @Date: 2020-08-05 14:40 |
| | | */ |
| | | @Component |
| | | public class MyMetaObjectHandler implements MetaObjectHandler { |
| | | |
| | | @Override |
| | | public void insertFill(MetaObject metaObject) { |
| | | // 判断字段是否存在,存在才填充 |
| | | if (metaObject.hasSetter("createBy")) { |
| | | Long userId; |
| | | try { |
| | | if (SecurityUtils.getLoginUser() != null) { |
| | | userId = SecurityUtils.getLoginUser().getUserId(); |
| | | this.strictInsertFill(metaObject, "createBy", String.class, userId.toString()); |
| | | } |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void insertFill(MetaObject metaObject) { |
| | | this.setFieldValByName("createTime", LocalDateTime.now(), metaObject); |
| | | this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject); |
| | | // 开线程,取不到user |
| | | try { |
| | | // SysUser currentUser = SecurityUtils.getLoginUser(); |
| | | // if (currentUser != null) { |
| | | // this.setFieldValByName("createUser", currentUser.getUsername(), metaObject); |
| | | // this.setFieldValByName("updateUser", currentUser.getUsername(), metaObject); |
| | | // } |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | | if (metaObject.hasSetter("createTime")) { |
| | | this.strictInsertFill(metaObject, "createTime", OffsetDateTime.class, OffsetDateTime.now()); |
| | | } |
| | | |
| | | @Override |
| | | public void updateFill(MetaObject metaObject) { |
| | | this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject); |
| | | try { |
| | | // ZttUser currentUser = SecurityUtils.getUser(); |
| | | // if (currentUser != null) { |
| | | // this.setFieldValByName("updateUser", currentUser.getUsername(), metaObject); |
| | | // } |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | | } |
| | | if (metaObject.hasSetter("updateTime")) { |
| | | this.strictUpdateFill(metaObject, "updateTime", OffsetDateTime.class, OffsetDateTime.now()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateFill(MetaObject metaObject) { |
| | | // 判断字段是否存在,存在才填充 |
| | | if (metaObject.hasSetter("updateBy")) { |
| | | Long userId = SecurityUtils.getLoginUser().getUserId(); |
| | | this.strictUpdateFill(metaObject, "updateBy", String.class, userId.toString()); |
| | | } |
| | | |
| | | if (metaObject.hasSetter("updateTime")) { |
| | | this.strictUpdateFill(metaObject, "updateTime", OffsetDateTime.class, OffsetDateTime.now()); |
| | | } |
| | | } |
| | | } |