package com.ruoyi.framework.config;

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;

@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        if (SecurityUtils.getLoginUser() != null) {
            if (SecurityUtils.getLoginUser().getDeptId() != null) {
                this.setFieldValByName("deptId", SecurityUtils.getLoginUser().getDeptId(), metaObject);
            }
            this.setFieldValByName("createBy", SecurityUtils.getLoginUser().getUsername(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
        if (SecurityUtils.getLoginUser() != null) {
            this.setFieldValByName("updateBy", SecurityUtils.getLoginUser().getUsername(), metaObject);
        }
    }
}