UpgradeRelatedParagraph.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * 更新段落关联数据库
  4. * 用于找到根本和义注复注的对应段落
  5. */
  6. namespace App\Console\Commands;
  7. use App\Models\BookTitle;
  8. use App\Models\RelatedParagraph;
  9. use App\Tools\Tools;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Log;
  12. class UpgradeRelatedParagraph extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'upgrade:related.paragraph {book?}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. if (Tools::isStop()) {
  43. return 0;
  44. }
  45. $this->info('upgrade related.paragraph');
  46. $startTime = time();
  47. // 删除目标数据库中数据
  48. RelatedParagraph::where('book', '>', 0)->delete();
  49. // 打开csv文件并读取数据
  50. $strFileName = config('mint.path.pali_title').'/cs6_para.csv';
  51. if (! file_exists($strFileName)) {
  52. return 1;
  53. }
  54. $inputRow = 0;
  55. $fp = fopen($strFileName, 'r');
  56. if (! $fp) {
  57. $this->error("can not open csv $strFileName");
  58. Log::error("can not open csv $strFileName");
  59. }
  60. $bookTitles = BookTitle::orderBy('sn', 'desc')->get();
  61. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  62. if ($inputRow > 0) {
  63. if (! empty($this->argument('book'))) {
  64. if ($this->argument('book') != $data[0]) {
  65. continue;
  66. }
  67. }
  68. // 获取书号
  69. $bookId = 0;
  70. foreach ($bookTitles as $bookTitle) {
  71. // code...
  72. if ((int) $data[0] === $bookTitle->book) {
  73. if ((int) $data[1] >= $bookTitle->paragraph) {
  74. $bookId = $bookTitle->id;
  75. break;
  76. }
  77. }
  78. }
  79. $begin = (int) $data[3];
  80. $end = (int) $data[4];
  81. $arrPara = [];
  82. for ($i = $begin; $i <= $end; $i++) {
  83. $arrPara[] = $i;
  84. }
  85. foreach ($arrPara as $key => $para) {
  86. $newRow = new RelatedParagraph;
  87. $newRow->book = $data[0];
  88. $newRow->para = $data[1];
  89. $newRow->book_id = $bookId;
  90. $newRow->cs_para = $para;
  91. $newRow->book_name = $data[2];
  92. $newRow->save();
  93. }
  94. }
  95. $inputRow++;
  96. if ($inputRow % 1000 == 0) {
  97. $this->info($inputRow);
  98. }
  99. }
  100. fclose($fp);
  101. $this->info('all done. in '.time() - $startTime.'s');
  102. return 0;
  103. }
  104. }