AsyncOrderStats.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $this->fireOrderReport();
  46. }
  47. private function init()
  48. {
  49. $this->order_info = OrderService::getById($this->orderId);
  50. $this->uid = $this->order_info->uid;
  51. }
  52. private function activityStats()
  53. {
  54. $item = $this->order_info;
  55. if ($item->status != 'PAID') return;
  56. if (!$item->activity_id) return;
  57. $day = date('Y-m-d', strtotime($item->created_at));
  58. $exist = DB::table('activity_statistic_all')
  59. ->where('activity_id', $item->activity_id)
  60. ->where('day', $day)
  61. ->select('id', 'amount')
  62. ->where('distribution_channel_id', $item->distribution_channel_id)->first();
  63. if ($exist) {
  64. DB::table('activity_statistic_all')->where('id', $exist->id)->update(
  65. [
  66. 'amount' => $exist->amount + $item->price * 100,
  67. 'updated_at' => date('Y-m-d H:i:s')
  68. ]
  69. );
  70. return;
  71. }
  72. $pv = 0;
  73. $uv = 0;
  74. DB::table('activity_statistic_all')->insert([
  75. 'activity_id' => $item->activity_id, 'day' => $day, 'distribution_channel_id' => $item->distribution_channel_id,
  76. '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')
  77. ]);
  78. }
  79. private function bookChargeStats(){
  80. $bid = $this->order_info->from_bid;
  81. if(!$bid) return ;
  82. $day = date('Y-m-d', strtotime($this->order_info->created_at));
  83. $price = $this->order_info->price * 100;
  84. Redis::hincrby('book:charge:stats:'.$day, $bid, $price);
  85. }
  86. private function reportToChannel(){
  87. $link = ReportToChannelUserAndOrderService::getReportUrl($this->order_info->distribution_channel_id,ReportToChannelUserAndOrderService::$order_type);
  88. if(!$link) return ;
  89. $uid = $this->uid;
  90. $distribution_channel_id = $this->order_info->distribution_channel_id;
  91. $price = $this->order_info->price;
  92. $order_created_at = $this->order_info->created_at->format('Y-m-d H:i:s');
  93. $client = new Client(['timeout'=>3.0]);
  94. try{
  95. $result = $client->request('post',$link,['form_params'=>[
  96. 'uid'=>$uid,
  97. 'distribution_channel_id'=>$distribution_channel_id,
  98. 'price'=>$price,
  99. 'order_created_at'=>$order_created_at,
  100. 'event'=>'order',
  101. 'source'=>'wangduyun'
  102. ]])->getBody()->getContents();
  103. if($result && str_contains(strtolower($result),self::SUCCESS)){
  104. return ;
  105. }
  106. $error = 'result error ,'.$result;
  107. }catch (\Exception $e){
  108. $error = 'exception,'.$e->getMessage();
  109. }
  110. DB::table('report_to_channel_orders')->insert([
  111. 'uid'=>$uid,
  112. 'distribution_channel_id'=>$distribution_channel_id,
  113. 'price'=>$price,
  114. 'order_created_at'=>$order_created_at,
  115. 'report_status'=>0,
  116. 'error_msg'=>$error,
  117. 'created_at'=>date('Y-m-d H:i:s'),
  118. 'updated_at'=>date('Y-m-d H:i:s'),
  119. ]);
  120. }
  121. private function fireOrderReport(){
  122. if ($this->order_info->status != 'PAID') return;
  123. $client = new Client();
  124. $reportData = [
  125. 'platform' => 'wangduyun',
  126. 'order' => [
  127. 'uid' => getProp($this->order_info, 'uid'),
  128. 'order_no' => getProp($this->order_info, 'trade_no'),
  129. 'price' => getProp($this->order_info, 'price'),
  130. 'channel_id' => getProp($this->order_info, 'distribution_channel_id'),
  131. 'pay_time' => getProp($this->order_info, 'pay_end_at'),
  132. 'order_created_at' => $this->order_info->created_at->format('Y-m-d H:i:s'),
  133. 'order_type'=> getProp($this->order_info, 'order_type'),
  134. 'from_bid'=> getProp($this->order_info, 'from_bid'),
  135. 'pay_type'=> getProp($this->order_info, 'pay_type')
  136. ]
  137. ];
  138. $record = [
  139. 'uid'=>$reportData['order']['uid'],
  140. 'trade_no'=>$reportData['order']['order_no'],
  141. 'price'=>$reportData['order']['price'],
  142. 'distribution_channel_id'=>$reportData['order']['channel_id'],
  143. 'pay_time'=>$reportData['order']['pay_time'],
  144. 'msg'=>'',
  145. 'status'=>0,
  146. 'created_at'=>date('Y-m-d H:i:s'),
  147. 'updated_at'=>date('Y-m-d H:i:s'),
  148. ];
  149. $host = 'https://firetrack.wd.amanbook.com';
  150. try{
  151. // 执行上报
  152. $result = $client->post($host . '/api/reportOrder', [
  153. 'headers' => [
  154. 'x-code' => 'Mvnx1Yr3O8i!TS5u'
  155. ],
  156. 'json' => $reportData
  157. ]);
  158. $record['msg'] = $result->getBody()->getContents();
  159. $record['status'] = 1;
  160. }catch (\Exception $e){
  161. $record['status'] = 2;
  162. $record['msg'] = '';
  163. \Log::error($e);
  164. }
  165. try{
  166. DB::table('fire_order_report_record')->insert($record);
  167. }catch (\Exception $e){
  168. }
  169. }
  170. }