<?php

declare(strict_types=1);

namespace App\Model;

use OpenApi\Attributes as OA;

/**
 * @property int $id 主键
 * @property int $company_id 公司 ID 与 Tools 保持一致
 * @property int $platform_id 平台 ID 与 Tools 保持一致
 * @property int $store_id 店铺 ID 与 Tools 保持一致
 * @property int $order_id 父订单 ID
 * @property string $platform_order_id 平台父订单 ID
 * @property string $sub_order_id 平台子订单 ID，部分平台有，如 Tmall
 * @property int $sub_order_type_id 平台子订单 类型 id，标记字段
 * @property int $product_id 商品 ID， 与 Tools 保持一致
 * @property string $platform_product_id 平台商品 ID， 与 Tools 保持一致
 * @property string $product_sku 商品 SKU 编码， 客户提供的内部商品编码
 * @property string $product_barcode 商品 条形码 GTIN 等
 * @property string $unit_price 单件价格
 * @property int $quantity 商品子项总数量
 * @property string $discount 订单子项总的折扣金额
 * @property string $total 总金额 = 单价X数量 - 折扣
 * @property \Carbon\Carbon $created_date 订单创建时间（与父订单 created_date 一致，hypertable 分区键）
 * @property array|null $ext 扩展字段
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @mixin \App_Model_OrderItem
 */
#[OA\Schema(
    schema: 'OrderItem',
    type: 'object',
    properties: [
        new OA\Property(property: 'id', type: 'integer', example: 1),
        new OA\Property(property: 'company_id', type: 'integer', example: 1),
        new OA\Property(property: 'platform_id', type: 'integer', example: 2),
        new OA\Property(property: 'store_id', type: 'integer', example: 100),
        new OA\Property(property: 'order_id', type: 'integer', example: 1000),
        new OA\Property(property: 'platform_order_id', type: 'string', example: 'ORD-20260101-001'),
        new OA\Property(property: 'sub_order_id', type: 'string', nullable: true, example: 'SUB-001'),
        new OA\Property(property: 'sub_order_type_id', type: 'integer', example: 1),
        new OA\Property(property: 'product_id', type: 'integer', example: 500),
        new OA\Property(property: 'platform_product_id', type: 'string', example: 'PROD-001'),
        new OA\Property(property: 'product_sku', type: 'string', nullable: true, example: 'SKU-ABC-001'),
        new OA\Property(property: 'product_barcode', type: 'string', nullable: true, example: '6901234567890'),
        new OA\Property(property: 'unit_price', type: 'number', format: 'decimal', example: 49.99),
        new OA\Property(property: 'quantity', type: 'integer', example: 2),
        new OA\Property(property: 'discount', type: 'number', format: 'decimal', example: 5.00),
        new OA\Property(property: 'total', type: 'number', format: 'decimal', example: 94.98),
        new OA\Property(property: 'created_date', type: 'string', format: 'date-time'),
        new OA\Property(property: 'ext', type: 'object', nullable: true, description: '扩展字段'),
        new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
        new OA\Property(property: 'updated_at', type: 'string', format: 'date-time'),
    ]
)]
class OrderItem extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'order_items';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'company_id', 'platform_id', 'store_id', 'order_id', 'platform_order_id', 'sub_order_id', 'sub_order_type_id', 'product_id', 'platform_product_id', 'product_sku', 'product_barcode', 'unit_price', 'quantity', 'discount', 'total', 'created_date', 'ext', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = [
        'id' => 'integer',
        'company_id' => 'integer',
        'platform_id' => 'integer',
        'store_id' => 'integer',
        'order_id' => 'integer',
        'sub_order_type_id' => 'integer',
        'product_id' => 'integer',
        'quantity' => 'integer',
        'created_date' => 'datetime',
        'ext' => 'json',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * 获取父订单（基于 hypertable 分区键查询）
     *
     * @return Order|null
     */
    public function getParentOrder(): ?Order
    {
        return Order::query()
            ->where('platform_id', $this->platform_id)
            ->where('store_id', $this->store_id)
            ->where('platform_order_id', $this->platform_order_id)
            ->where('created_date', $this->created_date)
            ->first();
    }
}
