package com.zfire.mall.manager.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zfire.mall.manager.utils.CommonUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * 自动补充插入或更新时的值
 */
@Component
public class MpMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时的填充策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createBy", CommonUtils.getNickName(),metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", CommonUtils.getNickName(),metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        this.setFieldValByName("updateBy", CommonUtils.getNickName(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
