<?php

declare(strict_types=1);

namespace App\Model;

use App\Model\OrderItem;

/**
 * @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 $raw 远程原始数据
 * @property string $ext 扩展字段
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @mixin \App_Model_Order
 */
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', '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();
    }
}

