package com.its.api.its.model.entity.database;

import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class AuditingEntity {

    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime lastModifiedDate;

    @CreatedBy
    @Column(updatable = false)
    private String createdBy;

    @LastModifiedBy
    private String lastModifiedBy;

    @PrePersist
    public void onPrePersist() {
        LocalDateTime now = LocalDateTime.now();
        String createDt = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }

    @PreUpdate
    public void onPreUpdate() {
        LocalDateTime now = LocalDateTime.now();
        String updateDt = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }
}
