InstallWbwTemplate.php 2.8 KB

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