商店的简单数据库方案

Simple database scheme for shop

我在数据库架构方面遇到了一些问题。我正在自己做一个简单的网上商店。我不知道如何制作逻辑数据库。 我有 User, Cart, CartItem, Product,和 订单 tables.

我的关系

用户OneToOne购物车
购物车 OneToMany CartItem
CartItem ManyToOne Product

而且我不知道我应该为 table 订单选择哪个关系。 用户可以将任意数量的产品添加到他的购物车中,在确认购买后我想将结果存储到订单 table 中。 请帮我弄清楚。我是第一次做网上商店,这对我来说很有挑战性。

谨致问候。 购物车

Entity
public class Cart {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CartItem> cartItems;
@OneToOne
private User user;
private Double grandTotal;

public Cart(List<CartItem> cartItems) {
    this.cartItems = cartItems;
}

public Cart(Double grandTotal) {
    this.grandTotal = grandTotal;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public List<CartItem> getCartItemsl() {
    return cartItems;
}

public void setCartItemsl(List<CartItem> cartItemsl) {
    this.cartItems = cartItemsl;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public Double getGrandTotal() {
    return grandTotal;
}

public void setGrandTotal(Double grandTotal) {
    this.grandTotal = grandTotal;
}
}

购物车商品

@Entity
public class CartItem {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JsonIgnore
private Cart cart;
@ManyToOne
private Product product;
private Integer quantity;
private Double totalPrice;

public CartItem() {
}

public CartItem(Integer quantity, Double totalPrice) {
    this.quantity = quantity;
    this.totalPrice = totalPrice;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Cart getCart() {
    return cart;
}

public void setCart(Cart cart) {
    this.cart = cart;
}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public Integer getQuantity() {
    return quantity;
}

public void setQuantity(Integer quantity) {
    this.quantity = quantity;
}

public Double getTotalPrice() {
    return totalPrice;
}

public void setTotalPrice(Double totalPrice) {
    this.totalPrice = totalPrice;
}

}

产品

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String description;
private Float price;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Category category;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<CartItem> cartItemList;
private boolean available;

public Product() {
}

public Product(String name, String description, Float price, Category category, boolean available) {
    this.name = name;
    this.description = description;
    this.price = price;
    this.category = category;
    this.available = available;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Float getPrice() {
    return price;
}

public void setPrice(Float price) {
    this.price = price;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public List<CartItem> getCartItemList() {
    return cartItemList;
}

public void setCartItemList(List<CartItem> cartItemList) {
    this.cartItemList = cartItemList;
}

public boolean isAvailable() {
    return available;
}

public void setAvailable(boolean available) {
    this.available = available;
}

}

当用户完成购买时,您将其写入订单 table,并将用户的购物车写入订单商品。订单应在订单记录中包含账单地址和送货地址。

您还可以选择将用户账单信息写入客户 table 和送货 table。 (一个客户可以有多个送货地址)然后不在订单记录中包含该信息并且 link 到这些记录可能很诱人 - 不要这样做。可能会出现许多复杂情况,但简而言之,特定订单的信息必须保留在订单中。

这样,为了订单的历史目的,您需要保留的所有 tables - 与用于购物的 tables 完全分开。

所以听起来您的 'cart' 确实是 'order' 总数 运行 的开始。这很酷,但你可能想给它起别的名字。

重要的是购物车项目应该定期检查产品 table 以确认价格,并确认产品仍有库存。这些变化中的任何一个都会给商店带来巨大的问题,特别是如果客户订购的商品已经脱销。商店可能会在联系客户所需的客户服务时间内失去所有利润,前提是他们可以说服客户购买不同的商品。显然,如果产品价格上涨,那么商店就会在销售中损失这笔钱。

这意味着购物车(购物车商品)的唯一责任是保存产品 sku(商品编号)和数量。购物车可以保留价格用于展示,但不对价格负责。在提供运输信息后和最终点击生成交易之前,购物车至少应检查产品 table 的价格和库存水平。

你有用户的想法很好,这会让事情变得容易得多。因此,要考虑的一件事是让用户持有不同的 运行 总计 - 而不是看起来像您正在做的 'cart' 。因为有些事情会影响处理中的订单 运行 总计 - 如运费、销售、税收等 - 与 'cart' 无关。

另一个想法是例如将处理中的订单运输信息保存给用户。换句话说,他们已经提交了运输信息,但交易还没有完成。这样一来,即使他们从未完成购买(这种情况经常发生),您也不会浪费任何资源来发送 table。如果您只为已完成的订单写信给发货 table,那么每条记录都是有效的。计费的想法相同。