package cn.pluss.platform.config;

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

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final String UPDATE_TIME = "updateTime";
    private static final String CREATE_TIME = "createTime";

    private static final String UPDATE_DT = "updateDt";
    private static final String CREATE_DT = "createDt";

    @Override
    public void insertFill(MetaObject metaObject) {
        strictInsertFill(metaObject, CREATE_TIME, LocalDateTime.class, LocalDateTime.now());
        strictInsertFill(metaObject, CREATE_TIME, Date.class, new Date());

        strictInsertFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
        strictInsertFill(metaObject, UPDATE_TIME, Date.class, new Date());

        strictInsertFill(metaObject, CREATE_DT, LocalDateTime.class, LocalDateTime.now());
        strictInsertFill(metaObject, CREATE_DT, Date.class, new Date());

        strictInsertFill(metaObject, UPDATE_DT, LocalDateTime.class, LocalDateTime.now());
        strictInsertFill(metaObject, UPDATE_DT, Date.class, new Date());
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
        strictUpdateFill(metaObject, UPDATE_TIME, Date.class, new Date());

        strictUpdateFill(metaObject, UPDATE_DT, LocalDateTime.class, LocalDateTime.now());
        strictUpdateFill(metaObject, UPDATE_DT, Date.class, new Date());
    }
}
