TransferOrder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Module\Transfer\Models;
  3. use App\Module\Transfer\Casts\TransferOrderCast;
  4. use App\Module\Transfer\Casts\CallbackDataCast;
  5. use App\Module\Transfer\Enums\TransferStatus;
  6. use App\Module\Transfer\Enums\TransferType;
  7. use UCore\ModelCore;
  8. /**
  9. * 划转订单模型
  10. *
  11. * field start
  12. * @property int $id 主键ID
  13. * @property int $transfer_app_id 划转应用ID
  14. * @property int $out_id 开放接口ID
  15. * @property string $out_order_id 外部订单ID
  16. * @property string $out_user_id 外部用户ID
  17. * @property int $user_id 内部用户ID
  18. * @property int $currency_id 货币类型ID
  19. * @property int $fund_id 资金账户类型ID
  20. * @property \App\Module\Transfer\Enums\TransferType $type 订单类型(1=转入,2=转出)
  21. * @property \App\Module\Transfer\Enums\TransferStatus $status 订单状态
  22. * @property float $out_amount 外部金额
  23. * @property float $amount 内部金额
  24. * @property float $exchange_rate 使用汇率
  25. * @property \App\Module\Transfer\Casts\CallbackDataCast $callback_data 回调数据
  26. * @property string $error_message 错误信息
  27. * @property string $remark 备注信息
  28. * @property \Carbon\Carbon $processed_at 处理时间
  29. * @property \Carbon\Carbon $callback_at 回调时间
  30. * @property \Carbon\Carbon $completed_at 完成时间
  31. * @property \Carbon\Carbon $created_at 创建时间
  32. * @property \Carbon\Carbon $updated_at 更新时间
  33. * @property \Carbon\Carbon $deleted_at 删除时间
  34. * field end
  35. */
  36. class TransferOrder extends ModelCore
  37. {
  38. /**
  39. * 数据表名
  40. */
  41. protected $table = 'transfer_orders';
  42. // attrlist start
  43. protected $fillable = [
  44. 'id',
  45. 'transfer_app_id',
  46. 'out_id',
  47. 'out_order_id',
  48. 'out_user_id',
  49. 'user_id',
  50. 'currency_id',
  51. 'fund_id',
  52. 'type',
  53. 'status',
  54. 'out_amount',
  55. 'amount',
  56. 'exchange_rate',
  57. 'callback_data',
  58. 'error_message',
  59. 'remark',
  60. 'processed_at',
  61. 'callback_at',
  62. 'completed_at',
  63. ];
  64. // attrlist end
  65. /**
  66. * 属性类型转换
  67. */
  68. protected $casts = [
  69. 'id' => 'integer',
  70. 'transfer_app_id' => 'integer',
  71. 'out_id' => 'integer',
  72. 'user_id' => 'integer',
  73. 'currency_id' => 'integer',
  74. 'fund_id' => 'integer',
  75. 'type' => TransferType::class,
  76. 'status' => TransferStatus::class,
  77. 'out_amount' => 'decimal:10',
  78. 'amount' => 'decimal:10',
  79. 'exchange_rate' => 'decimal:4',
  80. 'callback_data' => CallbackDataCast::class,
  81. 'processed_at' => 'datetime',
  82. 'callback_at' => 'datetime',
  83. 'completed_at' => 'datetime',
  84. 'created_at' => 'datetime',
  85. 'updated_at' => 'datetime',
  86. 'deleted_at' => 'datetime',
  87. ];
  88. /**
  89. * 软删除
  90. */
  91. protected $dates = ['deleted_at'];
  92. /**
  93. * 隐藏字段
  94. */
  95. protected $hidden = [];
  96. /**
  97. * 关联划转应用
  98. */
  99. public function transferApp()
  100. {
  101. return $this->belongsTo(TransferApp::class, 'transfer_app_id');
  102. }
  103. /**
  104. * 获取类型文本
  105. */
  106. public function getTypeTextAttribute(): string
  107. {
  108. return $this->type->getDescription();
  109. }
  110. /**
  111. * 获取状态文本
  112. */
  113. public function getStatusTextAttribute(): string
  114. {
  115. return $this->status->getDescription();
  116. }
  117. /**
  118. * 获取状态颜色
  119. */
  120. public function getStatusColorAttribute(): string
  121. {
  122. return $this->status->getColor();
  123. }
  124. /**
  125. * 获取类型颜色
  126. */
  127. public function getTypeColorAttribute(): string
  128. {
  129. return $this->type->getColor();
  130. }
  131. /**
  132. * 判断是否为转入订单
  133. */
  134. public function isTransferIn(): bool
  135. {
  136. return $this->type === TransferType::IN;
  137. }
  138. /**
  139. * 判断是否为转出订单
  140. */
  141. public function isTransferOut(): bool
  142. {
  143. return $this->type === TransferType::OUT;
  144. }
  145. /**
  146. * 判断是否为最终状态
  147. */
  148. public function isFinalStatus(): bool
  149. {
  150. return $this->status->isFinal();
  151. }
  152. /**
  153. * 判断是否可以重试
  154. */
  155. public function canRetry(): bool
  156. {
  157. return $this->status->canRetry();
  158. }
  159. /**
  160. * 更新状态
  161. */
  162. public function updateStatus(TransferStatus $status, string $message = null): bool
  163. {
  164. $data = ['status' => $status];
  165. if ($message) {
  166. $data['error_message'] = $message;
  167. }
  168. // 设置时间戳
  169. switch ($status) {
  170. case TransferStatus::PROCESSING:
  171. $data['processed_at'] = now();
  172. break;
  173. case TransferStatus::CALLBACK:
  174. $data['callback_at'] = now();
  175. break;
  176. case TransferStatus::COMPLETED:
  177. case TransferStatus::FAILED:
  178. $data['completed_at'] = now();
  179. break;
  180. }
  181. return $this->update($data);
  182. }
  183. }