57 lines
2.1 KiB
C++
57 lines
2.1 KiB
C++
#include "order.hpp"
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
#include <ctime>
|
|
#include <numeric>
|
|
|
|
std::string orderStatusToString(OrderStatus status) {
|
|
switch (status) {
|
|
case OrderStatus::Pending: return "\xe5\xbe\x85\xe6\x94\xaf\xe4\xbb\x98"; // 待支付
|
|
case OrderStatus::Paid: return "\xe5\xb7\xb2\xe6\x94\xaf\xe4\xbb\x98"; // 已支付
|
|
case OrderStatus::Shipped: return "\xe5\xb7\xb2\xe5\x8f\x91\xe8\xb4\xa7"; // 已发货
|
|
case OrderStatus::Delivered: return "\xe5\xb7\xb2\xe9\x80\x81\xe8\xbe\xbe"; // 已送达
|
|
case OrderStatus::Cancelled: return "\xe5\xb7\xb2\xe5\x8f\x96\xe6\xb6\x88"; // 已取消
|
|
default: return "\xe6\x9c\xaa\xe7\x9f\xa5"; // 未知
|
|
}
|
|
}
|
|
|
|
Order::Order(uint64_t orderId, ShoppingCart& cart)
|
|
: orderId_(orderId)
|
|
, status_(OrderStatus::Pending)
|
|
, createTime_(std::time(nullptr))
|
|
{
|
|
items_ = std::move(cart.getItems());
|
|
cart.clear();
|
|
}
|
|
|
|
double Order::totalPrice() const {
|
|
return std::accumulate(items_.begin(), items_.end(), 0.0,
|
|
[](double sum, const CartItem& item) {
|
|
return sum + item.subtotal();
|
|
});
|
|
}
|
|
|
|
std::string Order::toString() const {
|
|
std::ostringstream oss;
|
|
char timeBuf[64] = {0};
|
|
std::tm* tmPtr = std::localtime(&createTime_);
|
|
if (tmPtr) {
|
|
std::strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", tmPtr);
|
|
}
|
|
|
|
oss << "\xe2\x94\x81\xe2\x94\x81\xe2\x96\xa0 \xe8\xae\xa2\xe5\x8d\x95 #" << orderId_
|
|
<< " \xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\n";
|
|
oss << " \xe7\x8a\xb6\xe6\x80\x81: " << orderStatusToString(status_) << "\n";
|
|
oss << " \xe6\x97\xb6\xe9\x97\xb4: " << timeBuf << "\n";
|
|
oss << " \xe5\x95\x86\xe5\x93\x81:\n";
|
|
|
|
for (const auto& item : items_) {
|
|
oss << " " << item.product.getName()
|
|
<< " x" << item.quantity
|
|
<< " \xEF\xBF\xA5" << std::fixed << std::setprecision(2) << item.subtotal()
|
|
<< "\n";
|
|
}
|
|
oss << " \xe5\x90\x88\xe8\xae\xa1: \xEF\xBF\xA5" << std::fixed << std::setprecision(2) << totalPrice() << "\n";
|
|
return oss.str();
|
|
}
|