UpgradeRelatedParagraph.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $this->info("upgrade related.paragraph");
  42. $startTime = time();
  43. #删除目标数据库中数据
  44. RelatedParagraph::where('book','>',0)->delete();
  45. // 打开csv文件并读取数据
  46. $strFileName = config("app.path.pali_title") . "/cs6_para.csv";
  47. if(!file_exists($strFileName)){
  48. return 1;
  49. }
  50. $inputRow = 0;
  51. $fp = fopen($strFileName, "r");
  52. if (!$fp ) {
  53. $this->error("can not open csv $strFileName");
  54. Log::error("can not open csv $strFileName");
  55. }
  56. $bookTitles = BookTitle::orderBy('id','desc')->get();
  57. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  58. if($inputRow>0){
  59. if(!empty($this->argument('book'))){
  60. if($this->argument('book') !=$data[0] ){
  61. continue;
  62. }
  63. }
  64. //获取书号
  65. $bookId = 0;
  66. foreach ($bookTitles as $bookTitle) {
  67. # code...
  68. if((int)$data[0] === $bookTitle->book){
  69. if((int)$data[1] >= $bookTitle->paragraph){
  70. $bookId = $bookTitle->id;
  71. break;
  72. }
  73. }
  74. }
  75. $begin = (int) $data[3];
  76. $end = (int) $data[4];
  77. $arrPara = array();
  78. for ($i = $begin; $i <= $end; $i++) {
  79. $arrPara[] = $i;
  80. }
  81. foreach ($arrPara as $key => $para) {
  82. $newRow = new RelatedParagraph();
  83. $newRow->book = $data[0];
  84. $newRow->para = $data[1];
  85. $newRow->book_id = $bookId;
  86. $newRow->cs_para = $para;
  87. $newRow->book_name = $data[2];
  88. $newRow->save();
  89. }
  90. }
  91. $inputRow++;
  92. if($inputRow % 1000 == 0){
  93. $this->info($inputRow);
  94. }
  95. }
  96. fclose($fp);
  97. $this->info("all done. in ". time()-$startTime . "s" );
  98. return 0;
  99. }
  100. }