sunhao 1 年間 前
コミット
b02672108f
共有1 個のファイルを変更した85 個の追加12 個の削除を含む
  1. 85 12
      app/Console/Commands/ZeanExcel.php

+ 85 - 12
app/Console/Commands/ZeanExcel.php

@@ -29,6 +29,35 @@ class ZeanExcel extends Command
29 29
 
30 30
         $this->info(date('m-d H:i:s') . ' 开始整理');
31 31
 
32
+  /*      $source = '品倍福-电影';
33
+        #获取列序列
34
+        $sourcefile1 = public_path("upload/csv/".$source).'/'.$source.'_0.Csv';
35
+
36
+        $cn = $this->getCsvColSort($sourcefile1);
37
+
38
+        $flitFiles = $this->readCsvFiles($source);
39
+        foreach($flitFiles as $csvPath){
40
+            $first = $csvPath == $sourcefile1 ? 1 : 0;
41
+            $this->stat($csvPath, $source, 'Csv', $cn, $first); 
42
+        }
43
+        exit;*/
44
+
45
+       /* $source = '品倍福-电影';
46
+        $file = public_path("upload/csv/".$source).'/'.$source.'_0.Csv';
47
+        $cn = $this->getCsvColSort('C:\Users\admin\Desktop\新建文件夹\博影-影城.xlsx');
48
+        exit;*/
49
+        //$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
50
+        
51
+       /* $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
52
+        
53
+        //$filePath= 'C:\Users\admin\Desktop\新建文件夹\博影-影城.xlsx';
54
+        $filePath= 'C:\Users\admin\Desktop\工作簿2.csv';
55
+        $filePath= 'C:\Users\admin\Desktop\品倍福-电影_0.csv';
56
+        //$reader->setInputEncoding($strCode);
57
+        $spreadsheet = $reader->load($filePath);
58
+        //echo "\n yes";
59
+        exit;*/
60
+
32 61
         $time = time();
33 62
         //$rkey = 'zean.sourceExcelExportdo';
34 63
         while(true){
@@ -54,6 +83,7 @@ class ZeanExcel extends Command
54 83
             foreach($files as $filePath){
55 84
                 $source = strstr(basename($filePath), '.xlsx', true);
56 85
                 $data = $excel->cutFromFile($filePath, $source);
86
+                //echo "\nCCC";
57 87
                 if($data == 49999){
58 88
                     $this->stat($filePath, $source, 'xlsx'); //小于49999,直接处理
59 89
                 } else {
@@ -84,8 +114,13 @@ class ZeanExcel extends Command
84 114
 
85 115
     public function getCsvColSort($filePath)
86 116
     {
87
-        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
88
-        $spreadsheet = $reader->load($filePath);
117
+        //echo "\nDDD";
118
+        //echo "\n".$filePath;
119
+        
120
+        //$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
121
+        //$spreadsheet = $reader->load($filePath);
122
+        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filePath);
123
+        //echo "\nEEE";
89 124
         $data = $spreadsheet->getActiveSheet()->toArray();
90 125
 
91 126
         $columns = ['供应商全称', '供应商简称', '发票类型', '税率', '结算金额', '结算类型'];
@@ -93,8 +128,10 @@ class ZeanExcel extends Command
93 128
 
94 129
         $cn = array();
95 130
         $row = array_shift($data);
131
+
96 132
         //确认需要的列
97 133
         foreach($row as $col_key => $col_val){
134
+            $col_val = trim($col_val, '"');
98 135
             if(in_array($col_val, $columns)){
99 136
                 $cn[$col_t[$col_val]] = $col_key;
100 137
             }
@@ -107,6 +144,8 @@ class ZeanExcel extends Command
107 144
 
108 145
         ksort($cn);
109 146
 
147
+        //echo "\nFFF";
148
+
110 149
         return $cn;            
111 150
     }
112 151
 
@@ -121,6 +160,7 @@ class ZeanExcel extends Command
121 160
         } else {
122 161
             $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
123 162
         }
163
+
124 164
         //$reader->setInputEncoding($strCode);
125 165
         $spreadsheet = $reader->load($filePath);
126 166
         $data = $spreadsheet->getActiveSheet()->toArray();
@@ -132,6 +172,7 @@ class ZeanExcel extends Command
132 172
             $list = array();
133 173
             
134 174
             foreach ($data as $k => $row) {
175
+
135 176
                 if(!$row[0]){
136 177
                     break;
137 178
                 }
@@ -151,6 +192,7 @@ class ZeanExcel extends Command
151 192
                     ksort($cn);
152 193
                     continue;
153 194
                 } elseif($ext == 'Csv' && $k==0 && $first){
195
+                    //echo "\n888888";
154 196
                     continue;
155 197
                 } 
156 198
 
@@ -158,6 +200,8 @@ class ZeanExcel extends Command
158 200
                     continue;
159 201
                 }
160 202
 
203
+                //echo "\n33333";
204
+
161 205
                 $list[] = [
162 206
                     'source' => $source, 
163 207
                     'title' => $row[$cn[0]], 
@@ -173,6 +217,7 @@ class ZeanExcel extends Command
173 217
                     $this->info(date('m-d H:i:s') . ' 插入1000条 '. $source);
174 218
                     $list = array();
175 219
                 }
220
+                //echo "\n44444";
176 221
 
177 222
             }
178 223
 
@@ -182,7 +227,7 @@ class ZeanExcel extends Command
182 227
                 $this->info(date('m-d H:i:s') . ' 插入'.$en.'条 '. $source);
183 228
                 $list = null;
184 229
             }
185
-
230
+            //echo "\n55555";
186 231
             $spreadsheet->disconnectWorksheets();
187 232
             unset($spreadsheet);
188 233
             unset($data);
@@ -304,6 +349,7 @@ class ZeanExcel extends Command
304 349
 
305 350
     function readCsvFiles($source)
306 351
     {
352
+        //echo "\nGGG";
307 353
         $filePaths = array();
308 354
         $dir = public_path("upload/csv/".$source.'/');  // 文件夹路径
309 355
 
@@ -375,11 +421,35 @@ class MyreadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
375 421
  */
376 422
 class ExcelCutHelper
377 423
 {
378
-    public $cutNum = 5;
424
+    public $cutNum = 3;
379 425
     public $returnType = 'Csv';
380 426
     public $fileDir = 'upload/csv/';
381 427
     public $log;
382
-    
428
+
429
+    /**
430
+     * 切割字符串
431
+     * @param $str
432
+     * @return array|bool
433
+     */
434
+    public function cutFromStr($str)
435
+    {
436
+        try {
437
+            $filePath = '/tmp/' . time() . mt_rand(1000, 9000) . $this->returnType;
438
+            file_put_contents($filePath, $str);
439
+            if (file_exists($filePath)) {
440
+                $result =  $this->cutFromFile($filePath);
441
+                unlink($filePath);
442
+                return $result;
443
+            } else {
444
+                throw new Exception('文件写入错误');
445
+            }
446
+        } catch (Exception $e) {
447
+            $this->log = $e->getMessage();
448
+            return false;
449
+        }
450
+
451
+    }
452
+
383 453
     /**
384 454
      * 切割文件
385 455
      * @param $file
@@ -403,6 +473,7 @@ class ExcelCutHelper
403 473
             $reader->setReadDataOnly(true);
404 474
             $reader->setReadFilter($myFilter);
405 475
             
476
+            //echo "\nAAA";
406 477
             foreach ($cutRules as $sheetName => $rowIndexRange) {
407 478
                 //读
408 479
                 list($myFilter->startRow, $myFilter->endRow, $myFilter->worksheetName) = $rowIndexRange;
@@ -426,8 +497,10 @@ class ExcelCutHelper
426 497
                 $spreadsheetWriter->disconnectWorksheets();
427 498
                 unset($spreadsheetWriter);
428 499
                 $results[] = $saveFile;
500
+                //echo "\nXXX";
429 501
             }
430 502
 
503
+            //echo "\nBBB";
431 504
             unset($myFilter);
432 505
             unset($reader);
433 506
             
@@ -452,22 +525,22 @@ class ExcelCutHelper
452 525
             $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
453 526
             $reader->setReadDataOnly(true); //只读数据
454 527
             $reader->setReadFilter($myFilter);
455
-            //$spreadsheet = $reader->load($file);
528
+            $spreadsheet = $reader->load($file);
456 529
             //$sheetData = $spreadsheet->getActiveSheet()->toArray(null, false, false, false);
457 530
             list($fileName,) = explode('.', basename($file));
458 531
             $datas = array();
459 532
 
460 533
             $fcount = reset($myFilter->record); //第一个工作表个数
461 534
 
462
-            echo "\n总条数:".$fcount;
535
+            //echo "\n总条数:".$fcount;
463 536
 
464
-            if($fcount<30000){
465
-                return 49999; //30000条一下不处理
537
+            if($fcount<50000){
538
+                return 49999; //50000条一下不处理
466 539
             }
467 540
 
468
-            //大于6万后重新计算分割数,保证每个表最多一
469
-            if($fcount >= 60000){
470
-                $this->cutNum = ceil($fcount / 10000);
541
+            //大于15万后重新计算分割数,保证每个表最多4
542
+            if($fcount > 130000){
543
+                $this->cutNum = ceil($fcount / 40000);
471 544
             }
472 545
 
473 546
             $averageNum = ceil($fcount / $this->cutNum);