TransferOrder.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 int $type 订单类型
  21. * @property int $status 订单状态
  22. * @property string $out_amount 外部金额
  23. * @property string $amount 内部金额
  24. * @property string $exchange_rate 使用汇率
  25. * @property array $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. /**
  44. * 可批量赋值的属性
  45. */
  46. protected $fillable = [
  47. 'transfer_app_id',
  48. 'out_id',
  49. 'out_order_id',
  50. 'out_user_id',
  51. 'user_id',
  52. 'currency_id',
  53. 'fund_id',
  54. 'type',
  55. 'status',
  56. 'out_amount',
  57. 'amount',
  58. 'exchange_rate',
  59. 'callback_data',
  60. 'error_message',
  61. 'remark',
  62. 'processed_at',
  63. 'callback_at',
  64. 'completed_at',
  65. ];
  66. // attrlist end
  67. /**
  68. * 属性类型转换
  69. */
  70. protected $casts = [
  71. 'id' => 'integer',
  72. 'transfer_app_id' => 'integer',
  73. 'out_id' => 'integer',
  74. 'user_id' => 'integer',
  75. 'currency_id' => 'integer',
  76. 'fund_id' => 'integer',
  77. 'type' => TransferType::class,
  78. 'status' => TransferStatus::class,
  79. 'out_amount' => 'decimal:10',
  80. 'amount' => 'decimal:10',
  81. 'exchange_rate' => 'decimal:4',
  82. 'callback_data' => CallbackDataCast::class,
  83. 'processed_at' => 'datetime',
  84. 'callback_at' => 'datetime',
  85. 'completed_at' => 'datetime',
  86. 'created_at' => 'datetime',
  87. 'updated_at' => 'datetime',
  88. 'deleted_at' => 'datetime',
  89. ];
  90. /**
  91. * 软删除
  92. */
  93. protected $dates = ['deleted_at'];
  94. /**
  95. * 隐藏字段
  96. */
  97. protected $hidden = [];
  98. /**
  99. * 关联划转应用
  100. */
  101. public function transferApp()
  102. {
  103. return $this->belongsTo(TransferApp::class, 'transfer_app_id');
  104. }
  105. /**
  106. * 获取类型文本
  107. */
  108. public function getTypeTextAttribute(): string
  109. {
  110. return $this->type->getDescription();
  111. }
  112. /**
  113. * 获取状态文本
  114. */
  115. public function getStatusTextAttribute(): string
  116. {
  117. return $this->status->getDescription();
  118. }
  119. /**
  120. * 获取状态颜色
  121. */
  122. public function getStatusColorAttribute(): string
  123. {
  124. return $this->status->getColor();
  125. }
  126. /**
  127. * 获取类型颜色
  128. */
  129. public function getTypeColorAttribute(): string
  130. {
  131. return $this->type->getColor();
  132. }
  133. /**
  134. * 判断是否为转入订单
  135. */
  136. public function isTransferIn(): bool
  137. {
  138. return $this->type === TransferType::IN;
  139. }
  140. /**
  141. * 判断是否为转出订单
  142. */
  143. public function isTransferOut(): bool
  144. {
  145. return $this->type === TransferType::OUT;
  146. }
  147. /**
  148. * 判断是否为最终状态
  149. */
  150. public function isFinalStatus(): bool
  151. {
  152. return $this->status->isFinal();
  153. }
  154. /**
  155. * 判断是否可以重试
  156. */
  157. public function canRetry(): bool
  158. {
  159. return $this->status->canRetry();
  160. }
  161. /**
  162. * 更新状态
  163. */
  164. public function updateStatus(TransferStatus $status, string $message = null): bool
  165. {
  166. $data = ['status' => $status];
  167. if ($message) {
  168. $data['error_message'] = $message;
  169. }
  170. // 设置时间戳
  171. switch ($status) {
  172. case TransferStatus::PROCESSING:
  173. $data['processed_at'] = now();
  174. break;
  175. case TransferStatus::CALLBACK:
  176. $data['callback_at'] = now();
  177. break;
  178. case TransferStatus::COMPLETED:
  179. case TransferStatus::FAILED:
  180. $data['completed_at'] = now();
  181. break;
  182. }
  183. return $this->update($data);
  184. }
  185. }