SendOrderSendOrderPvUv.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/4/10
  6. * Time: 11:52
  7. */
  8. namespace App\Console\Commands\SendOrder;
  9. use DB;
  10. use Illuminate\Console\Command;
  11. use Log;
  12. use Redis;
  13. class SendOrderSendOrderPvUv extends Command
  14. {
  15. /**
  16. * 执行命令 php artisan send_order:generate_day_stat
  17. *
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'SendOrder:SendOrderSendOrderPvUv {--type=}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '派单即时访问量';
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle(){
  35. $type = $this->option('type');
  36. if($type == 'history'){
  37. $this->updateHistotyStats();
  38. }else{
  39. $this->updateTodayStats();
  40. }
  41. }
  42. private function updateTodayStats(){
  43. $day = date('Y-m-d');
  44. $i = 0;
  45. $send_order_list = Redis::SMEMBERS('send_order' . $day);
  46. //while (true){
  47. /*$list = Redis::sscan('send_order' . $day,$i,['match'=>'*','count'=>100]);
  48. if(!$list){
  49. break;
  50. }
  51. list($i,$send_order_list) = $list;*/
  52. foreach ($send_order_list as $send_order_id){
  53. $uv = (int)(Redis::hget('send_order_uv_'.$send_order_id,$day));
  54. $pv = (int)(Redis::hget('send_order_pv_'.$send_order_id,$day));
  55. $pvInfo = DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->select('history_pv','history_uv','total_pv','total_uv','uv_reach_200_time','uv_reach_500_time')->first();
  56. if($pvInfo){
  57. $total_uv = $pvInfo->history_uv+$uv;
  58. $total_pv = $pvInfo->history_pv+$pv;
  59. $update_data = [
  60. 'today_pv'=>$pv,
  61. 'today_uv'=>$uv,
  62. 'total_pv'=>$total_pv,
  63. 'total_uv'=>$total_uv,
  64. 'updated_at'=>date('Y-m-d H:i:s')
  65. ];
  66. if(!$pvInfo->uv_reach_200_time && ($total_uv-200) > 0){
  67. $update_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
  68. }
  69. if(!$pvInfo->uv_reach_200_time && $pvInfo->total_uv < 200 && $total_uv>200){
  70. $update_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
  71. }
  72. if(!$pvInfo->uv_reach_500_time && ($total_uv-500) > 0){
  73. $update_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
  74. }
  75. if(!$pvInfo->uv_reach_500_time && $pvInfo->total_uv < 500 && $total_uv>500){
  76. $update_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
  77. }
  78. DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->update($update_data);
  79. }else{
  80. $insert_data = [
  81. 'today_pv'=>$pv,
  82. 'today_uv'=>$uv,
  83. 'total_pv'=>$pv,
  84. 'total_uv'=>$uv,
  85. 'history_pv'=>0,
  86. 'history_uv'=>0,
  87. 'send_order_id'=>$send_order_id,
  88. 'created_at'=>date('Y-m-d H:i:s'),
  89. 'updated_at'=>date('Y-m-d H:i:s')
  90. ];
  91. if(($uv-200) > 0){
  92. $insert_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
  93. }
  94. if(($uv-500) > 0){
  95. $insert_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
  96. }
  97. DB::table('send_order_uv_pv')->insert($insert_data);
  98. }
  99. }
  100. //}
  101. }
  102. private function updateHistotyStats(){
  103. $day = date('Y-m-d',time()-86400);
  104. $i = 0;
  105. $send_order_list = Redis::SMEMBERS('send_order' . $day);
  106. //while (true){
  107. /*$list = Redis::sscan('send_order' . $day,$i,['match'=>'*','count'=>100]);
  108. if(!$list){
  109. break;
  110. }
  111. list($i,$send_order_list) = $list;*/
  112. foreach ($send_order_list as $send_order_id){
  113. $uv = (int)(Redis::hget('send_order_uv_'.$send_order_id,$day));
  114. $pv = (int)(Redis::hget('send_order_pv_'.$send_order_id,$day));
  115. $pvInfo = DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->select('history_pv','history_uv','uv_reach_200_time','uv_reach_500_time')->first();
  116. if($pvInfo){
  117. $total_uv = $pvInfo->history_uv+$uv;
  118. $total_pv = $pvInfo->history_pv+$pv;
  119. $update_data = [
  120. 'history_pv'=>$total_pv,
  121. 'history_uv'=>$total_uv,
  122. 'updated_at'=>date('Y-m-d H:i:s')
  123. ];
  124. DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->update($update_data);
  125. }else{
  126. $insert_data = [
  127. 'today_pv'=>0,
  128. 'today_uv'=>0,
  129. 'total_pv'=>$pv,
  130. 'total_uv'=>$uv,
  131. 'history_pv'=>$pv,
  132. 'history_uv'=>$uv,
  133. 'send_order_id'=>$send_order_id,
  134. 'created_at'=>date('Y-m-d H:i:s'),
  135. 'updated_at'=>date('Y-m-d H:i:s')
  136. ];
  137. if(abs($uv-200) <10){
  138. $insert_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
  139. }
  140. if(abs($uv-500) <10){
  141. $insert_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
  142. }
  143. DB::table('send_order_uv_pv')->insert($insert_data);
  144. }
  145. }
  146. //}
  147. }
  148. }