<?php

declare(strict_types=1);

namespace App\Model;

use App\Model\OrderItem;
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_status_id 订单状态 ID
 * @property string $platform_order_id 平台 订单 ID
 * @property string $buyer_user_id 平台买家 id
 * @property int $payment_method_id 订单状态 ID， 默认为在线支付
 * @property boolean $presale 是否是预售， 默认为非预售
 * @property string $total_amount 订单总金额
 * @property string $total_paid 已支付金额
 * @property string $total_discount 总折扣金额
 * @property string $total_received 实收金额
 * @property string $freight_fee 运费
 * @property string $tax_fee 税费
 * @property string $discount_fee 优惠金额
 * @property string $commission_fee 佣金
 * @property string $coupon_amount 折扣券 - 优惠券金额
 * @property string $voucher_amount 代金券 - 兑换券金额
 * @property int $order_type_id 订单类型 ID, 默认为一般订单，部分平台可能有补差价，代金券订单等
 * @property \Carbon\Carbon $created_date 订单的创建时间
 * @property \Carbon\Carbon|null $updated_date 订单的更新时间
 * @property \Carbon\Carbon|null $paid_date 订单的付款时间
 * @property \Carbon\Carbon|null $shipping_date 订单的发货时间
 * @property string $zipcode 邮编
 * @property string $city 城市
 * @property string $province 省
 * @property string $country 国家
 * @property string $hash raw 字段的 MD5 哈希值
 * @property string $raw 远程原始数据
 * @property string $ext 扩展字段
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @mixin \App_Model_Order
 */
#[OA\Schema(
    schema: 'Order',
    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_status_id', type: 'integer', example: 1),
        new OA\Property(property: 'platform_order_id', type: 'string', example: 'ORD-20260101-001'),
        new OA\Property(property: 'buyer_user_id', type: 'string', nullable: true, example: 'buyer_123'),
        new OA\Property(property: 'payment_method_id', type: 'integer', example: 1),
        new OA\Property(property: 'presale', type: 'boolean', example: false),
        new OA\Property(property: 'total_amount', type: 'number', format: 'decimal', example: 199.99),
        new OA\Property(property: 'total_paid', type: 'number', format: 'decimal', example: 189.99),
        new OA\Property(property: 'total_discount', type: 'number', format: 'decimal', example: 10.00),
        new OA\Property(property: 'total_received', type: 'number', format: 'decimal', example: 189.99),
        new OA\Property(property: 'freight_fee', type: 'number', format: 'decimal', example: 0.00),
        new OA\Property(property: 'tax_fee', type: 'number', format: 'decimal', example: 0.00),
        new OA\Property(property: 'discount_fee', type: 'number', format: 'decimal', example: 10.00),
        new OA\Property(property: 'commission_fee', type: 'number', format: 'decimal', example: 5.00),
        new OA\Property(property: 'coupon_amount', type: 'number', format: 'decimal', example: 0.00),
        new OA\Property(property: 'voucher_amount', type: 'number', format: 'decimal', example: 0.00),
        new OA\Property(property: 'order_type_id', type: 'integer', example: 1),
        new OA\Property(property: 'hash', type: 'string', example: 'a1b2c3d4e5f6...'),
        new OA\Property(property: 'raw', type: 'object', nullable: true, description: '平台原始数据'),
        new OA\Property(property: 'ext', type: 'object', nullable: true, description: '扩展字段'),
        new OA\Property(property: 'created_date', type: 'string', format: 'date-time'),
        new OA\Property(property: 'updated_date', type: 'string', format: 'date-time', nullable: true),
        new OA\Property(property: 'paid_date', type: 'string', format: 'date-time', nullable: true),
        new OA\Property(property: 'shipping_date', type: 'string', format: 'date-time', nullable: true),
        new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
        new OA\Property(property: 'updated_at', type: 'string', format: 'date-time'),
    ]
)]
class Order extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'orders';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'company_id', 'platform_id', 'store_id', 'order_status_id', 'platform_order_id', 'buyer_user_id', 'payment_method_id', 'presale', 'total_amount', 'total_paid', 'total_discount', 'total_received', 'freight_fee', 'tax_fee', 'discount_fee', 'commission_fee', 'coupon_amount', 'voucher_amount', 'order_type_id', 'created_date', 'updated_date', 'paid_date', 'shipping_date', 'zipcode', 'city', 'province', 'country', 'hash', 'raw', '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_status_id' => 'integer',
        'payment_method_id' => 'integer',
        'presale' => 'boolean',
        'order_type_id' => 'integer',
        'created_date' => 'datetime',
        'updated_date' => 'datetime',
        'paid_date' => 'datetime',
        'shipping_date' => 'datetime',
        'raw' => 'json',
        'ext' => 'json',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * 获取订单子项（基于 hypertable 分区键查询）
     *
     * @return \Hyperf\Database\Model\Collection
     */
    public function getOrderItems()
    {
        return OrderItem::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)
            ->get();
    }
}

