BookEnum.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\admin\command;
  3. use app\common\model\Book;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\Db;
  8. use think\Request;
  9. class BookEnum extends Command
  10. {
  11. /**
  12. * 配置指令
  13. */
  14. protected function configure()
  15. {
  16. $this->setName('BookEnum')->setDescription('遍历 book mysql 与 book change redis');
  17. }
  18. protected function execute(Input $input, Output $output)
  19. {
  20. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  21. try {
  22. /** @var Book $bookModel */
  23. $bookModel = model('book');
  24. Db::table('book')->field('id,name,realname')->chunk(100, function ($bookList) use ($bookModel, $output) {
  25. foreach ($bookList as $book) {
  26. $bookInfo = $bookModel::getBookInfo($book['id']);
  27. $output->info("mysql book:" . json_encode($book,
  28. JSON_UNESCAPED_UNICODE) . " redis book:" . json_encode($bookInfo, JSON_UNESCAPED_UNICODE));
  29. }
  30. });
  31. } catch (\Throwable $Th) {
  32. $output->info('error msg:' . $Th->getMessage().' trace:'.$Th->getTraceAsString());
  33. }
  34. $output->info('book enum over');
  35. }
  36. }