package com.yunyan.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yunyan.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        try {
            if (SecurityUtils.getLoginUser() != null) {
                this.setFieldValByName("createBy", SecurityUtils.getLoginUser().getUser().getNickName(), metaObject);
            }
        } catch (Exception e) {
            log.debug("获取用户异常");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        try {
            if (SecurityUtils.getLoginUser() != null) {
                this.setFieldValByName("updateBy", SecurityUtils.getLoginUser().getUser().getNickName(), metaObject);
            }
        } catch (Exception e) {
            log.debug("获取用户异常");
        }

    }
}
