package com.njcn.db.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.njcn.web.utils.RequestUtil;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 数据自动填充处理器
 *
 * @author hongawen
 * @version 1.0.0
 * @date 2021年12月14日 16:36
 */

public class AutoFillValueHandler implements MetaObjectHandler {

    private static final String CREATE_USER = "createBy";

    private static final String CREATE_TIME = "createTime";

    private static final String UPDATE_USER = "updateBy";

    private static final String UPDATE_TIME = "updateTime";


    /**
     * 执行元数据需要插入的值添加
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, CREATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, CREATE_USER, RequestUtil::getUserIndex, String.class);
        this.strictInsertFill(metaObject, UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, UPDATE_USER, RequestUtil::getUserIndex, String.class);
    }

    /**
     * 执行元数据需要更新的数据更新
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, UPDATE_USER, RequestUtil::getUserIndex, String.class);
        this.strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime::now, LocalDateTime.class);
    }

}
