server/app/Model/Device.php

80 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
/**
* @property int $device_id
* @property int $org_id
* @property string $device_sn
* @property int $status
* @property int $del_flag
* @property string $create_time
* @property string $update_time
*/
class Device extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'device';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['device_id' => 'integer', 'org_id' => 'integer', 'status' => 'integer', 'del_flag' => 'integer'];
public static function pages(array $param)
{
$model = self::from("device as d")
->leftJoin("group as g", "g.group_id", "=", "d.group_id")
->leftJoin("app as a", "a.app_id", "=", "d.app_id")
->where("d.del_flag", 0);
foreach (['device_sn'] as $k) {
if (isset($param[$k]) && $param[$k] != "") {
$model = $model->where("d." . $k, "like", "%{$param[$k]}%");
}
}
foreach (['org_id', 'status', 'group_id', 'app_id'] as $k) {
if (isset($param[$k]) && $param[$k] != "") {
$model = $model->where("d." . $k, $param[$k]);
}
}
return $model->orderByDesc("d.device_id")
->paginate((int)$param['limit'], ["d.device_id", "d.device_sn", "d.status", "d.create_time", "g.group_name", "d.group_id", "d.online", "d.app_id", "a.app_name"], 'page', (int)$param['page']);
}
public static function add(array $data)
{
$data['create_time'] = date("Y-m-d H:i:s");
$data['del_flag'] = 0;
return self::insert($data);
}
public static function edit(array $data)
{
$data['update_time'] = date("Y-m-d H:i:s");
return self::where("device_id", $data['device_id'])
->where("org_id", $data['org_id'])
->where('del_flag', 0)
->update($data);
}
public static function del(array $account, $ids)
{
return self::whereIn('device_id', explode(",", $ids))
->where("org_id", $account['belong_id'])
->where('del_flag', 0)
->update([
'update_time' => date('Y-m-d H:i:s'),
'del_flag' => 1
]);
}
}