package com.huimv.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>
 *  mybatis-plus 自动填充时间
 * </p>
 * @author yinhao
 * @since 2021/4/21
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        //起始版本 3.3.0(推荐使用)
        Date fieldVal = new Date();
        this.strictInsertFill(metaObject, "gmtCreate", Date.class, fieldVal);
        this.strictUpdateFill(metaObject, "gmtModified", Date.class, fieldVal);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 起始版本 3.3.0(推荐)
        this.strictUpdateFill(metaObject, "gmtModified", Date.class, new Date());

    }
}