openapi: 3.0.0
info:
  title: 库存预订系统 API
  description: 提供库存管理和预订服务的 API 接口
  version: 1.0.0

servers:
  - url: /api/v1
    description: API 基础路径

components:
  schemas:
    Error:
      type: object
      properties:
        code:
          type: integer
          description: 错误码
        message:
          type: string
          description: 错误信息

    Inventory:
      type: object
      properties:
        id:
          type: integer
          format: uint64
          description: 库存ID
        manager_id:
          type: integer
          format: int64
          description: 库存管理方ID
        manager_name:
          type: string
          description: 库存管理方名称
        vendor_id:
          type: integer
          format: int64
          description: 供应商ID
        product_id:
          type: integer
          format: int64
          description: 商品ID
        total_quantity:
          type: integer
          description: 总库存数量

    Booking:
      type: object
      properties:
        id:
          type: integer
          format: uint64
          description: 预订ID
        source:
          type: string
          description: 预订来源
        source_id:
          type: string
          description: 来源ID
        booking_type:
          type: string
          enum: [time]
          description: 预订类型
        inventory_id:
          type: integer
          format: uint64
          description: 库存ID
        quantity:
          type: integer
          description: 预订数量
        status:
          type: string
          enum: [pending, confirmed, returned, cancelled]
          description: 预订状态
        remarks:
          type: string
          description: 备注
        booking_time:
          type: string
          format: date-time
          description: 预订时间
        delivery_time:
          type: string
          format: date-time
          description: 交付时间
        return_time:
          type: string
          format: date-time
          description: 归还时间
        cancel_time:
          type: string
          format: date-time
          description: 取消时间

  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

security:
  - bearerAuth: []

paths:
  /inventories:
    post:
      summary: 创建库存
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - manager_id
                - manager_name
                - vendor_id
                - product_id
                - total_quantity
              properties:
                manager_id:
                  type: integer
                  format: int64
                vendor_id:
                  type: integer
                  format: int64
                product_id:
                  type: integer
                  format: int64
                total_quantity:
                  type: integer
                  minimum: 0
      responses:
        '200':
          description: 成功创建库存
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Inventory'

    get:
      summary: 获取库存列表
      parameters:
        - name: page_num
          in: query
          required: true
          schema:
            type: integer
            minimum: 1
        - name: page_size
          in: query
          required: true
          schema:
            type: integer
            minimum: 1
            maximum: 100
        - name: manager_id
          in: query
          schema:
            type: integer
            format: int64
        - name: vendor_id
          in: query
          schema:
            type: integer
            format: int64
        - name: product_ids
          in: query
          schema:
            type: string
      responses:
        '200':
          description: 成功获取库存列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  total:
                    type: integer
                  items:
                    type: array
                    items:
                      $ref: '#/components/schemas/Inventory'

  /inventories/{id}:
    get:
      summary: 获取库存详情
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: uint64
      responses:
        '200':
          description: 成功获取库存详情
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Inventory'
    
    put:
      summary: 更新库存信息
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: uint64
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - total_quantity
                - manager_name
                - vendor_id
              properties:
                total_quantity:
                  type: integer
                  minimum: 0
                manager_name:
                  type: string
                vendor_id:
                  type: integer
                  format: int64
      responses:
        '200':
          description: 成功更新库存
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Inventory'

  /bookings:
    post:
      summary: 创建预订
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - source
                - source_id
                - booking_type
                - inventory_id
                - quantity
                - start_date
                - end_date
              properties:
                source:
                  type: string
                source_id:
                  type: string
                booking_type:
                  type: string
                  enum: [time]
                inventory_id:
                  type: integer
                  format: uint64
                quantity:
                  type: integer
                  minimum: 1
                start_date:
                  type: string
                  format: date-time
                end_date:
                  type: string
                  format: date-time
                remarks:
                  type: string
      responses:
        '200':
          description: 成功创建预订
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Booking'

    get:
      summary: 获取预订列表
      parameters:
        - name: page
          in: query
          required: true
          schema:
            type: integer
            minimum: 1
        - name: page_size
          in: query
          required: true
          schema:
            type: integer
            minimum: 1
            maximum: 100
        - name: status
          in: query
          schema:
            type: string
            enum: [pending, confirmed, returned, cancelled]
      responses:
        '200':
          description: 成功获取预订列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  total:
                    type: integer
                  items:
                    type: array
                    items:
                      $ref: '#/components/schemas/Booking'

  /bookings/{id}:
    get:
      summary: 获取预订详情
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: uint64
      responses:
        '200':
          description: 成功获取预订详情
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Booking'

    delete:
      summary: 取消预订
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
            format: uint64
      responses:
        '200':
          description: 成功取消预订

  /bookings/check:
    post:
      summary: 检查预订可用性
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - items
              properties:
                items:
                  type: array
                  items:
                    type: object
                    required:
                      - inventory_id
                      - start_date
                      - end_date
                      - quantity
                    properties:
                      inventory_id:
                        type: integer
                        format: uint64
                      start_date:
                        type: string
                        format: date-time
                      end_date:
                        type: string
                        format: date-time
                      quantity:
                        type: integer
                        minimum: 1
      responses:
        '200':
          description: 成功检查预订可用性
          content:
            application/json:
              schema:
                type: object
                properties:
                  results:
                    type: array
                    items:
                      type: object
                      properties:
                        inventory_id:
                          type: integer
                          format: uint64
                        available:
                          type: boolean
                        request_qty:
                          type: integer
                        stock_qty:
                          type: integer