package com.example.cattletends.entity;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 省份每日均价实体类
 * 存储15个省份每天的省份均价，只累加不覆盖
 */
@Entity
@Table(name = "province_daily_price", 
    uniqueConstraints = {
        @UniqueConstraint(name = "uk_province_date", columnNames = {"province", "price_date"})
    },
    indexes = {
        @Index(name = "idx_province", columnList = "province"),
        @Index(name = "idx_price_date", columnList = "price_date"),
        @Index(name = "idx_province_date_composite", columnList = "province,price_date"),
        @Index(name = "idx_price", columnList = "price"),
        @Index(name = "idx_date_price", columnList = "price_date,price"),
        @Index(name = "idx_province_price", columnList = "province,price"),
        @Index(name = "idx_up_time", columnList = "up_time"),
        @Index(name = "idx_create_time", columnList = "create_time")
    }
)
public class ProvinceDailyPrice {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    /**
     * 省份名称
     */
    @Column(name = "province", nullable = false, length = 100)
    private String province;
    
    /**
     * 省份均价（元/斤）
     */
    @Column(name = "price", precision = 10, scale = 2, nullable = false)
    private BigDecimal price;
    
    /**
     * 价格日期（只记录日期，不包含时间）
     */
    @Column(name = "price_date", nullable = false)
    private LocalDate priceDate;
    
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private LocalDateTime createTime;
    
    /**
     * 更新时间
     */
    @Column(name = "up_time", nullable = false)
    private LocalDateTime upTime;
    
    @PrePersist
    protected void onCreate() {
        createTime = LocalDateTime.now();
        upTime = LocalDateTime.now();
        if (priceDate == null) {
            priceDate = LocalDate.now();
        }
    }
    
    @PreUpdate
    protected void onUpdate() {
        upTime = LocalDateTime.now();
    }
    
    // Getters and Setters
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getProvince() {
        return province;
    }
    
    public void setProvince(String province) {
        this.province = province;
    }
    
    public BigDecimal getPrice() {
        return price;
    }
    
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    
    public LocalDate getPriceDate() {
        return priceDate;
    }
    
    public void setPriceDate(LocalDate priceDate) {
        this.priceDate = priceDate;
    }
    
    public LocalDateTime getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }
    
    public LocalDateTime getUpTime() {
        return upTime;
    }
    
    public void setUpTime(LocalDateTime upTime) {
        this.upTime = upTime;
    }
}

