'integer', 'transfer_app_id' => 'integer', 'out_id' => 'integer', 'user_id' => 'integer', 'currency_id' => 'integer', 'fund_id' => 'integer', 'type' => TransferType::class, 'status' => TransferStatus::class, 'out_amount' => 'decimal:10', 'amount' => 'decimal:10', 'exchange_rate' => 'decimal:4', 'fee_rate' => 'decimal:4', 'fee_amount' => 'decimal:4', 'actual_amount' => 'decimal:4', 'callback_data' => CallbackDataCast::class, 'processed_at' => 'datetime', 'callback_at' => 'datetime', 'completed_at' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', ]; /** * 软删除 */ protected $dates = ['deleted_at']; /** * 隐藏字段 */ protected $hidden = []; /** * 关联划转应用 */ public function transferApp() { return $this->belongsTo(TransferApp::class, 'transfer_app_id'); } /** * 获取类型文本 */ public function getTypeTextAttribute(): string { return $this->type->getDescription(); } /** * 获取状态文本 */ public function getStatusTextAttribute(): string { return $this->status->getDescription(); } /** * 获取状态颜色 */ public function getStatusColorAttribute(): string { return $this->status->getColor(); } /** * 获取类型颜色 */ public function getTypeColorAttribute(): string { return $this->type->getColor(); } /** * 判断是否为转入订单 */ public function isTransferIn(): bool { return $this->type === TransferType::IN; } /** * 判断是否为转出订单 */ public function isTransferOut(): bool { return $this->type === TransferType::OUT; } /** * 判断是否为最终状态 */ public function isFinalStatus(): bool { return $this->status->isFinal(); } /** * 判断是否可以重试 */ public function canRetry(): bool { return $this->status->canRetry(); } /** * 更新状态 */ public function updateStatus(TransferStatus $status, string $message = null): bool { $data = ['status' => $status]; if ($message) { $data['error_message'] = $message; } // 设置时间戳 switch ($status) { case TransferStatus::PROCESSING: $data['processed_at'] = now(); break; case TransferStatus::CALLBACK: $data['callback_at'] = now(); break; case TransferStatus::COMPLETED: case TransferStatus::FAILED: $data['completed_at'] = now(); break; } return $this->update($data); } /** * 判断是否收取了手续费 */ public function hasFee(): bool { return $this->fee_amount > 0; } /** * 获取手续费率百分比文本 */ public function getFeeRatePercentAttribute(): string { return number_format($this->fee_rate * 100, 2) . '%'; } /** * 获取手续费金额格式化文本 */ public function getFeeAmountTextAttribute(): string { return number_format($this->fee_amount, 4); } /** * 获取实际到账金额格式化文本 */ public function getActualAmountTextAttribute(): string { return number_format($this->actual_amount, 4); } /** * 设置手续费信息 * * @param float $feeRate 手续费率 * @param string $feeAmount 手续费金额 * @param string $actualAmount 实际到账金额 */ public function setFeeInfo(float $feeRate, string $feeAmount, string $actualAmount): void { $this->fee_rate = $feeRate; $this->fee_amount = $feeAmount; $this->actual_amount = $actualAmount; } /** * 计算手续费信息(基于关联的应用配置) */ public function calculateFeeFromApp(): array { if (!$this->transferApp) { return [ 'fee_rate' => 0.0000, 'fee_amount' => '0.0000', 'actual_amount' => $this->amount, ]; } if ($this->isTransferIn()) { return $this->transferApp->calculateInFee($this->amount); } else { return $this->transferApp->calculateOutFee($this->amount); } } }