package com.gree.mall.miniapp.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gree.mall.miniapp.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


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

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //log.info("【更新填充】:{}",CommonUtils.getNickName());
        this.setFieldValByName("updateBy", CommonUtils.getNickName(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
