package com.ruoyi.framework.handle;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.domain.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Duke_yzl
 * @date 20230611.01
 * @describe：
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "loginTime", Date.class, DateTime.now());
        this.strictInsertFill(metaObject, "createTime", Date.class, DateTime.now());
        this.strictInsertFill(metaObject, "updateTime", Date.class, DateTime.now());
        this.strictInsertFill(metaObject, "delFlag", String.class, "0");
        try{
            String userName = ((LoginUser) StpUtil.getSession().get(StpUtil.getTokenValue())).getUser().getUserName();
            this.strictInsertFill(metaObject, "createBy", String.class, userName);
            this.strictInsertFill(metaObject, "updateBy", String.class, userName);
        }catch (Exception e){}

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateTime.now());
        try{
            String userName = ((LoginUser) StpUtil.getSession().get(StpUtil.getTokenValue())).getUser().getUserName();
            this.strictInsertFill(metaObject, "updateBy", String.class, userName);
        }catch (Exception e){}
    }


}