package com.soa.order.entity; import javax.persistence.*; import java.math.BigDecimal; import java.util.Date; import java.util.List; /** * 订单实体类 */ @Entity @Table(name = "orders") public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "user_id", nullable = false) private Long userId; @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) private List items; @Column(name = "total_amount", nullable = false, precision = 10, scale = 2) private BigDecimal totalAmount; @Column(nullable = false) private String status; // PENDING, PAID, SHIPPED, DELIVERED, CANCELLED @Column(name = "created_at") @Temporal(TemporalType.TIMESTAMP) private Date createdAt; // 构造函数 public Order() { this.createdAt = new Date(); this.status = "PENDING"; this.totalAmount = BigDecimal.ZERO; } // getter和setter方法 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public List getItems() { return items; } public void setItems(List items) { this.items = items; // 计算总金额 calculateTotalAmount(); } public BigDecimal getTotalAmount() { return totalAmount; } public void setTotalAmount(BigDecimal totalAmount) { this.totalAmount = totalAmount; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Date getCreatedAt() { return createdAt; } public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } // 计算订单总金额 public void calculateTotalAmount() { BigDecimal total = BigDecimal.ZERO; if (items != null) { for (OrderItem item : items) { if (item.getSubtotal() != null) { total = total.add(item.getSubtotal()); } } } this.totalAmount = total; } }