package com.snp.batch.global.model; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; /** * 배치 작업 스케줄 정보를 저장하는 엔티티 * Quartz 스케줄러와 연동하여 DB에 영속화 * * JPA를 사용하므로 @PrePersist, @PreUpdate로 감사 필드 자동 설정 */ @Entity @Table(name = "job_schedule", schema = "snp_batch", indexes = { @Index(name = "idx_job_name", columnList = "job_name", unique = true), @Index(name = "idx_active", columnList = "active") }) @Data @Builder @NoArgsConstructor @AllArgsConstructor public class JobScheduleEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; /** * 배치 작업 이름 (BatchConfig에 등록된 Job Bean 이름) * 예: "jsonToPostgresJob", "shipDataImportJob" */ @Column(name = "job_name", unique = true, nullable = false, length = 100) private String jobName; /** * Cron 표현식 * 예: "0 0 2 * * ?" (매일 새벽 2시) */ @Column(name = "cron_expression", nullable = false, length = 100) private String cronExpression; /** * 스케줄 설명 */ @Column(name = "description", length = 500) private String description; /** * 활성화 여부 * true: 스케줄 활성, false: 일시 중지 */ @Column(name = "active", nullable = false) @Builder.Default private Boolean active = true; /** * 생성 일시 (감사 필드) */ @Column(name = "created_at", nullable = false, updatable = false) private LocalDateTime createdAt; /** * 수정 일시 (감사 필드) */ @Column(name = "updated_at", nullable = false) private LocalDateTime updatedAt; /** * 생성자 (감사 필드) */ @Column(name = "created_by", length = 100) private String createdBy; /** * 수정자 (감사 필드) */ @Column(name = "updated_by", length = 100) private String updatedBy; /** * 엔티티 저장 전 자동 호출 (INSERT 시) */ @PrePersist protected void onCreate() { LocalDateTime now = LocalDateTime.now(); this.createdAt = now; this.updatedAt = now; if (this.createdBy == null) { this.createdBy = "SYSTEM"; } if (this.updatedBy == null) { this.updatedBy = "SYSTEM"; } } /** * 엔티티 업데이트 전 자동 호출 (UPDATE 시) */ @PreUpdate protected void onUpdate() { this.updatedAt = LocalDateTime.now(); if (this.updatedBy == null) { this.updatedBy = "SYSTEM"; } } }