Browse Source

内部设备的导入操作

ChenWuJie 4 years ago
parent
commit
5feabe2bc6
1 changed files with 39 additions and 22 deletions
  1. 39 22
      app/Imports/InnerDeviceImport.php

+ 39 - 22
app/Imports/InnerDeviceImport.php

xqd xqd xqd xqd
@@ -5,6 +5,7 @@ namespace App\Imports;
 use App\Exceptions\ImportError;
 use App\Models\DeviceName;
 use App\Models\InnerDevice;
+use App\Models\InnerDeviceNamesModel;
 use App\Models\Option;
 use App\Models\Project;
 use App\Models\ProjectZone;
@@ -33,38 +34,59 @@ class InnerDeviceImport implements ToCollection
         $this->model = new InnerDevice();
     }
 
-    public function formatDate($date)
-    {
-        if(empty($date)) return '';
-        return Date::excelToDateTimeObject($date)->format('Y-m-d');
+    function excelTime($date, $time = false) {
+        if(function_exists('GregorianToJD')){
+            if (is_numeric( $date )) {
+                $jd = GregorianToJD( 1, 1, 1970 );
+                $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
+                $date = explode( '/', $gregorian );
+                $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
+                    ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
+                    ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
+                    . ($time ? " 00:00:00" : '');
+                return $date_str;
+            }
+        }else{
+            $date=$date>25568?$date+1:25569;
+            /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
+            $ofs=(70 * 365 + 17+2) * 86400;
+            $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
+        }
+//        dd($date);
+        return $date;
     }
 
     public function collection(Collection $rows)
     {
         if(count($rows) <= 1) {
             /** @noinspection PhpUnhandledExceptionInspection */
-//            throw new ImportError('表格为空');
             return false;
         };
         foreach ($rows as $key => $row) {
             if($key == 0) continue;
-            if(!isset($row[0])) break;
-            $device_name = DeviceName::firstOrCreate([
-                'name' => $row[1]
-            ]);
-            $spec = Spec::firstOrCreate([
-                'device_name_id' => $device_name->id,
-                'name' => $row[2]
-            ]);
-
+            if(empty($row[0])&&empty($row[1])&&empty($row[2]))
+            {
+                break;
+            }
+            //设备名称
+            if ($row[1])
+            {
+                $device_name = InnerDeviceNamesModel::firstOrCreate([
+                    'name' => $row[1],
+                    'sort' => 1,
+                    'status' => 1
+                ]);
+            }
+            $spec_name = $row[2];
             $project = null;
             $work_point = null;
+            //项目
             if($row[7]) {
                 $project = Project::firstOrCreate([
                     'name' => $row[7]
                 ]);
             }
-
+            //工点
             if($project && $row[8]) {
                 $work_point = WorkPoint::firstOrCreate([
                     'project_id' => $project->id,
@@ -78,12 +100,11 @@ class InnerDeviceImport implements ToCollection
                 ['name', '=', $row[9]]
             ])->first();
 
-
             $data = [
                 'number' => $row[0],
                 'device_name_id' => $device_name ? $device_name->id : '',
-                'spec_id' => $spec ? $spec->id : '',
-                'produce_date' => $this->formatDate($row[3]),
+                'spec_name' => $spec_name ? $spec_name : '',
+                'produce_date' => $this->excelTime($row[3]),
                 'buy_origin' => $row[4],
                 'manufacturer' => $row[5],
                 'shape' => $row[6],
@@ -92,10 +113,6 @@ class InnerDeviceImport implements ToCollection
                 'status' => $status ? $status->id : ''
             ];
             $this->model->create($data);
-//            $num = (int)$row[7];
-//            for($i = 0; $i < $num; ++$i) {
-//                $this->model->create($data);
-//            }
         }
         return true;
     }