where(['device_code = :code'], [':code' => $device_code])->fetch(); } /** * 检查设备是否存在且启用 */ public function isActive($device_code) { $device = $this->where([ 'device_code = :code', 'status = 1' ], [':code' => $device_code])->fetch(); return $device ? $device : false; } /** * 根据设备编码和组别校验 */ public function validate($device_code, $group_name = '') { $conditions = ['device_code = :code', 'status = 1']; $params = [':code' => $device_code]; if (!empty($group_name)) { $conditions[] = 'group_name = :group'; $params[':group'] = $group_name; } return $this->where($conditions, $params)->fetch(); } /** * 获取所有启用设备 */ public function getAllActive() { return $this->where(['status = 1'])->order(['group_name ASC', 'device_code ASC'])->fetchAll(); } /** * 添加设备 */ public function addDevice($data) { return $this->add($data); } /** * 更新设备 */ public function updateDevice($id, $data) { return $this->where(['id = :id'], [':id' => $id])->update($data); } /** * 根据 IP 查找设备 */ public function findByIp($device_ip) { return $this->where(['device_ip = :ip'], [':ip' => $device_ip])->fetch(); } /** * 根据 ID 查找设备 */ public function findById($id) { return $this->where(['id = :id'], [':id' => intval($id)])->fetch(); } /** * 获取全部设备(调试页用,含禁用设备) */ public function getAll() { return $this->order(['group_name ASC', 'device_code ASC'])->fetchAll(); } }