CacheWbwPreference.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $prefix = 'wbw-preference';
  39. if($this->option('view')==='all' ||
  40. $this->option('view')==='my'){
  41. $this->info('个人数据');
  42. /**
  43. * 个人数据算法
  44. * 最新优先
  45. */
  46. $wbw = WbwAnalysis::select(['wbw_word','type','editor_id']);
  47. $count = DB::select('SELECT count(*) from (
  48. SELECT wbw_word,type,editor_id from wbw_analyses group by wbw_word,type,editor_id) T');
  49. if($this->option('editor')){
  50. $wbw = $wbw->where('editor_id',$this->option('editor'));
  51. $count = DB::select('SELECT count(*) from (
  52. SELECT wbw_word,type,editor_id from wbw_analyses where editor_id=? group by wbw_word,type,editor_id) T',
  53. [$this->option('editor')]);
  54. }
  55. $wbw = $wbw->groupBy(['wbw_word','type','editor_id'])->cursor();
  56. $bar = $this->output->createProgressBar($count[0]->count);
  57. foreach ($wbw as $key => $value) {
  58. $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
  59. ->where('type',$value->type)
  60. ->where('editor_id',$value->editor_id)
  61. ->orderBy('updated_at','desc')
  62. ->value('data');
  63. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/{$value->editor_id}",$data);
  64. $bar->advance();
  65. }
  66. $bar->finish();
  67. }
  68. if($this->option('view')==='all' ||
  69. $this->option('view')==='community'
  70. ){
  71. $this->info('社区通用');
  72. /**
  73. * 社区数据算法
  74. * 多的优先
  75. */
  76. $wbw = WbwAnalysis::select(['wbw_word','type']);
  77. $count = DB::select('SELECT count(*) from (
  78. SELECT wbw_word,type from wbw_analyses group by wbw_word,type) T');
  79. $wbw = $wbw->groupBy(['wbw_word','type'])->cursor();
  80. $bar = $this->output->createProgressBar($count[0]->count);
  81. foreach ($wbw as $key => $value) {
  82. $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
  83. ->where('type',$value->type)
  84. ->selectRaw('data,count(*)')
  85. ->groupBy("data")
  86. ->orderBy("count", "desc")
  87. ->first();
  88. Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/0",$data->data);
  89. $bar->advance();
  90. }
  91. $bar->finish();
  92. }
  93. return 0;
  94. }
  95. }