BookSpider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 {--type=} {--bid=}';
  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. $type = $this->option('type');
  41. if($type == 'yunqi'){
  42. $bids = $this->option('bid');
  43. $bids = explode(',',$bids);
  44. foreach ($bids as $bid){
  45. $this->newYunQi($bid);
  46. }
  47. }
  48. }
  49. private function cpc(){
  50. //32426552
  51. $sql_format = "SELECT u.id,f.created_at as subscribe_time,u.openid,
  52. (SELECT ifnull(sum(price),0) from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as amount,
  53. (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
  54. FROM users u
  55. JOIN force_subscribe_users f on u.id = f.uid
  56. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  57. $all = 32427;
  58. for ($i =0;$i<=$all;$i++){
  59. $subscribes = DB::table('force_subscribe_users')->where('id','>',$i*1000)->select('uid')->limit(1000)->get();
  60. if($subscribes->isEmpty()){
  61. break;
  62. }
  63. foreach ($subscribes as $value){
  64. $result = DB::select(sprintf($sql_format,$value->uid));
  65. if($result){
  66. $user_property_info = $this->level($result);
  67. $this->create($user_property_info);
  68. }
  69. }
  70. }
  71. }
  72. private function level($res){
  73. $earliest_subscribe_time = $res[0]->subscribe_time;
  74. $subscribe_three_day_info = [];
  75. $subscribe_no_three_day_info = [];
  76. foreach ($res as $v){
  77. (strtotime($v->subscribe_time) < strtotime($earliest_subscribe_time)) && $earliest_subscribe_time = $v->subscribe_time;
  78. if(time()-strtotime($v->subscribe_time) >= 86400*3){
  79. array_push($subscribe_three_day_info,$v->amount);
  80. }else{
  81. array_push($subscribe_no_three_day_info,$v->three_day_amount);
  82. }
  83. }
  84. $result = [
  85. 'earliest_subscribe_time'=>$earliest_subscribe_time,
  86. 'property'=>'',
  87. 'type'=>'',
  88. 'openid'=> $res[0]->openid
  89. ];
  90. if($subscribe_three_day_info){
  91. //存量用户
  92. $result['type'] = 'CUILIANG';
  93. $amount = round(array_sum($subscribe_three_day_info)/count($subscribe_three_day_info),2);
  94. if($amount>15){
  95. $result['property'] = 'high';
  96. }elseif($amount >2){
  97. $result['property'] = 'medium';
  98. } elseif($amount >0){
  99. $result['property'] = 'low';
  100. } else{
  101. $result['property'] = 'none';
  102. }
  103. }else{
  104. //新用户
  105. $result['type'] = 'NEW';
  106. if($subscribe_no_three_day_info)
  107. $amount = max($subscribe_no_three_day_info);
  108. else
  109. $amount = 0;
  110. if($amount>50){
  111. $result['property'] = 'high';
  112. }elseif($amount >2){
  113. $result['property'] = 'medium';
  114. } elseif($amount >0){
  115. $result['property'] = 'low';
  116. } else{
  117. $result['property'] = 'none';
  118. }
  119. }
  120. return $result;
  121. }
  122. private function create($data){
  123. $old = DB::table('user_division_cpc_property')->where('openid',$data['openid'])->where('is_enable',1)->first();
  124. if(!$old){
  125. DB::table('user_division_cpc_property')->insert([
  126. 'openid'=>$data['openid'] ,
  127. 'property'=>$data['property'] ,
  128. 'is_enable'=>1 ,
  129. 'type'=>$data['type'] ,
  130. 'earliest_subscribe_time'=>$data['earliest_subscribe_time'] ,
  131. 'created_at'=>date('Y-m-d H:i:s'),
  132. 'updated_at'=>date('Y-m-d H:i:s')
  133. ]);
  134. }
  135. }
  136. public function cleanRedis()
  137. {
  138. $flag = 0;
  139. $i = 0;
  140. do{
  141. $result = Redis::scan($flag,['match'=>'wap:string:chapter:{*','count'=>1000]);
  142. if ($result) {
  143. if (isset($result[0])) {
  144. $flag = $result[0];
  145. }
  146. if (isset($result[1]) && $result[1] ) {
  147. foreach ($result[1] as $v) {
  148. Redis::del($v);
  149. echo $v . PHP_EOL;
  150. $i++;
  151. }
  152. }
  153. }
  154. }while($flag);
  155. echo 'del ' . $i . PHP_EOL;
  156. return "";
  157. }
  158. private function newYunQi($bid){
  159. $result = \App\Modules\Book\Services\BookService::newYunQiBook($bid);
  160. if($result == -1){
  161. echo 'yunqi book had collected'.PHP_EOL;
  162. }
  163. if ($result == -2) {
  164. echo 'new_yunqi book not exists'.PHP_EOL;
  165. }
  166. echo "OK!";
  167. }
  168. }