package com.chushang.common.mybatis.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.chushang.common.core.context.SecurityContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.util.ClassUtils;

import java.nio.charset.Charset;
import java.time.LocalDateTime;

/**
 * MybatisPlus 自动填充配置
 *
 * @author L.cm
 */
@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
	/**
	 * 遵循严格填充
	 * @param metaObject 元对象
	 */
	@Override
	public void insertFill(MetaObject metaObject) {
		if(log.isDebugEnabled()){
			log.debug("mybatis plus start insert fill ....");
		}
		// 修改人, 创建人
		// 用于填充 创建人以及修改人
//		String userName = SecurityContextHolder.getUserName();
		String userName = "system";
		this.fillStrategy(metaObject, "createBy", userName);
		// 自动填充下 版本号
		this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
//		this.strictInsertFill(metaObject, "createBy", String.class, userName);
	}

	/**
	 * 遵循严格填充
	 * @param metaObject 元对象
	 */
	@Override
	public void updateFill(MetaObject metaObject) {
		log.debug("mybatis plus start update fill ....");
//		String userName = SecurityContextHolder.getUserName();
		String userName = "system";
		this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
		this.strictUpdateFill(metaObject, "updateBy", String.class, userName);
	}

}
