package com.example.cattletends.entity;

import javax.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 省份数据实体类
 */
@Entity
@Table(name = "cattleprovince", indexes = {
    @Index(name = "idx_province_name", columnList = "province"),
    @Index(name = "idx_province_price", columnList = "province_price"),
    @Index(name = "idx_inv_23", columnList = "inventory_23th"),
    @Index(name = "idx_sla_23", columnList = "slaughter_23th"),
    @Index(name = "idx_inv_24", columnList = "inventory_24th"),
    @Index(name = "idx_sla_24", columnList = "slaughter_24th"),
    @Index(name = "idx_inv_25", columnList = "inventory_25th"),
    @Index(name = "idx_sla_25", columnList = "slaughter_25th")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CattleProvince {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    private Integer id;

    /**
     * 省份名称
     */
    @Column(name = "province", length = 255, nullable = false, unique = true)
    private String province;

    /**
     * 省份均价（存储最低价格）
     */
    @Column(name = "province_price", precision = 10, scale = 2)
    private BigDecimal provincePrice;
    
    /**
     * 省份均价区间（原始字符串，如 "13.4-14.9"）
     */
    @Column(name = "province_price_range", length = 50)
    private String provincePriceRange;

    /**
     * 23年存栏（万头）
     */
    @Column(name = "inventory_23th")
    private Integer inventory23th;

    /**
     * 23年出栏（万头）
     */
    @Column(name = "slaughter_23th")
    private Integer slaughter23th;

    /**
     * 24年存栏（万头）
     */
    @Column(name = "inventory_24th")
    private Integer inventory24th;

    /**
     * 24年出栏（万头）
     */
    @Column(name = "slaughter_24th")
    private Integer slaughter24th;

    /**
     * 25年存栏（万头）
     */
    @Column(name = "inventory_25th")
    private Integer inventory25th;

    /**
     * 25年出栏（万头）
     */
    @Column(name = "slaughter_25th")
    private Integer slaughter25th;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Column(name = "up_time", nullable = false)
    private LocalDateTime upTime;

    /**
     * 保存前自动设置创建时间和更新时间
     */
    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        if (createTime == null) {
            createTime = now;
        }
        if (upTime == null) {
            upTime = now;
        }
    }

    /**
     * 更新前自动设置更新时间
     */
    @PreUpdate
    protected void onUpdate() {
        upTime = LocalDateTime.now();
    }
}

