package com.seer.teach.common.config;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @Author: Captain
 * @Autograph: 安稳
 * @Description:
 * @Date: 2023-07-25 23:16:15
 */
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            Object loginId = StpUtil.getLoginIdDefaultNull();
            if (loginId != null) {
                this.setFieldValByName("createBy", loginId.toString(), metaObject);
            }
        } catch (Exception e) {
        }
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            Object loginId = StpUtil.getLoginIdDefaultNull();
            if (loginId != null) {
                this.setFieldValByName("updateBy", loginId.toString(), metaObject);
            }
        } catch (Exception e) {
        }
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}