InstallWbwTemplate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $this->info("instert wbw template");
  41. $_from = $this->argument('from');
  42. $_to = $this->argument('to');
  43. if(empty($_from) && empty($_to)){
  44. $_from = 1;
  45. $_to = 217;
  46. }else if(empty($_to)){
  47. $_to = $_from;
  48. }
  49. $fileListFileName = public_path('/palihtml/filelist.csv');
  50. $filelist = array();
  51. if (($handle = fopen($fileListFileName, 'r')) !== false) {
  52. while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) {
  53. }
  54. }
  55. $bar = $this->output->createProgressBar($_to-$_from+1);
  56. for ($from=$_from; $from <=$_to ; $from++) {
  57. # code...
  58. $fileSn = $from-1;
  59. $outputFileNameHead = $filelist[$fileSn][1];
  60. $dirXmlBase = public_path('/tmp/palicsv') . "/";
  61. $dirXml = $outputFileNameHead . "/";
  62. #删除目标数据库中数据
  63. WbwTemplate::where('book', $from)->delete();
  64. // 打开文件并读取数据
  65. if (($GLOBALS["fp"] = fopen($dirXmlBase . $dirXml . $outputFileNameHead . ".csv", "r")) !== false) {
  66. $GLOBALS["row"]=0;
  67. DB::transaction(function () {
  68. while (($data = fgetcsv($GLOBALS["fp"], 0, ',')) !== false) {
  69. $GLOBALS["row"]++;
  70. if($GLOBALS["row"]==1){
  71. continue;
  72. }
  73. #或略第一行 标题行
  74. $params = [
  75. 'book'=>mb_substr($data[2], 1),
  76. 'paragraph'=>$data[3],
  77. 'wid'=>$data[16],
  78. 'word'=>$data[4],
  79. 'real'=>$data[5],
  80. 'type'=>$data[6],
  81. 'gramma'=>$data[7],
  82. 'part'=>$data[10],
  83. 'style'=>$data[15]
  84. ];
  85. WbwTemplate::insert($params);
  86. }
  87. });
  88. fclose($GLOBALS["fp"]);
  89. } else {
  90. $this->error("can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv".PHP_EOL) ;
  91. Log::error("can not open csv file. filename=" . $dirXmlBase . $dirXml . $outputFileNameHead . ".csv".PHP_EOL) ;
  92. }
  93. $bar->advance();
  94. }
  95. $bar->finish();
  96. return 0;
  97. }
  98. }