package com.fzm.mall.server.admin.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author wangp
 * @date 2021/2/26 18:13
 * @description 自动填充create|update_time字段
 * @since JDK 1.8
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Long currentTime = SystemClock.now();
        if (metaObject.hasSetter("createTime")) {
            this.setFieldValByName("createTime", currentTime, metaObject);
        }
        if (metaObject.hasSetter("updateTime")) {
            this.setFieldValByName("updateTime", currentTime, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long currentTime = SystemClock.now();
        if (metaObject.hasSetter("updateTime")) {
            this.setFieldValByName("updateTime", currentTime, metaObject);
        }
    }
}
