BookSpider.php 5.0 KB

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