'timestamp', 'rewards_given' => 'array', 'invite_rewards' => 'array', 'multiplier_applied' => 'float' ]; /** * 关联兑换码 */ public function code(): BelongsTo { return $this->belongsTo(GiftCardCode::class, 'code_id'); } /** * 关联模板 */ public function template(): BelongsTo { return $this->belongsTo(GiftCardTemplate::class, 'template_id'); } /** * 关联使用用户 */ public function user(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } /** * 关联邀请人 */ public function inviteUser(): BelongsTo { return $this->belongsTo(User::class, 'invite_user_id'); } /** * 创建使用记录 */ public static function createRecord( GiftCardCode $code, User $user, array $rewards, array $options = [] ): self { return self::create([ 'code_id' => $code->id, 'template_id' => $code->template_id, 'user_id' => $user->id, 'invite_user_id' => $user->invite_user_id, 'rewards_given' => $rewards, 'invite_rewards' => $options['invite_rewards'] ?? null, 'user_level_at_use' => $user->plan ? $user->plan->sort : null, 'plan_id_at_use' => $user->plan_id, 'multiplier_applied' => $options['multiplier'] ?? 1.0, // 'ip_address' => $options['ip_address'] ?? null, 'user_agent' => $options['user_agent'] ?? null, 'notes' => $options['notes'] ?? null, 'created_at' => time(), ]); } }