package com.hlab.yanalyst.domain.social; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.LocalDateTime; @Entity @Table(name = "social_videos", uniqueConstraints = @UniqueConstraint(name = "uk_social_video_channel_external", columnNames = {"channel_id", "external_video_id"}), indexes = @Index(name = "idx_social_video_published", columnList = "published_at")) @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @EntityListeners(AuditingEntityListener.class) public class SocialVideo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne(fetch = FetchType.LAZY, optional = false) @JoinColumn(name = "channel_id", nullable = false) private SocialChannel channel; @Column(name = "external_video_id", nullable = false, length = 300) private String externalVideoId; @Column(nullable = false, length = 2000) private String url; @Column(nullable = false, columnDefinition = "TEXT") private String title; @Column(name = "thumbnail_url", length = 4000) private String thumbnailUrl; @Column(name = "published_at") private LocalDateTime publishedAt; @Column(name = "duration_sec") private Integer durationSec; @CreatedDate @Column(name = "collected_at", updatable = false) private LocalDateTime collectedAt; public static SocialVideo from(SocialChannel channel, SocialVideoCandidate candidate) { SocialVideo video = new SocialVideo(); video.channel = channel; video.externalVideoId = candidate.externalVideoId(); video.url = candidate.url(); video.title = candidate.title() == null || candidate.title().isBlank() ? "(제목 없음)" : candidate.title(); video.thumbnailUrl = candidate.thumbnailUrl(); video.publishedAt = candidate.publishedAt(); video.durationSec = candidate.durationSec(); return video; } public void fillDuration(Integer value) { if (durationSec == null && value != null && value > 0) durationSec = value; } }