package com.ecep.contract.ds.converter;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.time.MonthDay;
import java.time.format.DateTimeFormatter;

@Converter(autoApply = true)
public class MonthDayConverter implements AttributeConverter<MonthDay, String> {

    // 定义日期格式（月-日，两位数）
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MM-dd");

    @Override
    public String convertToDatabaseColumn(MonthDay attribute) {
        // 将 MonthDay 转换为数据库存储的字符串（MM-dd）
        return attribute != null ? attribute.format(FORMATTER) : null;
    }

    @Override
    public MonthDay convertToEntityAttribute(String dbData) {
        // 将数据库字符串（MM-dd）转换为 MonthDay
        return dbData != null ? MonthDay.parse(dbData, FORMATTER) : null;
    }
}