|
@@ -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;
|
|
|
+ }
|
|
|
}
|