如何设计处理Order,OrderItem,Return,Refund,Exchange的数据库?

时间:2013-09-04 02:56:22

标签: database database-design

互联网上的大多数问题都集中在Order,OrderItem上。关于设计一个全面处理在线零售(Order,OrderItem,Return,Refund,Exchange)各个方面的数据库的问题很少。

我基本上只知道这个数据模型。

Product (ProductID, Name, etc)
Order (OrderID, Date, totalcost, etc)
OrderItem (OrderID, ProductID, Quantity, UnitPrice, etc)

根据以上结构,我该如何管理退货,退款,换货?

我注意到当我在超市上退货/换货时,那里的工作人员会重新生成新的发票。这是他们处理退货,退款,换货的方式吗?

4 个答案:

答案 0 :(得分:3)

  • (F table.column)表示指向table.column
  • 的外键
  • (P)表示主键
  • (U)表示唯一键

以下是一些表格和示例数据......

addresses
    id          unsigned int(P)
    line1       varchar(50)
    line2       varchar(50) // Allow NULL
    city_id     unsigned int(F cities.id)
    zip         varchar(6) // 5 digits for US and MX, 6 characters (X9X9X9) for CA
    zip4        char(4) // Allow NULL

+----+-----------------+-------+---------+--------+------+
| id | line1           | line2 | city_id | zip    | zip4 |
+----+-----------------+-------+---------+--------+------+
|  1 | 123 Main Street | Apt A |      17 | 92101  | 1234 |
|  2 | 345 East Street | NULL  |      25 | T1X0L3 | NULL |
| .. | ............... | ..... | ....... | ...... | .... |
+----+-----------------+-------+---------+--------+------+

cities
    id                  unsigned int(P)
    state_id            unsigned int(F states.id)
    name                varchar(50)
    ...

+----+----------+-----------+-----+
| id | state_id | name      | ... |
+----+----------+-----------+-----+
| .. | ........ | ......... | ... |
| 17 |      130 | San Diego | ... |
| .. | ........ | ......... | ... |
| 25 |       14 | Calgary   | ... |
| .. | ........ | ......... | ... |
+----+----------+-----------+-----+

请参阅ISO 3166-1

countries
    id              char(2)(P)
    iso3            char(3)(U)
    iso_num         char(3)(U)
    name            varchar(45)(U)

+----+------+---------+---------------+
| id | iso3 | iso_num | name          |
+----+------+---------+---------------+
| .. | .... | ....... | ............. |
| CA | CAN  | 124     | Canada        |
| .. | .... | ....... | ............. |
| MX | MEX  | 484     | Mexico        |
| .. | .... | ....... | ............. |
| US | USA  | 840     | United States |
| .. | .... | ....... | ............. |
+----+------+---------+---------------+

有关散列密码的信息,请参阅PHP's crypt() function

customers
    id              unsigned int(P)
    first_name      varchar(50)
    middle_name     varchar(50) // Allow NULL
    last_name       varchar(50)
    email           varchar(255)
    username        varchar(32)
    password        varbinary(255) // hashed
    ...

+----+------------+-------------+-----------+----------------------------+-----------+----------+-----+
| id | first_name | middle_name | last_name | email                      | username  | password | ... |
+----+------------+-------------+-----------+----------------------------+-----------+----------+-----+
|  1 | John       | Quincy      | Public    | jqp@privacy.com            | johnqball | xxxxxxxx | ... |
|  2 | Jane       | NULL        | Doe       | ladyinred@chrisdeburgh.com | janeykins | xxxxxxxx | ... |
| .. | .......... | ........... | ......... | .......................... | ......... | .......  | ... |
+----+------------+-------------+-----------+----------------------------+-----------+----------+-----+

customers_addresses
    id              unsigned int(P)
    customer_id     unsigned int(F customers.id)
    address_id      unsigned int(F addresses.id)

orders
    id                  unsigned int(P)
    customer_id         unsigned int(F customers.id)
    bill_address_id     unsigned int(F addresses.id)
    ship_address_id     unsigned int(F addresses.id)
    created             datetime
    shipped             datetime
    ...

+----+-------------+-----------------+-----------------+---------------------+---------------------+-----+
| id | customer_id | bill_address_id | ship_address_id | created             | shipped             | ... |
+----+-------------+-----------------+-----------------+---------------------+---------------------+-----+
|  1 | 1           | 1               | 1               | 2012-12-31 23:59:59 | 2013-01-01 00:00:00 | ... |
+----+-------------+-----------------+-----------------+---------------------+---------------------+-----+

orders_products
    id              unsigned int(P)
    order_id        unsigned int(F orders.id)
    product_id      unsigned int(F products.id)
    quantity        unsigned int
    unit_price      double
    ...

+----+----------+------------+----------+------------+-----+
| id | order_id | product_id | quantity | unit_price | ... |
+----+----------+------------+----------+------------+-----+
|  1 | 1        | 1          | 1        | 12.34      | ... |
|  2 | 1        | 2          | 13       | 1.78       | ... |
| .. | ........ | .......... | ........ | .......... | ... |
+----+----------+------------+----------+------------+-----+

products
    id                  unsigned int(P)
    name                varchar(50)
    price               double
    ...

+----+----------+-------+-----+
| id | name     | price | ... |
+----+----------+-------+-----+
|  1 | Widget 1 | 12.34 | ... |
|  2 | Widget 2 | 1.78  | ... |
| .. | ........ | ..... | ... |
+----+----------+-------+-----+

returns
    id                      unsigned int(P)
    order_product_id        unsigned int(F orders_products.id)
    quantity                unsigned int
    ...

+----+------------------+----------+-----+
| id | order_product_id | quantity | ... |
+----+------------------+----------+-----+
|  1 | 1                | 1        | ... |
| .. | ................ | ........ | ... |
+----+------------------+----------+-----+

请参阅ISO 3166-2

states
    id              unsigned int(P)
    country_id      char(2)(F countries.id)
    code            char(2) // AB, AL, NL, etc.
    name            varchar(50) // Alberta, Alabama, Nuevo Leon, etc.
    ...

+-----+------------+------+------------+-----+
| id  | country_id | code | name       | ... |
+-----+------------+------+------------+-----+
| ... | .......... | .... | .......... | ... |
|  14 | CA         | AB   | Alberta    | ... |
| ... | .......... | .... | .......... | ... |
|  72 | MX         | CH   | Chiapas    | ... |
| ... | .......... | .... | .......... | ... |
| 130 | US         | CA   | California | ... |
| ... | .......... | .... | .......... | ... |
+-----+------------+------+------------+-----+

退货,退款和换货都是真正的回报 - 客户正在退货。你如何处理它取决于你的业务规则......

答案 1 :(得分:1)

你可以建立一个处理退货和交换的表,比如

Returns (ID, OrderID, ExchangeID)

因此,如果客户返回了某些内容,则将OrderID放入“返回”并完成 - 如果他们交换了您处理新订单的内容,则将返回的商品的OrderID放入Returns.OrderID字段和新的OrderID中在Returns.ExchangeID字段中,这样您就知道哪个产品被交换了什么。这应该足够灵活,以允许无限的回报和交换。

显然还有更多的东西 - 只是想到让我的球开始滚动......

答案 2 :(得分:0)

存在混淆事物的风险,您需要另一个概念,即订单商品的生命周期。现在,您隐含了订单商品的“付费”生命周期。每个项目实际上都有与之相关的其他生命周期,例如“有序”(例如在网上商店中),“有库存”,“已打包”,“已发送”,“已交付”等。

销售后,您有其他生命周期状态。

最简单的方法是让查找表包含每个生命周期状态的条目,让我们称之为LifecycleStatus,并从OrderItem表创建一个外键列。但是,这可能不会为您提供足够的信息,并且会丢失任何历史记录。

下一步是将LifecycleInfo表添加到OrderItem表中。它还有一列到LifecycleStatus表的外键。这个表(它发生的关系表,多对多)也应该有其他列,通常这至少是一个日期和一个字符串来保存描述性内容。

这两个实体允许单个OrderItem行具有多个状态,并使用一些有用信息跟踪该状态。

处理这些条目的逻辑通常非常简单,例如防止同一行多次退款。

希望有所帮助。

答案 3 :(得分:0)

我认为我得到了这个简单的解决方案但不确定它有任何问题。

你甚至不需要创建任何表,而是做这两件简单的事情:

1,只需在OrderItem表中添加1个字段(orderItemStatus)即可。 OrderItemStatus(1是正常的,2是returnRefund,3是returnExchange)

2,将relatedOrderID添加到Order表中。

Product (ProductID, Name, etc)
Order (OrderID, RelatedOrderID, Date, totalcost, etc)
OrderItem (OrderID, ProductID, OrderItemStatus, Quantity, UnitPrice, etc)

当用户购买商品时,然后生成订单&为RelatedOrderID字段留空,也为OrderItemStatus设置为1。

当用户想要退货退款时,在OrderItem中添加1行,其中OrderID是现有的&设置OrderItemStatus = 2

当用户想要返回交换时,然后在OrderItem中添加1行,其中OrderID(1)是现有的&设置OrderItemStatus = 3。但是这次我们将创建一个具有RelatedOrderID =(1)的新订单。此新订单的OrderItem也具有相同的数量和数量。 ProductID作为(1)中的OrderItem,我们为这个新订单设置OrderItemStatus = 1.

我不确定这是否是最简单但满足所有需求解决方案