| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Module\TransferOld;
- use App\Jobs\TransferOrderInCall;
- use App\Jobs\TransferOrderInCallback;
- use App\Module\Outside\Http;
- use App\Module\TransferOld\Enums\TStatus;
- use App\Module\TransferOld\Enums\TType;
- use App\Module\TransferOld\Model\TransferOrder;
- use Carbon\Carbon;
- use Dcore\Exception\LogicException;
- use Dcore\Helper\Logger;
- use Fund\Account;
- use Fund\Fund;
- use Illuminate\Support\Facades\DB;
- class AutoCall
- {
- /**
- * 入包 的 处理
- *
- * @return void
- */
- static public function in_call()
- {
- $models = TransferOrder::query()->where([
- 'status' => TStatus::CREATED,
- 'type' => TType::In
- ])->get();
- foreach ($models as $model) {
- TransferOrderInCall::dispatch($model->id)->delay(2);
- }
- }
- static public function in_call1(TransferOrder $model)
- {
- $run = false;
- DB::beginTransaction();
- try {
- $model = TransferOrder::query()->lockForUpdate()->find($model->id);
- // dump($model->status);
- if ($model->status === TStatus::CREATED) {
- $run = self::in_call2($model);
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- dump($e);
- Logger::exception('', $e);
- }
- if ($run) {
- TransferOrderInCallback::dispatch($model->id)->delay(1);
- }
- }
- /**
- * 入包的处理
- *
- * @param TransferOrder $order
- * @return void
- */
- static private function in_call2(TransferOrder $order)
- {
- // dump($order);
- $user_id = $order->user_id;
- $out_id = $order->out_id;
- $trapp_id = $order->trapp_id;
- $app = App::get($trapp_id);
- $fundE = \Fund\Enums\Fund::from($app->fund_id);
- $fund = new Fund($app->fund_in_uid, $app->fund_id);
- $res = $fund->trade($user_id, $fundE, $order->amount, 'Transfer', $order->id, '划转 - 转入');
- if (is_string($res)) {
- throw new \Exception($res);
- }
- $order->status = TStatus::CALL;
- if (!$order->save()) {
- throw new \Exception('保存失败');
- }
- return true;
- }
- static public function in_callback()
- {
- $models = TransferOrder::query()
- ->where([
- 'status' => TStatus::CALL,
- 'type' => TType::In
- ])
- ->where('created_at', '>', Carbon::now()->subMinutes(2))
- ->limit(10)
- ->orderBy('id', 'desc')
- ->get();
- foreach ($models as $model) {
- TransferOrderInCallback::dispatch($model->id)->delay(5);
- }
- }
- static public function in_callback_all()
- {
- $models = TransferOrder::query()
- ->where([
- 'status' => TStatus::CALL,
- 'type' => TType::In
- ])
- ->orderBy('id', 'desc')
- ->get();
- foreach ($models as $model) {
- TransferOrderInCallback::dispatch($model->id)->delay(5);
- }
- }
- /**
- * 入包,回调处理
- *
- * @param $model
- * @return void
- */
- static public function in_callback1(TransferOrder $model)
- {
- if ($model->type === TType::In) {
- // dump($model->status);
- if ($model->status === TStatus::CALL) {
- self::in_callback2($model);
- }
- }
- }
- /**
- * 回调处理
- *
- * @param TransferOrder $order
- * @return true
- */
- static private function in_callback2(TransferOrder $order)
- {
- // dump($order);
- // $user_id = $order->user_id;
- $out_id = $order->out_id;
- $trapp_id = $order->trapp_id;
- $app = App::get($trapp_id);
- $http = Http::getById($app->out_id);
- $res = $http->postJson($app->order_callback, [
- 'business_id' => $order->out_order_id,
- 'wallet_id' => $order->id,
- 'status' => $order->status->value(),
- ]);
- if ($res['code'] != 0) {
- throw new \Exception('回调失败');
- }
- // dd($res);
- $order->status = TStatus::CALLBACK;
- if (!$order->save()) {
- throw new \Exception('保存失败');
- }
- return true;
- }
- }
|