package com.ruoyi.framework.config.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 实体类自动配置创建日期和更新日期
 *
 * @author 600477
 * @date 2023/6/6
 */
@Component
public class MyMetaHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createBy", SecurityUtils.getLoginUser().getUser().getNickName() + '-'+ SecurityUtils.getUsername(), metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", SecurityUtils.getLoginUser().getUser().getNickName() + '-'+ SecurityUtils.getUsername(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", SecurityUtils.getLoginUser().getUser().getNickName() + '-'+ SecurityUtils.getUsername(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
