InstallWbwTemplate.php 2.6 KB

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