CrmBookAutoRecommendService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Modules\Book\Services;
  3. use App\Consts\SysConsts;
  4. use App\Modules\Book\Models\Book;
  5. use App\Modules\Book\Models\BookConfig;
  6. use App\Modules\Book\Models\CrmBookAutoRecommend;
  7. use DB;
  8. use Redis;
  9. use function GuzzleHttp\json_decode;
  10. class CrmBookAutoRecommendService
  11. {
  12. private $this_week;
  13. private $redis_key;
  14. public function __construct()
  15. {
  16. $this->this_week = date('Ymd', strtotime('this week Monday'));
  17. $this->redis_key = 'crm_book_auto_recommend:' . $this->this_week;
  18. }
  19. /**
  20. * 生成推荐书籍
  21. */
  22. public function generateRecommendBooks()
  23. {
  24. if (CrmBookAutoRecommend::where('week', $this->this_week)->exists()) {
  25. return;
  26. } else {
  27. $categories = BookConfig::join('books', 'books.id', 'book_configs.bid')
  28. ->select('books.category_id')
  29. ->where('book_configs.is_on_shelf', 2)->where('book_configs.recommend_index', '>=', 80)
  30. ->groupBy('books.category_id')
  31. ->havingRaw('count(1)>=10')
  32. ->get();
  33. $categories = $categories->shuffle()->take(3)->all();
  34. if ($categories) {
  35. collect($categories)->each(function ($item) {
  36. $books = BookConfig::join('books', 'books.id', 'book_configs.bid')
  37. ->where('books.category_id', $item->category_id)
  38. ->where('book_configs.is_on_shelf', 2)->where('book_configs.recommend_index', '>=', 80)
  39. ->select('book_configs.bid')
  40. ->get();
  41. $books = $books->shuffle()->take(10)->all();
  42. $recommends = BookConfig::join('books', 'books.id', 'book_configs.bid')
  43. ->join('book_categories', 'book_categories.id', 'books.category_id')
  44. ->whereIn('book_configs.bid', collect($books)->pluck('bid')->all())
  45. ->select('book_configs.bid', 'book_configs.book_name', 'books.category_id', 'book_categories.category_name')
  46. ->get();
  47. $recommends = $recommends->transform(function ($item) {
  48. return [
  49. 'week' => $this->this_week,
  50. 'bid' => $item->bid,
  51. 'name' => $item->book_name,
  52. 'category_id' => $item->category_id,
  53. 'category_name' => $item->category_name,
  54. ];
  55. });
  56. DB::table('crm_book_auto_recommend')->insert($recommends->all());
  57. });
  58. }
  59. }
  60. }
  61. /**
  62. * 设置redis缓存
  63. */
  64. public function setRecommendBooksRedis()
  65. {
  66. $results = $this->getRecommendBooksFromDB();
  67. Redis::setex($this->redis_key, SysConsts::ONE_WEEK_SECONDS, json_encode($results));
  68. }
  69. /**
  70. * 获取推荐书籍
  71. */
  72. public function getRecommendBooksFromRedis()
  73. {
  74. $value = Redis::get($this->redis_key);
  75. if ($value) {
  76. return json_decode($value, true);
  77. } else {
  78. $results = $this->getRecommendBooksFromDB();
  79. Redis::setex($this->redis_key, SysConsts::ONE_WEEK_SECONDS, json_encode($results));
  80. return $results;
  81. }
  82. }
  83. /**
  84. * 获取推荐书籍
  85. */
  86. private function getRecommendBooksFromDB()
  87. {
  88. $recommends = CrmBookAutoRecommend::where('week', $this->this_week)->get();
  89. $bids = $recommends->pluck('bid')->all();
  90. if ($recommends) {
  91. $configs = BookConfig::whereIn('bid', $bids)->select('bid', 'cover', 'recommend_index')->get();
  92. $books = Book::whereIn('id', $bids)->select('id', 'status', 'intro', 'size', 'last_cid', 'last_chapter')->get();
  93. $results = $recommends->map(function ($item) use ($books, $configs) {
  94. $bid = $item->bid;
  95. $name = $item->name;
  96. $category_id = $item->category_id;
  97. $category_name = $item->category_name;
  98. $config = $configs->where('bid', $bid)->first();
  99. $book = $books->where('id', $bid)->first();
  100. $cover = $config->cover;
  101. $recommend_index = $config->recommend_index;
  102. $intro = $book->intro;
  103. $status = $book->status;
  104. $size = $book->size;
  105. $last_cid = $book->last_cid;
  106. $last_chapter = $book->last_chapter;
  107. $is_vip = 1;
  108. return compact('bid', 'name', 'category_id', 'category_name', 'cover', 'recommend_index', 'status', 'intro', 'size', 'last_cid', 'last_chapter', 'is_vip');
  109. });
  110. return $results;
  111. }
  112. }
  113. }