BookSpider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.created_at as subscribe_time,u.openid,
  43. (SELECT ifnull(sum(price),0) from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as amount,
  44. (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
  45. FROM users u
  46. JOIN force_subscribe_users f on u.id = f.uid
  47. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  48. $all = 32427;
  49. for ($i =0;$i<=$all;$i++){
  50. $subscribes = DB::table('force_subscribe_users')->where('id','>',$i*1000)->select('uid')->limit(1000)->get();
  51. if($subscribes->isEmpty()){
  52. break;
  53. }
  54. foreach ($subscribes as $value){
  55. $result = DB::select(sprintf($sql_format,$value->uid));
  56. if($result){
  57. $user_property_info = $this->level($result);
  58. $this->create($user_property_info);
  59. }
  60. }
  61. }
  62. }
  63. private function level($res){
  64. $earliest_subscribe_time = $res[0]->subscribe_time;
  65. $subscribe_three_day_info = [];
  66. $subscribe_no_three_day_info = [];
  67. foreach ($res as $v){
  68. (strtotime($v->subscribe_time) < strtotime($earliest_subscribe_time)) && $earliest_subscribe_time = $v->subscribe_time;
  69. if(time()-strtotime($v->subscribe_time) >= 86400*3){
  70. array_push($subscribe_three_day_info,$v->amount);
  71. }else{
  72. array_push($subscribe_no_three_day_info,$v->three_day_amount);
  73. }
  74. }
  75. $result = [
  76. 'earliest_subscribe_time'=>$earliest_subscribe_time,
  77. 'property'=>'',
  78. 'type'=>'',
  79. 'openid'=> $res[0]->openid
  80. ];
  81. if($subscribe_three_day_info){
  82. //存量用户
  83. $result['type'] = 'CUILIANG';
  84. $amount = round(array_sum($subscribe_three_day_info)/count($subscribe_three_day_info),2);
  85. if($amount>15){
  86. $result['property'] = 'high';
  87. }elseif($amount >2){
  88. $result['property'] = 'medium';
  89. } elseif($amount >0){
  90. $result['property'] = 'low';
  91. } else{
  92. $result['property'] = 'none';
  93. }
  94. }else{
  95. //新用户
  96. $result['type'] = 'NEW';
  97. if($subscribe_no_three_day_info)
  98. $amount = max($subscribe_no_three_day_info);
  99. else
  100. $amount = 0;
  101. if($amount>50){
  102. $result['property'] = 'high';
  103. }elseif($amount >2){
  104. $result['property'] = 'medium';
  105. } elseif($amount >0){
  106. $result['property'] = 'low';
  107. } else{
  108. $result['property'] = 'none';
  109. }
  110. }
  111. return $result;
  112. }
  113. private function create($data){
  114. $old = DB::table('user_division_cpc_property')->where('openid',$data['openid'])->where('is_enable',1)->first();
  115. if(!$old){
  116. DB::table('user_division_cpc_property')->insert([
  117. 'openid'=>$data['openid'] ,
  118. 'property'=>$data['property'] ,
  119. 'is_enable'=>1 ,
  120. 'type'=>$data['type'] ,
  121. 'earliest_subscribe_time'=>$data['earliest_subscribe_time'] ,
  122. 'created_at'=>date('Y-m-d H:i:s'),
  123. 'updated_at'=>date('Y-m-d H:i:s')
  124. ]);
  125. }
  126. }
  127. }