| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?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 int $type 订单类型
- * @property int $status 订单状态
- * @property string $out_amount 外部金额
- * @property string $amount 内部金额
- * @property string $exchange_rate 使用汇率
- * @property array $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 删除时间
- * field end
- */
- class TransferOrder extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'transfer_orders';
- // attrlist start
- /**
- * 可批量赋值的属性
- */
- protected $fillable = [
- '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',
- ];
- // 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',
- '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);
- }
- }
|