'integer', 'cart_item_id' => 'integer', 'product_id' => 'integer', 'rule_id' => 'integer', 'rule_type' => 'integer', 'allowed_qty' => 'float', 'reserved_qty' => 'float', 'auto_add' => 'boolean', 'is_item_deleted' => 'boolean', ]; /** * 所属购物车。 */ public function cart(): BelongsTo { return $this->belongsTo(Cart::class, 'cart_id'); } /** * 对应的购物车行。 */ public function cart_item(): BelongsTo { return $this->belongsTo(CartItem::class, 'cart_item_id'); } /** * 赠送该赠品的规则。 */ public function cart_rule(): BelongsTo { return $this->belongsTo(CartRule::class, 'rule_id'); } /** * 规则是否允许「一组合计送 N 个」。 */ public function isOneOf(): bool { return (int) $this->rule_type === Gift::TYPE_ONE; } /** * 规则是否允许「每个 SKU 各送 N 个」。 */ public function isAllOf(): bool { return (int) $this->rule_type === Gift::TYPE_ALL; } /** * 用户是否手动删除过该赠品(删除后不再自动加购,直到购物车清空)。 */ public function isDeleted(): bool { return (bool) $this->is_item_deleted; } /** * 还差几个需要补进购物车。 */ public function getQtyToProcessAttribute(): float { return max(0, (float) $this->allowed_qty - (float) $this->reserved_qty); } }