package com.trade.admin.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.trade.admin.system.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        String userNickName = null;
        try {
            userNickName = SecurityUtils.getUserNickName();
        } catch (Exception e) {
            userNickName = "system";
        }
        if (metaObject.hasGetter("createBy") && metaObject.hasGetter("updateBy")) {
            setFieldValByName("createBy", userNickName, metaObject);
            setFieldValByName("updateBy", userNickName, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userNickName = null;
        try {
            userNickName = SecurityUtils.getUserNickName();
        } catch (Exception e) {
            userNickName = "system";
        }
        if (metaObject.hasGetter("updateBy")) {
            setFieldValByName("updateBy", userNickName, metaObject);
        }
    }
}
