NewBookTestUserDetail.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Console\Commands\Book;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use Redis;
  6. class NewBookTestUserDetail extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'new_book_test_user_detail';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '新书测试用户数据';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $new_book_test_ids = DB::table('new_book_tests')
  37. ->leftjoin('new_book_test_users','new_book_test_users.test_id','new_book_tests.id')
  38. ->whereNotNull('new_book_tests.test_end_time')
  39. ->where('new_book_tests.status',2)
  40. ->whereNull('new_book_test_users.test_id')
  41. ->select('new_book_tests.*')
  42. ->get()
  43. ->pluck('id')
  44. ->toArray();
  45. if(!$new_book_test_ids){return;}
  46. foreach ($new_book_test_ids as $new_book_test_id){
  47. $this->getNewBookTestUserData($new_book_test_id);
  48. }
  49. }
  50. /**
  51. * 获取新书测试用户数据
  52. * @param $new_book_test_id
  53. */
  54. public static function getNewBookTestUserData($new_book_test_id){
  55. if(!$new_book_test_id)return [];
  56. $key ='NewBookTestUserDetail:'.$new_book_test_id;
  57. $count = Redis::ZCARD($key);
  58. $member_list = Redis::ZRANGE($key,0,$count);
  59. $insert_data = array();
  60. foreach ($member_list as $index=>$member){
  61. $data = array();
  62. $score = Redis::ZSCORE($key,$member);
  63. $data['uid'] = $member;
  64. $data['push_time'] = date('Y-m-d H:i:s',$score);
  65. $data['test_id'] = $new_book_test_id;
  66. $insert_data[] = $data;
  67. if(count($insert_data)>=1000){
  68. DB::table('new_book_test_users')->insert($insert_data);
  69. $insert_data = array();
  70. }
  71. }
  72. DB::table('new_book_test_users')->insert($insert_data);
  73. }
  74. }