UpgradeWbwParaNum.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WbwTemplate;
  4. use Illuminate\Console\Command;
  5. class UpgradeWbwParaNum extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. * php artisan upgrade:wbw.para.num 130
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'upgrade:wbw.para.num {book?}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. if (empty($this->argument('book'))) {
  37. $start = 1;
  38. $end = 217;
  39. } else {
  40. $start = $this->argument('book');
  41. $end = $this->argument('book');
  42. }
  43. for ($iBook = $start; $iBook <= $end; $iBook++) {
  44. $this->info("book = {$iBook}");
  45. $startAt = time();
  46. $count = WbwTemplate::where('book', $iBook)->count();
  47. $bar = $this->output->createProgressBar($count);
  48. $rows = WbwTemplate::where('book', $iBook)
  49. ->orderBy('paragraph')
  50. ->orderBy('wid')->cursor();
  51. $start = false;
  52. $bookCode = '';
  53. $count = 0;
  54. $currPara = 0;
  55. $bookCodeStack = [];
  56. foreach ($rows as $key => $row) {
  57. $bar->advance();
  58. if ($row->paragraph !== $currPara) {
  59. $currPara = $row->paragraph;
  60. $start = false;
  61. $bookCode = '';
  62. $bookCodeStack = [];
  63. }
  64. if ($row->word === '(') {
  65. $start = true;
  66. $bookCode = '';
  67. $bookCodeStack = [];
  68. continue;
  69. }
  70. if ($start) {
  71. if (is_numeric(str_replace('-', '', $row->word))) {
  72. if (empty($bookCode) && count($bookCodeStack) > 0) {
  73. // 继承之前的
  74. $bookCodeList = [];
  75. foreach ($bookCodeStack as $key => $value) {
  76. $bookCodeList[] = $key;
  77. }
  78. $bookCode = $bookCodeList[0];
  79. }
  80. $dot = mb_strrpos($bookCode, '.', 0, 'UTF-8');
  81. if ($dot === false) {
  82. $bookName = $bookCode;
  83. $paraNum = $row->word;
  84. } else {
  85. $bookName = mb_substr($bookCode, 0, $dot + 1, 'UTF-8');
  86. $paraNum = mb_substr($bookCode, $dot + 1, null, 'UTF-8').$row->word;
  87. }
  88. $bookName = mb_strtolower(mb_substr($bookName, 0, 64, 'UTF-8'), 'UTF-8');
  89. $bookCodeStack[$bookName] = 1;
  90. if (! empty($bookName)) {
  91. WbwTemplate::where('id', $row->id)->update([
  92. 'type' => ':cs.para:',
  93. 'gramma' => $bookName,
  94. 'part' => $paraNum,
  95. ]);
  96. }
  97. $count++;
  98. } elseif ($row->word === ';') {
  99. $bookCode = '';
  100. continue;
  101. } elseif ($row->word === ')') {
  102. $start = false;
  103. continue;
  104. }
  105. $bookCode .= $row->word;
  106. }
  107. }
  108. $bar->finish();
  109. $time = time() - $startAt;
  110. $this->info(" {$time}s {$count}");
  111. }
  112. return 0;
  113. }
  114. }