| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace App\Module\Transfer\Models;
- use App\Module\Transfer\Casts\TransferOrderCast;
- use App\Module\Transfer\Casts\CallbackDataCast;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Enums\TransferType;
- use UCore\ModelCore;
- /**
- * 划转订单模型
- *
- * field start
- * @property int $id 主键ID
- * @property int $transfer_app_id 划转应用ID
- * @property int $out_id 外部应用ID
- * @property string $out_order_id 外部订单ID
- * @property string $out_user_id 外部用户ID
- * @property int $user_id 内部用户ID
- * @property int $currency_id 货币类型ID
- * @property int $fund_id 资金账户类型ID
- * @property \App\Module\Transfer\Enums\TransferType $type 订单类型(1=转入,2=转出)
- * @property \App\Module\Transfer\Enums\TransferStatus $status 订单状态
- * @property float $out_amount 外部金额
- * @property float $amount 内部金额
- * @property float $exchange_rate 使用汇率
- * @property \App\Module\Transfer\Casts\CallbackDataCast $callback_data 回调数据
- * @property string $error_message 错误信息
- * @property string $remark 备注信息
- * @property \Carbon\Carbon $processed_at 处理时间
- * @property \Carbon\Carbon $callback_at 回调时间
- * @property \Carbon\Carbon $completed_at 完成时间
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * @property \Carbon\Carbon $deleted_at 删除时间
- * @property float $fee_rate 使用的手续费率
- * @property float $fee_amount 手续费金额
- * @property float $actual_amount 实际到账金额(扣除手续费后)
- * field end
- */
- class TransferOrder extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'transfer_orders';
- // attrlist start
- protected $fillable = [
- 'id',
- 'transfer_app_id',
- 'out_id',
- 'out_order_id',
- 'out_user_id',
- 'user_id',
- 'currency_id',
- 'fund_id',
- 'type',
- 'status',
- 'out_amount',
- 'amount',
- 'exchange_rate',
- 'callback_data',
- 'error_message',
- 'remark',
- 'processed_at',
- 'callback_at',
- 'completed_at',
- 'fee_rate',
- 'fee_amount',
- 'actual_amount',
- ];
- // attrlist end
- /**
- * 属性类型转换
- */
- protected $casts = [
- 'id' => '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);
- }
- }
- }
|