SetUserNovelPorperty.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Console\Commands\User;
  3. use App\Modules\Book\Models\Book;
  4. use App\Modules\Book\Models\BookCategory;
  5. use App\Modules\Book\Models\NovelUserPorperty;
  6. use App\Modules\OfficialAccount\Models\TempForceSubscribeUsers;
  7. use App\Modules\SendOrder\Models\SendOrder;
  8. use App\Modules\User\Models\User;
  9. use Illuminate\Console\Command;
  10. class SetUserNovelPorperty extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'set_user_novel_property {--is_init=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '设置小说用户属性';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * 书籍分类
  35. */
  36. private $categorys;
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $this->categorys = BookCategory::all();
  45. $is_init = $this->option('is_init');
  46. if ($is_init) {
  47. $this->runAllUsers();
  48. } else {
  49. myLog('test')->info('set_user_novel_property begin: ' . now());
  50. $users = $this->findUsers();
  51. foreach ($users as $user) {
  52. $property = $this->findUserProperty($user->uid);
  53. if ($property) {
  54. $this->createNovelUserPorperty($user->uid, $property);
  55. }
  56. }
  57. myLog('test')->info('set_user_novel_property end: ' . now());
  58. }
  59. }
  60. private function findUsers()
  61. {
  62. $model = NovelUserPorperty::model(date('Ymd'));
  63. $max_id = $model->max('id');
  64. $user = $model->find($max_id);
  65. $max = TempForceSubscribeUsers::where('uid', $user->uid)->first();
  66. return TempForceSubscribeUsers::where('id', '>', $max->id)
  67. ->where('uid', '>', 0)
  68. ->where('last_interactive_time', '>=', date('Y-m-d H:i:s', strtotime('-1 hours -20 seconds')))
  69. ->select('uid')
  70. ->get();
  71. }
  72. private function runAllUsers()
  73. {
  74. $max_ids = TempForceSubscribeUsers::max('id');
  75. $i = 0;
  76. myLog('test')->info('set_user_novel_property begin: ' . now());
  77. while ($i <= $max_ids) {
  78. $users = TempForceSubscribeUsers::where('id', '<=', $i + 3000)
  79. ->where('id', '>', $i)
  80. ->where('uid', '>', 0)
  81. ->select('uid')
  82. ->get();
  83. foreach ($users as $user) {
  84. $property = $this->findUserProperty($user->uid);
  85. if ($property) {
  86. $this->createNovelUserPorperty($user->uid, $property);
  87. }
  88. }
  89. $i += 3000;
  90. myLog('test')->info('set_user_novel_property id: ' . $i);
  91. }
  92. myLog('test')->info('set_user_novel_property end: ' . now());
  93. }
  94. private function findUserProperty(int $uid)
  95. {
  96. $user = User::where('id', $uid)->select('send_order_id')->first();
  97. if ($user) {
  98. $send_order_id = $user->send_order_id;
  99. $send_order = SendOrder::where('id', $send_order_id)->select('book_id')->first();
  100. if ($send_order) {
  101. $book_id = $send_order->book_id;
  102. $book = Book::where('id', $book_id)->select('category_id')->first();
  103. if ($book) {
  104. $category_id = $book->category_id;
  105. $category = $this->categorys->where('id', $category_id)->first();
  106. return $category;
  107. }
  108. }
  109. }
  110. }
  111. private function createNovelUserPorperty(int $uid, BookCategory $property)
  112. {
  113. for ($i = 0; $i < 3; $i++) {
  114. if ($i) {
  115. $date = date('Ymd', strtotime('+' . $i . ' days'));
  116. } else {
  117. $date = date('Ymd');
  118. }
  119. NovelUserPorperty::model($date)->updateOrCreate([
  120. 'uid' => $uid
  121. ], [
  122. 'category_id' => $property->id,
  123. 'category' => $property->category_name,
  124. 'sex' => $property->channel_name == '女频' ? 2 : 1,
  125. ]);
  126. }
  127. }
  128. }