Files
pos-system/services/order-service-net/docs/vi/ARCHITECTURE.md

3.9 KiB

Kiến Trúc Order Service

Tài liệu kiến trúc cho Order Service - bộ não điều phối.

Tổng Quan Kiến Trúc

graph TB
    subgraph "Tầng API"
        C[Controllers]
        CMD[Commands]
        Q[Queries]
    end
    
    subgraph "Tầng Application"
        SF[Strategy Factory]
        RS[RetailStrategy]
        SS[ServiceStrategy]
        FS[FnbStrategy]
    end
    
    subgraph "Tầng Domain"
        O[Order Aggregate]
        OI[OrderItem]
        OS[OrderStatus]
    end
    
    subgraph "Services Ngoài"
        INV[Inventory Service]
        BOOK[Booking Service]
        FNB[F&B Engine]
    end
    
    C --> CMD
    CMD --> SF
    SF --> RS --> INV
    SF --> SS --> BOOK
    SF --> FS --> FNB
    CMD --> O
    
    style O fill:#50c878,stroke:#2d8659,color:#fff
    style SF fill:#9b59b6,stroke:#7d3c98,color:#fff

Triển Khai Strategy Pattern

classDiagram
    class ILineItemStrategy {
        <<interface>>
        +SupportedType ProductType
        +ValidateAsync(OrderItem, Guid) Task
        +ExecuteAsync(OrderItem, Guid) Task
    }
    
    class RetailStrategy {
        +SupportedType = Physical
        +ValidateAsync() Kiểm tra tồn kho
        +ExecuteAsync() Trừ tồn kho
    }
    
    class ServiceStrategy {
        +SupportedType = Service
        +ValidateAsync() Kiểm tra slot
        +ExecuteAsync() Tạo cuộc hẹn
    }
    
    class FnbStrategy {
        +SupportedType = PreparedFood
        +ValidateAsync() Luôn OK
        +ExecuteAsync() Tạo phiếu bếp
    }
    
    ILineItemStrategy <|.. RetailStrategy
    ILineItemStrategy <|.. ServiceStrategy
    ILineItemStrategy <|.. FnbStrategy

Order State Machine

stateDiagram-v2
    [*] --> Draft: Tạo Đơn
    Draft --> Validated: Tất Cả Items Hợp Lệ
    Validated --> Paid: Thanh Toán OK
    Paid --> Processing: Thực Thi Items
    Processing --> Completed: Tất Cả Items Xong
    
    Draft --> Cancelled: Hủy
    Validated --> Cancelled: Hủy
    Paid --> Cancelled: Hoàn Tiền

Database Schema

erDiagram
    orders {
        uuid id PK
        uuid shop_id FK
        uuid customer_id FK
        varchar status
        decimal total_amount
        timestamp created_at
    }
    
    order_items {
        uuid id PK
        uuid order_id FK
        uuid product_id FK
        varchar product_type
        int quantity
        decimal unit_price
        varchar status
        jsonb metadata
    }
    
    orders ||--o{ order_items : contains

Luồng Xử Lý Đơn Hàng

sequenceDiagram
    participant Handler as CreateOrderHandler
    participant Factory as StrategyFactory
    participant Strategies as ILineItemStrategy[]
    participant Payment as WalletService
    
    Handler->>Factory: GetStrategies()
    
    rect rgb(200, 230, 200)
        Note over Handler,Strategies: Giai đoạn 1: Validation
        loop Mỗi Item
            Handler->>Factory: GetStrategy(item.Type)
            Factory-->>Handler: ILineItemStrategy
            Handler->>Strategies: ValidateAsync(item)
        end
    end
    
    Handler->>Payment: ProcessPayment(order)
    Payment-->>Handler: Thành Công
    
    rect rgb(200, 200, 230)
        Note over Handler,Strategies: Giai đoạn 2: Execution
        loop Mỗi Item
            Handler->>Strategies: ExecuteAsync(item)
        end
    end

Điểm Tích Hợp

Service Protocol Mục Đích
Catalog Service HTTP Lấy chi tiết sản phẩm
Merchant Service HTTP Lấy cấu hình shop
Wallet Service HTTP Xử lý thanh toán
Inventory Service HTTP Thao tác tồn kho
Booking Service HTTP Cuộc hẹn
F&B Engine Event Bus Phiếu bếp

Tài Nguyên