$value) { if (strpos($key, '.') !== false) { $hasAlias = true; break; } } // 如果使用了表别名,则需要使用查询构造器 if ($hasAlias) { return self::alias('d')->where($where)->count(); } else { return self::where($where)->count(); } } /** * 添加设备 * @param array $data 设备数据 * @return int 新增设备ID */ public static function addDevice($data) { $device = new self(); $device->allowField(true)->save($data); return $device->id; } /** * 更新设备 * @param int $id 设备ID * @param array $data 设备数据 * @return bool 更新结果 */ public static function updateDevice($id, $data) { return self::where('id', $id) ->where('isDeleted', 0) ->update($data); } /** * 删除设备(软删除) * @param int $id 设备ID * @return bool 删除结果 */ public static function deleteDevice($id) { return self::where('id', $id) ->update([ 'isDeleted' => 1, 'deleteTime' => date('Y-m-d H:i:s', time()) ]); } /** * 按设备品牌统计数量 * @return array 统计结果 */ public static function countByBrand() { return self::where('isDeleted', 0) ->group('brand') ->field('brand, count(*) as count') ->select(); } /** * 按设备在线状态统计数量 * @return array 统计结果 */ public static function countByStatus() { return self::where('isDeleted', 0) ->group('alive') ->field('alive, count(*) as count') ->select(); } }