UserLastChapterOrder.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/9/16
  6. * Time: 15:31
  7. */
  8. namespace App\Console\Commands\User;
  9. use DB;
  10. use Redis;
  11. use Illuminate\Console\Command;
  12. class UserLastChapterOrder extends Command
  13. {
  14. const USER_LAST_CHAPTER_ORDER = 'userLastChapterOrder';
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'User:UserLastChapterOrder';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '用户最近订阅';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle(){
  42. $this->start();
  43. }
  44. private function start(){
  45. $record = Redis::hgetAll(self::USER_LAST_CHAPTER_ORDER);
  46. if(!$record) return ;
  47. Redis::del(self::USER_LAST_CHAPTER_ORDER);
  48. $save_data = [];
  49. $temp = 1;
  50. foreach ($record as $uid=>$bid){
  51. $exists = DB::table('user_last_chapter_order')->where('uid',$uid)->select('id','bid')->first();
  52. if($exists){
  53. if($exists->bid == $bid)continue;
  54. DB::table('user_last_chapter_order')->where('uid',$uid)->update([
  55. 'bid'=>$bid,'updated_at'=>date('Y-m-d H:i:s')
  56. ]);
  57. }else{
  58. $save_data[] = ['uid'=>$uid,'bid'=>$bid,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  59. if($temp++ % 100 == 0){
  60. DB::table('user_last_chapter_order')->insert($save_data);
  61. $save_data = [];
  62. }
  63. }
  64. }
  65. if($save_data) {
  66. DB::table('user_last_chapter_order')->insert($save_data);
  67. }
  68. }
  69. }