ChapterRetentionRate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Console\Commands\Book;
  3. use App\Modules\Book\Models\Book;
  4. use App\Modules\Book\Models\BookCategory;
  5. use App\Modules\Book\Models\BookConfig;
  6. use App\Modules\Book\Models\RententionBookList;
  7. use App\Modules\Book\Services\RententionBookService;
  8. use Illuminate\Console\Command;
  9. use Log;
  10. /**
  11. * 章节留存率
  12. */
  13. class ChapterRetentionRate extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'calc_chapter_retention_rate {--is_init=} {--is_all=} {--bid=} {--begin_bid=} {--end_bid=}';
  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. {
  43. print_r("======计算章节留存率 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
  44. Log::info("======计算章节留存率 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
  45. $is_all = $this->option('is_all');
  46. $is_init = $this->option('is_init');
  47. if ($is_init) {
  48. $bids = RententionBookList::where('sex', 2)->pluck('bid')->all();
  49. foreach ($bids as $bid) {
  50. RententionBookService::saveRententionBook($bid);
  51. }
  52. } else if ($is_all) {
  53. $bids = $this->findAllRunBooks();
  54. foreach ($bids as $bid) {
  55. $this->runBook($bid);
  56. }
  57. } else {
  58. $bid = $this->option('bid');
  59. $begin_bid = $this->option('begin_bid');
  60. $end_bid = $this->option('end_bid');
  61. if ($begin_bid) {
  62. $bids = Book::where('id', '>=', $begin_bid)
  63. ->where('id', '<', $end_bid)
  64. ->orderBy('id', 'desc')
  65. ->pluck('id')
  66. ->all();
  67. foreach ($bids as $bid) {
  68. $this->runBook($bid);
  69. }
  70. } else {
  71. $this->runBook($bid);
  72. }
  73. }
  74. Log::info("======计算章节留存率 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  75. print_r("======计算章节留存率 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  76. }
  77. public function findAllRunBooks()
  78. {
  79. $catetory_ids = BookCategory::where('pid', 2)->pluck('id')->all();
  80. $new_bids = Book::where('created_at', '>=', date('Y-m-d', strtotime('-3 months')))
  81. ->whereIn('category_id', $catetory_ids)
  82. ->pluck('id')->all();
  83. $bids = BookConfig::whereIn('bid', $new_bids)->whereIn('is_on_shelf', [1, 2])->pluck('bid')->all();
  84. $rentention_bids = RententionBookList::join('book_configs', 'book_configs.bid', 'rentention_book_list.bid')
  85. ->where([
  86. 'is_updated' => 1,
  87. ])->where('type', '!=', RententionBookService::BelowStandard)
  88. ->where('sex', 2)
  89. ->whereIn('is_on_shelf', [1, 2])
  90. ->pluck('rentention_book_list.bid')->all();
  91. return array_merge(
  92. $rentention_bids,
  93. $bids
  94. );
  95. }
  96. public function runBook(int $bid)
  97. {
  98. myLog('rentention_book')->info('bid: ' . $bid . ' begin: ' . date('Y-m-d H:i:s'));
  99. RententionBookService::runRentention($bid);
  100. RententionBookService::saveRententionBook($bid);
  101. myLog('rentention_book')->info('bid: ' . $bid . ' end: ' . date('Y-m-d H:i:s'));
  102. }
  103. }