BookRelationInit.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\admin\command;
  3. use app\common\library\Redis;
  4. use app\common\service\BookRelationService;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\input\Argument;
  8. use think\console\Output;
  9. use think\Log;
  10. use think\Request;
  11. class BookRelationInit extends Command
  12. {
  13. /**
  14. * 配置指令
  15. */
  16. protected function configure()
  17. {
  18. $this->setName('BookRelationInit');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  23. $redis = Redis::instance();
  24. try {
  25. $ids = model('book_relation')->field('book_id')->group('book_id')->select();
  26. if ( empty($ids)){
  27. Log::info('table: book_relation empty' );
  28. }
  29. foreach ($ids as $k => $v) {
  30. $str = BookRelationService::instance()->getBookRelationById($v['book_id']);
  31. if ( $redis->Exists( 'B:'.$v['book_id']) ){
  32. $redis->hSet( 'B:'.$v['book_id'], 'relation_id', $str );
  33. Log::info('book_relation redis update:'.$v['book_id'].' relations:'.$str );
  34. }
  35. }
  36. } catch (\Throwable $Th) {
  37. Log::info('error:'.$Th->getMessage());
  38. }
  39. Log::info('BookRelationInit over');
  40. }
  41. }