package com.supervision.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.supervision.config.domain.UserInfo;
import com.supervision.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MybatisMetaHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("填充创建和开始时间以及新增用户");
        // 填充
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        UserInfo user = UserUtil.getUser();
        if (ObjectUtil.isNotEmpty(user)) {
            this.strictInsertFill(metaObject, "createUserId", String.class, user.getId());
            this.strictInsertFill(metaObject, "updateUserId", String.class, user.getId());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("填充修改时间以及修改用户");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        UserInfo user = UserUtil.getUser();
        if (ObjectUtil.isNotEmpty(user)) {
            this.strictInsertFill(metaObject, "updateUserId", String.class, user.getId());
        }
    }
}
