package cn.ezhizao.framework.mybaits;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import cn.ezhizao.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;


/**
 * @author 朱汕
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final String CREATE_BY_FIELD_NAME = "creatorId";
    private static final String CREATE_TIME_FIELD_NAME = "createTime";
    private static final String UPDATE_BY_FIELD_NAME = "updaterId";
    private static final String UPDATE_TIME_FIELD_NAME = "updateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName(CREATE_BY_FIELD_NAME, SecurityUtils.getUserId()==null?1:SecurityUtils.getUserId(), metaObject);
        this.setFieldValByName(CREATE_TIME_FIELD_NAME, new Timestamp(System.currentTimeMillis()), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName(UPDATE_BY_FIELD_NAME,SecurityUtils.getUserId()==null?1:SecurityUtils.getUserId(),metaObject);
        this.setFieldValByName(UPDATE_TIME_FIELD_NAME, new Timestamp(System.currentTimeMillis()), metaObject);
    }
}
