chenhj
5 天以前 73260a50749d5782c18771b98ab4e72499e31eda
ruoyi-common/src/main/java/com/ruoyi/common/handler/MyMetaObjectHandler.java
@@ -7,23 +7,38 @@
import java.time.LocalDateTime;
/**
 * @Author: zhangxy
 * @Date: 2020-08-05 14:40
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        this.strictInsertFill(metaObject, "createBy", Long.class, userId);
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // 判断字段是否存在,存在才填充
        if (metaObject.hasSetter("createBy")) {
            Long userId = 0L;
            try {
                if (SecurityUtils.getLoginUser() != null) {
                    userId = SecurityUtils.getLoginUser().getUserId();
                    this.strictInsertFill(metaObject, "createBy", Long.class, userId);
                }
            } catch (Exception ignored) {
            }
        }
        if (metaObject.hasSetter("createTime")) {
            this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        }
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        this.strictInsertFill(metaObject, "updateBy", Long.class, userId);
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 判断字段是否存在,存在才填充
        if (metaObject.hasSetter("updateBy")) {
            Long userId = SecurityUtils.getLoginUser().getUserId();
            this.strictUpdateFill(metaObject, "updateBy", Long.class, userId);
        }
        if (metaObject.hasSetter("updateTime")) {
            this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        }
    }
}
}