package com.blueland.mybatisplus;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.blueland.core.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * MP注入处理器
 *
 * @author Lion Li
 * @date 2021/4/25
 */
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		try {
			//根据属性名字设置要填充的值
			if (metaObject.hasGetter("appId") && ObjectUtil.isEmpty(metaObject.getValue("appId"))) {
				this.setFieldValByName("appId", UserUtils.getAppId(), metaObject);
			}
		} catch (Exception e) {
			throw new RuntimeException("自动注入异常 => " + e.getMessage());
		}
		updateFill(metaObject);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		try {
			//根据属性名字设置要填充的值
			if (metaObject.hasGetter("appId") && ObjectUtil.isEmpty(metaObject.getValue("appId"))) {
				this.setFieldValByName("appId", UserUtils.getAppId(), metaObject);
			}
		} catch (Exception e) {
			throw new RuntimeException("自动注入异常 => " + e.getMessage());
		}
	}
}
