<?php

declare(strict_types=1);

namespace Kylin\Retail\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kylin\Core\Models\BaseModel;

/**
 * 订单明细
 *
 * @property int    $id
 * @property int    $order_id       订单 ID
 * @property int    $product_id     商品 ID
 * @property int    $quantity       数量
 * @property int    $unit_price     单价（分）
 * @property int    $subtotal       小计（分）
 */
class OrderItem extends BaseModel
{
    protected $table = 'kylin_retail_order_items';

    protected $fillable = [
        'order_id', 'product_id', 'quantity',
        'unit_price', 'subtotal',
    ];

    protected $casts = [
        'quantity' => 'integer',
        'unit_price' => 'integer',
        'subtotal' => 'integer',
    ];

    public function order(): BelongsTo
    {
        return $this->belongsTo(Order::class, 'order_id');
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'product_id');
    }
}