CacheWbwPreference.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Cache;
  6. use App\Models\WbwAnalysis;
  7. use Illuminate\Support\Facades\DB;
  8. class CacheWbwPreference extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'cache:wbw.preference {--editor=} {--view=all}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '逐词解析的首选项预热';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if(\App\Tools\Tools::isStop()){
  39. return 0;
  40. }
  41. $prefix = 'wbw-preference';
  42. if($this->option('view')==='all' ||
  43. $this->option('view')==='my'){
  44. $this->info('个人数据');
  45. /**
  46. * 个人数据算法
  47. * 最新优先
  48. */
  49. $wbw = WbwAnalysis::select(['wbw_word','type','editor_id']);
  50. $count = DB::select('SELECT count(*) from (
  51. SELECT wbw_word,type,editor_id from wbw_analyses group by wbw_word,type,editor_id) T');
  52. if($this->option('editor')){
  53. $wbw = $wbw->where('editor_id',$this->option('editor'));
  54. $count = DB::select('SELECT count(*) from (
  55. SELECT wbw_word,type,editor_id from wbw_analyses where editor_id=? group by wbw_word,type,editor_id) T',
  56. [$this->option('editor')]);
  57. }
  58. $wbw = $wbw->groupBy(['wbw_word','type','editor_id'])->cursor();
  59. $bar = $this->output->createProgressBar($count[0]->count);
  60. foreach ($wbw as $key => $value) {
  61. $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
  62. ->where('type',$value->type)
  63. ->where('editor_id',$value->editor_id)
  64. ->orderBy('updated_at','desc')
  65. ->value('data');
  66. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/{$value->editor_id}",$data);
  67. $bar->advance();
  68. }
  69. $bar->finish();
  70. }
  71. if($this->option('view')==='all' ||
  72. $this->option('view')==='community'
  73. ){
  74. $this->info('社区通用');
  75. /**
  76. * 社区数据算法
  77. * 多的优先
  78. */
  79. $wbw = WbwAnalysis::select(['wbw_word','type']);
  80. $count = DB::select('SELECT count(*) from (
  81. SELECT wbw_word,type from wbw_analyses group by wbw_word,type) T');
  82. $wbw = $wbw->groupBy(['wbw_word','type'])->cursor();
  83. $bar = $this->output->createProgressBar($count[0]->count);
  84. foreach ($wbw as $key => $value) {
  85. $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
  86. ->where('type',$value->type)
  87. ->selectRaw('data,count(*)')
  88. ->groupBy("data")
  89. ->orderBy("count", "desc")
  90. ->first();
  91. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/0",$data->data);
  92. $bar->advance();
  93. }
  94. $bar->finish();
  95. }
  96. return 0;
  97. }
  98. }