InstallWbwTemplate.php 2.6 KB

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