AutoCall.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Module\Transfer;
  3. use App\Jobs\TransferOrderInCall;
  4. use App\Jobs\TransferOrderInCallback;
  5. use App\Module\Outside\Http;
  6. use App\Module\Transfer\Enums\TStatus;
  7. use App\Module\Transfer\Enums\TType;
  8. use App\Module\Transfer\Model\TransferOrder;
  9. use Carbon\Carbon;
  10. use Dcore\Exception\LogicException;
  11. use Dcore\Helper\Logger;
  12. use Fund\Account;
  13. use Fund\Fund;
  14. use Illuminate\Support\Facades\DB;
  15. class AutoCall
  16. {
  17. /**
  18. * 入包 的 处理
  19. *
  20. * @return void
  21. */
  22. static public function in_call()
  23. {
  24. $models = TransferOrder::query()->where([
  25. 'status' => TStatus::CREATED,
  26. 'type' => TType::In
  27. ])->get();
  28. foreach ($models as $model) {
  29. TransferOrderInCall::dispatch($model->id)->delay(2);
  30. }
  31. }
  32. static public function in_call1(TransferOrder $model)
  33. {
  34. $run = false;
  35. DB::beginTransaction();
  36. try {
  37. $model = TransferOrder::query()->lockForUpdate()->find($model->id);
  38. // dump($model->status);
  39. if ($model->status === TStatus::CREATED) {
  40. $run = self::in_call2($model);
  41. }
  42. DB::commit();
  43. } catch (\Exception $e) {
  44. DB::rollBack();
  45. dump($e);
  46. Logger::exception('', $e);
  47. }
  48. if ($run) {
  49. TransferOrderInCallback::dispatch($model->id)->delay(1);
  50. }
  51. }
  52. /**
  53. * 入包的处理
  54. *
  55. * @param TransferOrder $order
  56. * @return void
  57. */
  58. static private function in_call2(TransferOrder $order)
  59. {
  60. // dump($order);
  61. $user_id = $order->user_id;
  62. $out_id = $order->out_id;
  63. $trapp_id = $order->trapp_id;
  64. $app = App::get($trapp_id);
  65. $fundE = \Fund\Enums\Fund::from($app->fund_id);
  66. $fund = new Fund($app->fund_in_uid, $app->fund_id);
  67. $res = $fund->trade($user_id, $fundE, $order->amount, 'Transfer', $order->id, '划转 - 转入');
  68. if (is_string($res)) {
  69. throw new \Exception($res);
  70. }
  71. $order->status = TStatus::CALL;
  72. if (!$order->save()) {
  73. throw new \Exception('保存失败');
  74. }
  75. return true;
  76. }
  77. static public function in_callback()
  78. {
  79. $models = TransferOrder::query()
  80. ->where([
  81. 'status' => TStatus::CALL,
  82. 'type' => TType::In
  83. ])
  84. ->where('created_at', '>', Carbon::now()->subMinutes(2))
  85. ->limit(10)
  86. ->orderBy('id', 'desc')
  87. ->get();
  88. foreach ($models as $model) {
  89. TransferOrderInCallback::dispatch($model->id)->delay(5);
  90. }
  91. }
  92. static public function in_callback_all()
  93. {
  94. $models = TransferOrder::query()
  95. ->where([
  96. 'status' => TStatus::CALL,
  97. 'type' => TType::In
  98. ])
  99. ->orderBy('id', 'desc')
  100. ->get();
  101. foreach ($models as $model) {
  102. TransferOrderInCallback::dispatch($model->id)->delay(5);
  103. }
  104. }
  105. /**
  106. * 入包,回调处理
  107. *
  108. * @param $model
  109. * @return void
  110. */
  111. static public function in_callback1(TransferOrder $model)
  112. {
  113. if ($model->type === TType::In) {
  114. // dump($model->status);
  115. if ($model->status === TStatus::CALL) {
  116. self::in_callback2($model);
  117. }
  118. }
  119. }
  120. /**
  121. * 回调处理
  122. *
  123. * @param TransferOrder $order
  124. * @return true
  125. */
  126. static private function in_callback2(TransferOrder $order)
  127. {
  128. // dump($order);
  129. // $user_id = $order->user_id;
  130. $out_id = $order->out_id;
  131. $trapp_id = $order->trapp_id;
  132. $app = App::get($trapp_id);
  133. $http = Http::getById($app->out_id);
  134. $res = $http->postJson($app->order_callback, [
  135. 'business_id' => $order->out_order_id,
  136. 'wallet_id' => $order->id,
  137. 'status' => $order->status->value(),
  138. ]);
  139. if ($res['code'] != 0) {
  140. throw new \Exception('回调失败');
  141. }
  142. // dd($res);
  143. $order->status = TStatus::CALLBACK;
  144. if (!$order->save()) {
  145. throw new \Exception('保存失败');
  146. }
  147. return true;
  148. }
  149. }