AsyncOrderStats.php 4.2 KB

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