cqpCow 1 vuosi sitten
vanhempi
commit
8aeaad1018
2 muutettua tiedostoa jossa 42 lisäystä ja 2 poistoa
  1. 6 2
      app/Jobs/ManDeviceJob.php
  2. 36 0
      app/Model/DeviceData.php

+ 6 - 2
app/Jobs/ManDeviceJob.php

@@ -48,6 +48,10 @@ class ManDeviceJob implements ShouldQueue
             $deviceName = $this->data['data']['deviceName'];
             $ip = self::getIP();
             $data_type = 3;
+            $date = date('Ymd',time());
+
+            //工序-----------------------------
+            $model = new DeviceData(['channel' => $date]);
             if($this->data['type'] == "dataPoint"){
                 $insert = [];
                 foreach ($this->data['data']['dataPoints'] as $value){
@@ -68,7 +72,7 @@ class ManDeviceJob implements ShouldQueue
                         'crt_time' => time()
                     ];
                 }
-                DeviceData::insert($insert);
+                $model->insert($insert);
             }elseif ($this->data['type'] == "position"){
                 $dev_eui = $deviceId;
                 if(in_array($dev_eui,$device)) {
@@ -81,7 +85,7 @@ class ManDeviceJob implements ShouldQueue
                         'data_type' => $data_type,
                         'crt_time' => time()
                     ];
-                    DeviceData::insert($insert);
+                    $model->insert($insert);
                 }
             }
 

+ 36 - 0
app/Model/DeviceData.php

@@ -3,6 +3,8 @@
 namespace App\Model;
 
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
 
 class DeviceData extends Model
 {
@@ -10,5 +12,39 @@ class DeviceData extends Model
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+    protected $guarded = [];
 
+    public function __construct(array $attributes = [])
+    {
+        parent::__construct($attributes);
+        if (isset($attributes['channel']) && ! empty($attributes['channel'])) {
+            $channel = $attributes['channel'];
+            $year_month = substr($channel,0,7);
+            $year_month = str_replace('-','_',$year_month);
+            $this->setTableById($year_month);
+        }
+    }
+
+    public function setTableById($channel)
+    {
+        if (! empty($channel)) {
+            $tb = $this->table.'_' . (string)$channel;
+            $this->createTable($tb);
+            $this->setTable($tb);
+        }
+
+    }
+
+    //创建表
+    private function createTable($table){
+        if(! empty($table) && ! Schema::hasTable($table)){
+            //执行建表语句
+            DB::statement('create table '. $table .' like device_data_for_create');
+        }
+    }
+
+    public function is_table_isset(){
+        if(Schema::hasTable($this->table)) return true;
+        return false;
+    }
 }