shangcheng/include/order.hpp

79 lines
1.9 KiB
C++

#ifndef MALL_ORDER_HPP
#define MALL_ORDER_HPP
#include "shopping_cart.hpp"
#include <string>
#include <vector>
#include <ctime>
/**
* @brief 订单状态枚举。
*/
enum class OrderStatus {
Pending, ///< 待支付
Paid, ///< 已支付
Shipped, ///< 已发货
Delivered, ///< 已送达
Cancelled ///< 已取消
};
/**
* @brief 订单状态转可读字符串。
* @param status 订单状态
* @return 中文状态描述
*/
std::string orderStatusToString(OrderStatus status);
/**
* @brief 订单类,由购物车生成。
*/
class Order {
public:
/**
* @brief 默认构造函数。
*/
Order() = default;
/**
* @brief 从购物车构造订单。
* @param orderId 订单编号
* @param cart 购物车(内容会被移入订单)
*/
explicit Order(uint64_t orderId, ShoppingCart& cart);
/** @brief 获取订单编号 */
uint64_t getOrderId() const noexcept { return orderId_; }
/** @brief 获取订单状态 */
OrderStatus getStatus() const noexcept { return status_; }
/**
* @brief 设置订单状态。
* @param status 新状态
*/
void setStatus(OrderStatus status) noexcept { status_ = status; }
/** @brief 获取订单创建时间(时间戳) */
std::time_t getCreateTime() const noexcept { return createTime_; }
/** @brief 获取订单条目 */
const std::vector<CartItem>& getItems() const noexcept { return items_; }
/** @brief 计算订单总金额 */
double totalPrice() const;
/**
* @brief 返回订单摘要字符串。
* @return 多行描述的订单信息
*/
std::string toString() const;
private:
uint64_t orderId_ = 0; ///< 订单编号
OrderStatus status_ = OrderStatus::Pending; ///< 订单状态
std::time_t createTime_ = 0; ///< 创建时间
std::vector<CartItem> items_; ///< 商品条目
};
#endif // MALL_ORDER_HPP