Server_AccessControl/models/Device.php
2020-10-30 10:47:16 +07:00

131 lines
3.6 KiB
PHP

<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "device".
*
* @property int $id
* @property string $name
* @property string $serial
* @property string $ip_address
* @property string $subnet_mask
* @property string $gateway
* @property string $mac_address
* @property int $status
* @property string $type
* @property string $version
* @property int $area_id
* @property int $created_at
* @property int $modified_at
*/
class Device extends \yii\db\ActiveRecord {
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'device';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['name', 'ip_address', 'status', 'type', 'area_id'], 'required'],
[['status', 'area_id', 'created_at', 'modified_at'], 'integer'],
[['name', 'version'], 'string', 'max' => 100],
[['serial'], 'string', 'max' => 50],
[['ip_address', 'subnet_mask', 'gateway', 'mac_address', 'type'], 'string', 'max' => 20],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels() {
return [
'id' => 'ID',
'name' => 'Tên thiết bị',
'serial' => 'Serial',
'ip_address' => 'Địa chỉ IP',
'subnet_mask' => 'Subnet mask',
'gateway' => 'Gateway',
'mac_address' => 'Mac Address',
'status' => 'Trạng thái',
'type' => 'Loại thiết bị',
'version' => 'Phiên bản',
'area_id' => 'Khu vực',
'created_at' => 'Thời gian tạo',
'modified_at' => 'Thời gian sửa',
'door' => 'Quản lý cửa'
];
}
public static $typeArray = [
1 => "C3-100",
2 => "C3-200",
4 => "C3-400"
];
public static $statusArray = [
0 => "Ngưng hoạt động",
1 => "Hoạt động"
];
public function create($data) {
$r = $this->load([
'name' => $data['name'],
'serial' => $data['serial'],
'ip_address' => $data['ip_address'],
'subnet_mask' => $data['subnet_mask'],
'gateway' => $data['gateway'],
'mac_address' => $data['mac_address'],
'status' => $data['status'],
'type' => $data['type'],
'version' => $data['version'],
'area_id' => $data['area_id'],
"created_at" => time(),
"modified_at" => time()
], '');
if ($r) {
try {
$this->save();
return $this->id;
} catch (\Exception $ex) {
return false;
}
}
}
public static function deleteDevice($id) {
$model = self::findOne($id);
if ($model) {
Door::deleteAll(['device_id' => $model->id]);
$model->delete();
common::insertSystemLogs(["action" => "delete", "description" => "Xóa thiết bị: " . $model->name, "type" => "device"]);
}
}
public static function deviceIpLists() {
$lists = self::find()->all();
$results = [];
foreach ($lists as $key => $value) {
$results[] = $value->ip_address;
}
return $results;
}
public static function deviceArray() {
$lists = self::find()->all();
$results = [];
foreach ($lists as $key => $value) {
$results[$value->id] = $value->name;
}
return $results;
}
}