UpgradeQuote.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\ChannelApi;
  4. use App\Models\PaliSentence;
  5. use App\Models\Sentence;
  6. use App\Models\WbwTemplate;
  7. use App\Tools\Tools;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Str;
  10. class UpgradeQuote extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:quote {book?}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if (Tools::isStop()) {
  41. return 0;
  42. }
  43. $start = time();
  44. $channelId = ChannelApi::getSysChannel('_System_Quote_');
  45. if ($channelId === false) {
  46. $this->error('no channel');
  47. return 1;
  48. }
  49. for ($i = 1; $i <= 217; $i++) {
  50. if (! empty($this->argument('book'))) {
  51. if ($i != $this->argument('book')) {
  52. continue;
  53. }
  54. }
  55. $pts_book = 0;
  56. $pts_page = 0;
  57. $myanmar_book = 0;
  58. $myanmar_page = 0;
  59. $cs_para = 0;
  60. $pali = PaliSentence::where('book', $i);
  61. $bar = $this->output->createProgressBar(PaliSentence::where('book', $i)->count());
  62. $pali = $pali->select('book', 'paragraph', 'word_begin', 'word_end')->cursor();
  63. foreach ($pali as $value) {
  64. // code...
  65. $wbwContent = [];
  66. $words = WbwTemplate::where('book', $value->book)
  67. ->where('paragraph', $value->paragraph)
  68. ->where('wid', '>=', $value->word_begin)
  69. ->where('wid', '<=', $value->word_end)
  70. ->orderBy('wid', 'asc')
  71. ->get();
  72. $sent = '';
  73. foreach ($words as $wbw_word) {
  74. // code...
  75. if ($wbw_word->type === '.ctl.') {
  76. if (substr($wbw_word->word, 0, 1) === 'M') {
  77. $m = explode('.', substr($wbw_word->word, 1));
  78. $myanmar_book = (int) $m[0];
  79. $myanmar_page = (int) $m[1];
  80. }
  81. if (substr($wbw_word->word, 0, 1) === 'P') {
  82. $m = explode('.', substr($wbw_word->word, 1));
  83. $pts_book = (int) $m[0];
  84. $pts_page = (int) $m[1];
  85. }
  86. }
  87. if ($wbw_word->style === 'paranum') {
  88. $cs_para = (int) $wbw_word->word;
  89. }
  90. }
  91. $wbwContent = [
  92. 'pts_book' => $pts_book,
  93. 'pts_page' => $pts_page,
  94. 'myanmar_book' => $myanmar_book,
  95. 'myanmar_page' => $myanmar_page,
  96. 'cs_para' => $cs_para,
  97. ];
  98. $sent = \json_encode($wbwContent, JSON_UNESCAPED_UNICODE);
  99. $newRow = Sentence::firstOrNew(
  100. [
  101. 'book_id' => $value->book,
  102. 'paragraph' => $value->paragraph,
  103. 'word_start' => $value->word_begin,
  104. 'word_end' => $value->word_end,
  105. 'channel_uid' => $channelId,
  106. ],
  107. [
  108. 'id' => app('snowflake')->id(),
  109. 'uid' => Str::uuid(),
  110. ]
  111. );
  112. $newRow->editor_uid = config('mint.admin.root_uuid');
  113. $newRow->content = trim($sent);
  114. $newRow->content_type = 'json';
  115. $newRow->strlen = mb_strlen($sent, 'UTF-8');
  116. $newRow->status = 10;
  117. $newRow->create_time = time() * 1000;
  118. $newRow->modify_time = time() * 1000;
  119. $newRow->language = 'en';
  120. $newRow->save();
  121. $bar->advance();
  122. }
  123. $bar->finish();
  124. }
  125. $this->info('finished '.(time() - $start).'s');
  126. return 0;
  127. }
  128. }