package com.jiayue.ipp.common.mybatis;

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

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 处理新增和更新的审计字段数据填充
 *
 * @author L.ym
 * @date 2021-04-27
 **/
@Component
public class MpMetaObjectHandler implements MetaObjectHandler {

	private final String creator = "creator";
	private final String createTime = "createTime";
	private final String lastModifier = "lastModifier";
	private final String lastModifyTime = "lastModifyTime";
	private final String delFlag = "delFlag";

	@Override
	public void insertFill(MetaObject metaObject) {

//		Authentication authentication = SecurityUtils.getAuthentication();
//		Optional<String> operator = Optional.of(authentication == null ? "admin" : authentication.getName());

		this.strictInsertFill(metaObject, creator, String.class, "admin"); // 起始版本 3.3.0(推荐使用)

		this.strictInsertFill(metaObject, createTime, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)

		this.strictInsertFill(metaObject, lastModifier, String.class, "admin"); // 起始版本 3.3.0(推荐使用)

		this.strictInsertFill(metaObject, lastModifyTime, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)

		this.strictInsertFill(metaObject, delFlag, String.class, "0"); // 起始版本 3.3.0(推荐使用)



	}

	@Override
	public void updateFill(MetaObject metaObject) {
//		Authentication authentication = SecurityUtils.getAuthentication();
//		Optional<String> operator = Optional.of(authentication == null ? "admin" : authentication.getName());


		this.strictInsertFill(metaObject, lastModifier, String.class, "admin"); // 起始版本 3.3.0(推荐使用)

		this.strictInsertFill(metaObject, lastModifyTime, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)


	}
}