update Device full CRUD
This commit is contained in:
@@ -11,51 +11,110 @@ use Yii;
|
||||
* @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 int $type
|
||||
* @property string $type
|
||||
* @property string $version
|
||||
* @property int $area_id
|
||||
* @property int $created_at
|
||||
* @property int $modified_at
|
||||
*/
|
||||
class Device extends \yii\db\ActiveRecord
|
||||
{
|
||||
class Device extends \yii\db\ActiveRecord {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
public static function tableName() {
|
||||
return 'device';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
public function rules() {
|
||||
return [
|
||||
[['name', 'ip_address', 'status', 'type', 'area_id'], 'required'],
|
||||
[['status', 'type', 'area_id', 'created_at', 'modified_at'], 'integer'],
|
||||
[['name'], 'string', 'max' => 100],
|
||||
[['serial'], 'string', 'max' => 50],
|
||||
[['ip_address'], 'string', 'max' => 20],
|
||||
[['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()
|
||||
{
|
||||
public function attributeLabels() {
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'name' => 'Name',
|
||||
'name' => 'Tên thiết bị',
|
||||
'serial' => 'Serial',
|
||||
'ip_address' => 'Ip Address',
|
||||
'status' => 'Status',
|
||||
'type' => 'Type',
|
||||
'area_id' => 'Area ID',
|
||||
'created_at' => 'Created At',
|
||||
'modified_at' => 'Modified At',
|
||||
'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',
|
||||
];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,24 +10,22 @@ use app\models\Device;
|
||||
/**
|
||||
* DeviceSearch represents the model behind the search form of `app\models\Device`.
|
||||
*/
|
||||
class DeviceSearch extends Device
|
||||
{
|
||||
class DeviceSearch extends Device {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
public function rules() {
|
||||
return [
|
||||
[['id', 'status', 'type', 'area_id', 'created_at', 'modified_at'], 'integer'],
|
||||
[['name', 'serial', 'ip_address'], 'safe'],
|
||||
[['id', 'status', 'type', 'area_id'], 'integer'],
|
||||
[['name', 'serial', 'ip_address'], 'safe'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function scenarios()
|
||||
{
|
||||
public function scenarios() {
|
||||
// bypass scenarios() implementation in the parent class
|
||||
return Model::scenarios();
|
||||
}
|
||||
@@ -39,8 +37,7 @@ class DeviceSearch extends Device
|
||||
*
|
||||
* @return ActiveDataProvider
|
||||
*/
|
||||
public function search($params)
|
||||
{
|
||||
public function search($params) {
|
||||
$query = Device::find();
|
||||
|
||||
// add conditions that should always apply here
|
||||
@@ -68,9 +65,10 @@ class DeviceSearch extends Device
|
||||
]);
|
||||
|
||||
$query->andFilterWhere(['like', 'name', $this->name])
|
||||
->andFilterWhere(['like', 'serial', $this->serial])
|
||||
->andFilterWhere(['like', 'ip_address', $this->ip_address]);
|
||||
->andFilterWhere(['like', 'serial', $this->serial])
|
||||
->andFilterWhere(['like', 'ip_address', $this->ip_address]);
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
57
models/Door.php
Normal file
57
models/Door.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* This is the model class for table "door".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $device_id
|
||||
* @property string $name
|
||||
* @property int $code
|
||||
* @property int $created_at
|
||||
* @property int $modified_at
|
||||
*/
|
||||
class Door extends \yii\db\ActiveRecord {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName() {
|
||||
return 'door';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules() {
|
||||
return [
|
||||
[['device_id', 'name', 'code'], 'required'],
|
||||
[['device_id', 'code', 'created_at', 'modified_at'], 'integer'],
|
||||
[['name'], 'string', 'max' => 100],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels() {
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'device_id' => 'Device ID',
|
||||
'name' => 'Name',
|
||||
'code' => 'Code',
|
||||
'created_at' => 'Created At',
|
||||
'modified_at' => 'Modified At',
|
||||
];
|
||||
}
|
||||
|
||||
public function multiCreate($datas) {
|
||||
$field = ['device_id', 'name', 'code', 'created_at', 'modified_at'];
|
||||
static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -183,4 +183,14 @@ class common extends \yii\db\ActiveRecord {
|
||||
return $model->create($data);
|
||||
}
|
||||
|
||||
public static function requestToCardService($path, $data) {
|
||||
return file_get_contents("http://192.168.2.119:2001" . $path, false, stream_context_create([
|
||||
'http' => [
|
||||
'header' => "Content-Type: application/json",
|
||||
'method' => "POST",
|
||||
'content' => json_encode($data)
|
||||
]
|
||||
]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user