AsyncOrderStats.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/5/5
  6. * Time: 11:29
  7. */
  8. namespace App\Jobs;
  9. use App\Modules\Subscribe\Services\OrderService;
  10. use Illuminate\Bus\Queueable;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Redis;
  16. use DB;
  17. class AsyncOrderStats implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. private $orderId;
  21. private $order_info = null;
  22. private $uid = 0;
  23. public function __construct(int $order_id)
  24. {
  25. $this->orderId = $order_id;
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $this->init();
  35. //qkm推送统计
  36. //$this->qkmPushOrderStats();
  37. //activity 活动统计
  38. $this->activityStats();
  39. //$this->saveActivityOrder();
  40. //图书充值统计
  41. $this->bookChargeStats();
  42. }
  43. private function init()
  44. {
  45. $this->order_info = OrderService::getById($this->orderId);
  46. $this->uid = $this->order_info->uid;
  47. }
  48. private function qkmPushOrderStats()
  49. {
  50. $push_id = Redis::hget('book_read:' . $this->uid, 'qkm_push');
  51. if (!$push_id) return;
  52. $push_time = Redis::hget('qkm_push:time', $push_id);
  53. if (empty($push_time) || !is_numeric($push_time)) return;
  54. $time = ceil((time() - $push_time) / 86400);
  55. if ($time > 0 && $time <= 31) {
  56. $price = $this->order_info->price * 100;
  57. Redis::hincrby('qkm_push:amount', $push_id, $price);
  58. }
  59. }
  60. private function activityStats()
  61. {
  62. $item = $this->order_info;
  63. if ($item->status != 'PAID') return;
  64. if (!$item->activity_id) return;
  65. $day = date('Y-m-d', strtotime($item->created_at));
  66. $exist = DB::table('activity_statistic_all')
  67. ->where('activity_id', $item->activity_id)
  68. ->where('day', $day)
  69. ->select('id', 'amount')
  70. ->where('distribution_channel_id', $item->distribution_channel_id)->first();
  71. if ($exist) {
  72. DB::table('activity_statistic_all')->where('id', $exist->id)->update(
  73. [
  74. 'amount' => $exist->amount + $item->price * 100,
  75. 'updated_at' => date('Y-m-d H:i:s')
  76. ]
  77. );
  78. return;
  79. }
  80. $pv = 0;
  81. $uv = 0;
  82. DB::table('activity_statistic_all')->insert([
  83. 'activity_id' => $item->activity_id, 'day' => $day, 'distribution_channel_id' => $item->distribution_channel_id,
  84. 'amount' => $item->price * 100, 'pv' => $pv, 'uv' => $uv, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')
  85. ]);
  86. }
  87. private function saveActivityOrder()
  88. {
  89. if ($this->order_info->status != 'PAID') return;
  90. if (!$this->order_info->activity_id) return;
  91. DB::table('activity_order')->insert([
  92. 'order_id' => $this->orderId,
  93. 'activity_id' => $this->order_info->activity_id,
  94. 'distribution_channel_id' => $this->order_info->distribution_channel_id,
  95. 'created_at' => $this->order_info->created_at,
  96. 'updated_at' => date('Y-m-d H:i:s')
  97. ]);
  98. }
  99. private function bookChargeStats(){
  100. $bid = $this->order_info->from_bid;
  101. if(!$bid) return ;
  102. $day = date('Y-m-d', strtotime($this->order_info->created_at));
  103. $price = $this->order_info->price * 100;
  104. Redis::hincrby('book:charge:stats:'.$day, $bid, $price);
  105. }
  106. }