UpdateRecommandBooks.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands\SmartPush;
  3. use App\Modules\MediaPush\Models\MediaPushBookConfigs;
  4. use App\Modules\MediaPush\Models\MediaPushRelationBooks;
  5. use Illuminate\Console\Command;
  6. use DB;
  7. class UpdateRecommandBooks extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'update_recommand_books';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '更新图书推送的书籍信息';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->updateRecommandBooks();
  38. }
  39. /**
  40. * 获取推荐的书籍
  41. */
  42. public function updateRecommandBooks()
  43. {
  44. $bids = MediaPushBookConfigs::getBids();;
  45. $whereInBids = '(';
  46. foreach ($bids as $bidItem) {
  47. $whereInBids = $whereInBids . $bidItem->bid . ',';
  48. }
  49. $str_count = strlen($whereInBids);
  50. if ($str_count > 2) {
  51. $whereInBids = substr($whereInBids, 0, $str_count - 1);
  52. }
  53. $whereInBids .= ')';
  54. if ($bids) {
  55. $pro_bids = '';
  56. foreach ($bids as $item) {
  57. $bid = $item->bid;
  58. $sql = "select bid,count(*) as user_count from deep_read_records where bid <>{$bid} and bid in {$whereInBids} and uid in (select uid from deep_read_records where bid='{$bid}') GROUP BY bid ORDER BY user_count desc limit 5";
  59. $result = DB::select($sql);
  60. if ($result) {
  61. foreach ($result as $book_item) {
  62. $pro_bids = $pro_bids . $book_item->bid;
  63. $pro_bids = $pro_bids . ',';
  64. }
  65. $pro_bids = substr($pro_bids, 0, strlen($pro_bids) - 1);
  66. $updated_at = date("Y-m-d H:i:s");
  67. $created_at = date("Y-m-d H:i:s");
  68. $updateParam = compact('pro_bids', 'updated_at','created_at');
  69. MediaPushRelationBooks::createdOrUpdated(compact('bid'), $updateParam);
  70. $pro_bids = '';
  71. }
  72. }
  73. }
  74. }
  75. }