package com.ruoyi.framework.config;

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

import java.util.Date;

/**
 * 时间,用户等通用字段自动生成
 * @Date: 2022/05/09 23:13
 * @Version: 1.0
 */
@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        this.strictInsertFill(metaObject, "createDate", Date::new, Date.class);
        this.strictInsertFill(metaObject, "updateDate", Date::new, Date.class);
//        this.strictInsertFill(metaObject, "delFlag", String.class,"0");

        String loginName = ShiroUtils.getSysUser().getLoginName();
        if (StringUtils.isEmpty(loginName)) return;
        this.strictInsertFill(metaObject, "createBy", String.class, loginName);
        this.strictInsertFill(metaObject, "updateBy", String.class, loginName);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateDate", Date::new, Date.class);
        String loginName = ShiroUtils.getSysUser().getLoginName();
        if (StringUtils.isEmpty(loginName)) return;
        this.strictInsertFill(metaObject, "updateBy", String.class, loginName);
    }
}
