UpgradeRelatedParagraph.php 3.1 KB

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