李林
2024-04-06 c7e24959364e65e6632e71546d607433560bb403
framework/src/main/java/com/yuanchu/mom/mybatis_config/MyMetaObjectHandler.java
@@ -1,22 +1,44 @@
package com.yuanchu.mom.mybatis_config;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yuanchu.mom.utils.JackSonUtil;
import com.yuanchu.mom.utils.Jwt;
import com.yuanchu.mom.utils.ServletUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    Jwt jwt;
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
        Integer userId = null;
        try {
            userId = Integer.parseInt(JSONUtil.parseObj(jwt.readJWT(ServletUtils.getRequest().getHeader("token")).get("data")).get("id") + "");
        }catch (Exception ignored){}
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject, "createUser", Integer.class, userId);
        this.strictInsertFill(metaObject, "updateUser", Integer.class, userId);
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        Integer userId = null;
        try {
            userId = Integer.parseInt(JSONUtil.parseObj(jwt.readJWT(ServletUtils.getRequest().getHeader("token")).get("data")).get("id") + "");
        }catch (Exception ignored){}
        // update的时候使用,更新的时候强制进行填充
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐)
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        this.strictUpdateFill(metaObject, "updateUser", Integer.class, userId);
    }
}