package org.springblade.common.config;

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

import java.util.Date;

/**
 * Description:
 *
 * @author ggp
 * @date 2021/8/20 14:34
 */
@Slf4j
@Component
public class MyBatisMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateTime",  Date.class, new Date());
		Long userId = AuthUtil.getUserId();
		if (userId!=null){
			this.strictInsertFill(metaObject, "create_user_id", Long.class, userId);
			this.strictUpdateFill(metaObject, "update_user_id",  Long.class, userId);
		}
	}

    @Override
    public void updateFill(MetaObject metaObject) {

    	this.setFieldValByName("updateTime", new Date(), metaObject);
		Long userId = AuthUtil.getUserId();
		if (userId!=null){
			this.setFieldValByName("update_user_id", userId, metaObject);
		}
    }
}
