UpgradeFts.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\BookTitle;
  5. use App\Models\FtsText;
  6. use App\Models\WbwTemplate;
  7. class UpgradeFts extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'upgrade:fts {--content : upgrade col content}
  15. {para?}
  16. {--test : output log}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'upgrade full text search table';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. if($this->option('content')){
  40. if(!empty($this->argument('para'))){
  41. $para = explode('-',$this->argument('para'));
  42. $content = $this->getContent($para[0],$para[1]);
  43. if($this->option('test')){
  44. $this->info($content);
  45. }else{
  46. FtsText::where('book',$para[0])->where('paragraph',$para[1])->update(['content'=>$content]);
  47. }
  48. }else{
  49. for ($iBook=1; $iBook <= 217; $iBook++) {
  50. # code...
  51. $this->info('book:'.$iBook);
  52. $maxParagraph = WbwTemplate::where('book',$iBook)->max('paragraph');
  53. $bar = $this->output->createProgressBar($maxParagraph-1);
  54. for($iPara=1; $iPara <= $maxParagraph; $iPara++){
  55. $content = $this->getContent($iBook,$iPara);
  56. FtsText::where('book',$iBook)->where('paragraph',$iPara)->update(['content'=>$content]);
  57. $bar->advance();
  58. }
  59. $bar->finish();
  60. $this->info('done');
  61. }
  62. }
  63. }
  64. return 0;
  65. }
  66. private function getContent($book,$para){
  67. $words = WbwTemplate::where('book',$book)
  68. ->where('paragraph',$para)
  69. ->where('type',"<>",".ctl.")
  70. ->orderBy('wid')->get();
  71. $content = '';
  72. foreach ($words as $word) {
  73. if($word->style === 'bld'){
  74. if(strpos($word->word,"{")===FALSE){
  75. $content .= "**{$word->word}** ";
  76. }else{
  77. $content .= str_replace(['{','}'],['**','** '],$word->word);
  78. }
  79. }else if($word->style === 'note'){
  80. $content .= " _{$word->word}_ ";
  81. }else{
  82. $content .= $word->word . " ";
  83. }
  84. }
  85. return $content;
  86. }
  87. }