AsyncOrderStats.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 GuzzleHttp\Client;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Foundation\Bus\Dispatchable;
  16. use Redis;
  17. use DB;
  18. class AsyncOrderStats implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. const SUCCESS = 'success';
  22. private $orderId;
  23. private $order_info = null;
  24. private $uid = 0;
  25. public function __construct(int $order_id)
  26. {
  27. $this->orderId = $order_id;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return void
  33. */
  34. public function handle()
  35. {
  36. $this->init();
  37. //activity 活动统计
  38. $this->activityStats();
  39. //$this->saveActivityOrder();
  40. //图书充值统计
  41. $this->bookChargeStats();
  42. //订单上报给渠道
  43. $this->reportToChannel();
  44. }
  45. private function init()
  46. {
  47. $this->order_info = OrderService::getById($this->orderId);
  48. $this->uid = $this->order_info->uid;
  49. }
  50. private function activityStats()
  51. {
  52. $item = $this->order_info;
  53. if ($item->status != 'PAID') return;
  54. if (!$item->activity_id) return;
  55. $day = date('Y-m-d', strtotime($item->created_at));
  56. $exist = DB::table('activity_statistic_all')
  57. ->where('activity_id', $item->activity_id)
  58. ->where('day', $day)
  59. ->select('id', 'amount')
  60. ->where('distribution_channel_id', $item->distribution_channel_id)->first();
  61. if ($exist) {
  62. DB::table('activity_statistic_all')->where('id', $exist->id)->update(
  63. [
  64. 'amount' => $exist->amount + $item->price * 100,
  65. 'updated_at' => date('Y-m-d H:i:s')
  66. ]
  67. );
  68. return;
  69. }
  70. $pv = 0;
  71. $uv = 0;
  72. DB::table('activity_statistic_all')->insert([
  73. 'activity_id' => $item->activity_id, 'day' => $day, 'distribution_channel_id' => $item->distribution_channel_id,
  74. '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')
  75. ]);
  76. }
  77. private function bookChargeStats(){
  78. $bid = $this->order_info->from_bid;
  79. if(!$bid) return ;
  80. $day = date('Y-m-d', strtotime($this->order_info->created_at));
  81. $price = $this->order_info->price * 100;
  82. Redis::hincrby('book:charge:stats:'.$day, $bid, $price);
  83. }
  84. private function reportToChannel(){
  85. $link = $this->getReportUrl($this->distribution_channel_id);
  86. if(!$link) return ;
  87. $uid = $this->uid;
  88. $distribution_channel_id = $this->order_info->distribution_channel_id;
  89. $price = $this->order_info->price;
  90. $order_created_at = $this->order_info->created_at->format('Y-m-d H:i:s');
  91. $client = new Client(['timeout'=>3.0]);
  92. try{
  93. $result = $client->request('post',$link,['form_params'=>[
  94. 'uid'=>$uid,
  95. 'distribution_channel_id'=>$distribution_channel_id,
  96. 'price'=>$price,
  97. 'order_created_at'=>$order_created_at,
  98. ]])->getBody()->getContents();
  99. if($result && str_contains(strtolower($result),self::SUCCESS)){
  100. return ;
  101. }
  102. $error = 'result error ,'.$result;
  103. }catch (\Exception $e){
  104. $error = 'exception,'.$e->getMessage();
  105. }
  106. DB::table('report_to_channel_orders')->insert([
  107. 'uid'=>$uid,
  108. 'distribution_channel_id'=>$distribution_channel_id,
  109. 'price'=>$price,
  110. 'order_created_at'=>$order_created_at,
  111. 'report_status'=>0,
  112. 'error_msg'=>$error,
  113. 'created_at'=>date('Y-m-d H:i:s'),
  114. 'updated_at'=>date('Y-m-d H:i:s'),
  115. ]);
  116. }
  117. private function getReportUrl($distribution_channel_id){
  118. return '';
  119. }
  120. }