AddOrderAmount.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Jobs\Orders;
  3. use App\Consts\SysConsts;
  4. use App\Modules\Order\Services\OrderService;
  5. use App\Modules\Trade\Models\Order;
  6. use App\Modules\User\Models\User;
  7. use Exception;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use Illuminate\Support\Facades\DB;
  14. use Redis;
  15. /**
  16. * 增加订单统计金额
  17. */
  18. class AddOrderAmount implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. private $key;
  22. private $date;
  23. private $order_no;
  24. private $channel_id;
  25. private $send_order_id;
  26. private $uid;
  27. private $amount;
  28. private $pay_time;
  29. /**
  30. * Create a new job instance.
  31. * @param string $order_no 订单号
  32. * @param float $amount 支付金额
  33. * @param string $paid_time 支付成功事件
  34. * @return void
  35. */
  36. public function __construct(string $order_no)
  37. {
  38. $this->order_no = $order_no;
  39. $order = $this->findOrder($order_no);
  40. $this->channel_id = $order->distribution_channel_id;
  41. $this->send_order_id = $order->send_order_id;
  42. $this->amount = $order->price;
  43. $this->pay_time = $order->created_at;
  44. $this->date = date('Y-m-d', strtotime($order->created_at));
  45. $this->key = 'increase_order_amount:' . $this->date;
  46. }
  47. /**
  48. * Execute the job.
  49. *
  50. * @return void
  51. */
  52. public function handle()
  53. {
  54. $service = new OrderService;
  55. try {
  56. if (!Redis::exists($this->key)) {
  57. Redis::hSet($this->key, $this->order_no, 1);
  58. Redis::expire($this->key, SysConsts::ONE_DAY_SECONDS * 2);
  59. } else if (Redis::hExists($this->key, $this->order_no)) {
  60. return;
  61. } else {
  62. Redis::hSet($this->key, $this->order_no, 1);
  63. }
  64. DB::beginTransaction();
  65. $service->increaseChannelAmount($this->date, $this->channel_id, $this->amount);
  66. $service->increaseSendOrderAmount($this->date, $this->channel_id, $this->send_order_id, $this->amount, $this->pay_time);
  67. $user = User::where('id', $this->uid)->select('send_order_id')->first();
  68. if ($user) {
  69. $user_send_order = $user->send_order_id;
  70. $service->increaseUserSendOrderAmount($this->date, $this->channel_id, $user_send_order, $this->amount, $this->pay_time);
  71. }
  72. DB::commit();
  73. } catch (Exception $e) {
  74. DB::rollback();
  75. Redis::hDel($this->key, $this->order_no);
  76. myLog('increase_order_amount')->error($this->order_no);
  77. myLog('increase_order_amount')->error($e->getMessage());
  78. }
  79. }
  80. private function findOrder(string $order_no)
  81. {
  82. return Order::where('order_no', $order_no)
  83. ->select('send_order_id', 'distribution_channel_id', 'pay_end_at', 'uid', 'price')
  84. ->first();
  85. }
  86. }