BookSpider.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GuzzleHttp\Client;
  5. use DB;
  6. class BookSpider extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'zytest';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'zytest';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->client = new Client(['timeout' => 8.0,'allow_redirects'=>true]);
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. //32401811
  38. $this->cpc();
  39. }
  40. private function cpc(){
  41. //32426552
  42. $sql_format = "SELECT u.id,f.subscribe_time,u.openid,(SELECT ifnull(sum(price),0) from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as amount FROM users u
  43. JOIN force_subscribe_users f on u.id = f.uid
  44. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  45. $all = 32427;
  46. for ($i =0;$i<=$all;$i++){
  47. $subscribes = DB::table('force_subscribe_users')->where('id','>',$i*1000)->select('uid')->limit(1000)->get();
  48. if($subscribes->isEmpty()){
  49. break;
  50. }
  51. foreach ($subscribes as $value){
  52. $result = DB::select(sprintf($sql_format,$value->uid));
  53. if($result){
  54. $user_property_info = $this->level($result);
  55. $this->create($user_property_info);
  56. }
  57. }
  58. }
  59. }
  60. private function level($res){
  61. $earliest_subscribe_time = $res[0]->subscribe_time;
  62. $subscribe_three_day_info = [];
  63. $subscribe_no_three_day_info = [];
  64. foreach ($res as $v){
  65. (strtotime($v->subscribe_time) < strtotime($earliest_subscribe_time)) && $earliest_subscribe_time = $v->subscribe_time;
  66. if(time()-strtotime($v->subscribe_time) >= 86400*3){
  67. array_push($subscribe_three_day_info,$v->amount);
  68. }else{
  69. array_push($subscribe_no_three_day_info,$v->amount);
  70. }
  71. }
  72. $result = [
  73. 'earliest_subscribe_time'=>$earliest_subscribe_time,
  74. 'property'=>'',
  75. 'type'=>'',
  76. 'openid'=> $res[0]->openid
  77. ];
  78. if($subscribe_three_day_info){
  79. //存量用户
  80. $result['type'] = 'CUILIANG';
  81. $amount = round(array_sum($subscribe_three_day_info)/count($subscribe_three_day_info),2);
  82. if($amount>15){
  83. $result['property'] = 'high';
  84. }elseif($amount >2){
  85. $result['property'] = 'medium';
  86. } elseif($amount >0){
  87. $result['property'] = 'low';
  88. } else{
  89. $result['property'] = 'none';
  90. }
  91. }else{
  92. //新用户
  93. $result['type'] = 'NEW';
  94. if($subscribe_no_three_day_info)
  95. $amount = max($subscribe_no_three_day_info);
  96. else
  97. $amount = 0;
  98. if($amount>50){
  99. $result['property'] = 'high';
  100. }elseif($amount >2){
  101. $result['property'] = 'medium';
  102. } elseif($amount >0){
  103. $result['property'] = 'low';
  104. } else{
  105. $result['property'] = 'none';
  106. }
  107. }
  108. return $result;
  109. }
  110. private function create($data){
  111. $old = DB::table('user_division_cpc_property')->where('openid',$data['openid'])->where('is_enable',1)->first();
  112. if(!$old){
  113. DB::table('user_division_cpc_property')->insert([
  114. 'openid'=>$data['openid'] ,
  115. 'property'=>$data['property'] ,
  116. 'is_enable'=>1 ,
  117. 'type'=>$data['type'] ,
  118. 'earliest_subscribe_time'=>$data['earliest_subscribe_time'] ,
  119. 'created_at'=>date('Y-m-d H:i:s'),
  120. 'updated_at'=>date('Y-m-d H:i:s')
  121. ]);
  122. }
  123. }
  124. }