InstallWbwTemplate.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WbwTemplate;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class InstallWbwTemplate extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'install:wbwtemplate {from?} {to?}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (Tools::isStop()) {
  39. return 0;
  40. }
  41. $this->info('instert wbw template');
  42. $_from = $this->argument('from');
  43. $_to = $this->argument('to');
  44. if (empty($_from) && empty($_to)) {
  45. $_from = 1;
  46. $_to = 217;
  47. } elseif (empty($_to)) {
  48. $_to = $_from;
  49. }
  50. $fileListFileName = public_path('/palihtml/filelist.csv');
  51. $filelist = [];
  52. if (($handle = fopen($fileListFileName, 'r')) !== false) {
  53. while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) {
  54. }
  55. }
  56. $bar = $this->output->createProgressBar($_to - $_from + 1);
  57. for ($from = $_from; $from <= $_to; $from++) {
  58. // code...
  59. $fileSn = $from - 1;
  60. $outputFileNameHead = $filelist[$fileSn][1];
  61. $dirXmlBase = public_path('/tmp/palicsv').'/';
  62. $dirXml = $outputFileNameHead.'/';
  63. // 删除目标数据库中数据
  64. WbwTemplate::where('book', $from)->delete();
  65. // 打开文件并读取数据
  66. if (($GLOBALS['fp'] = fopen($dirXmlBase.$dirXml.$outputFileNameHead.'.csv', 'r')) !== false) {
  67. $GLOBALS['row'] = 0;
  68. DB::transaction(function () {
  69. while (($data = fgetcsv($GLOBALS['fp'], 0, ',')) !== false) {
  70. $GLOBALS['row']++;
  71. if ($GLOBALS['row'] == 1) {
  72. continue;
  73. }
  74. // 或略第一行 标题行
  75. $params = [
  76. 'book' => mb_substr($data[2], 1),
  77. 'paragraph' => $data[3],
  78. 'wid' => $data[16],
  79. 'word' => $data[4],
  80. 'real' => $data[5],
  81. 'type' => $data[6],
  82. 'gramma' => $data[7],
  83. 'part' => $data[10],
  84. 'style' => $data[15],
  85. ];
  86. WbwTemplate::insert($params);
  87. }
  88. });
  89. fclose($GLOBALS['fp']);
  90. } else {
  91. $this->error('can not open csv file. filename='.$dirXmlBase.$dirXml.$outputFileNameHead.'.csv'.PHP_EOL);
  92. Log::error('can not open csv file. filename='.$dirXmlBase.$dirXml.$outputFileNameHead.'.csv'.PHP_EOL);
  93. }
  94. $bar->advance();
  95. }
  96. $bar->finish();
  97. return 0;
  98. }
  99. }