package com.example.cattletends.entity;

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

import java.time.LocalDateTime;

/**
 * 全国总量数据实体类
 */
@Entity
@Table(name = "cattlenational", indexes = {
    @Index(name = "idx_national_inv_23", columnList = "national_inventory_23th"),
    @Index(name = "idx_national_sla_23", columnList = "national_slaughter_23th"),
    @Index(name = "idx_national_inv_24", columnList = "national_inventory_24th"),
    @Index(name = "idx_national_sla_24", columnList = "national_slaughter_24th"),
    @Index(name = "idx_national_inv_25", columnList = "national_inventory_25th"),
    @Index(name = "idx_national_sla_25", columnList = "national_slaughter_25th")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CattleNational {

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

    /**
     * 23年全国存栏量（15个省份存栏量的总和）
     */
    @Column(name = "national_inventory_23th")
    private Integer nationalInventory23th;

    /**
     * 23年全国出栏量（15个省份出栏量的总和）
     */
    @Column(name = "national_slaughter_23th")
    private Integer nationalSlaughter23th;

    /**
     * 24年全国存栏量（15个省份存栏量的总和）
     */
    @Column(name = "national_inventory_24th")
    private Integer nationalInventory24th;

    /**
     * 24年全国出栏量（15个省份出栏量的总和）
     */
    @Column(name = "national_slaughter_24th")
    private Integer nationalSlaughter24th;

    /**
     * 25年全国存栏量（15个省份存栏量的总和）
     */
    @Column(name = "national_inventory_25th")
    private Integer nationalInventory25th;

    /**
     * 25年全国出栏量（15个省份出栏量的总和）
     */
    @Column(name = "national_slaughter_25th")
    private Integer nationalSlaughter25th;

    /**
     * 创建时间
     */
    @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();
    }
}

