package com.xunwang.servicebase.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 属性名称
        this.setFieldValByName("firstLoginDatetime", generaDate() , metaObject);
        this.setFieldValByName("lastLoginDatetime", generaDate(), metaObject);
        this.setFieldValByName("createTime", generaDate(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("lastLoginDatetime", generaDate(), metaObject);
    }

    private String generaDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date());
    }
}
