| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getRoomStats = exports.deleteRoom = exports.updateRoom = exports.createRoom = exports.getRoomsByFloor = exports.getRoomById = exports.getAllRooms = void 0;
- const connection_1 = require("../database/connection");
- const getAllRooms = async (req, res) => {
- try {
- const query = `
- SELECT r.*,
- (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id AND status = 'online') as online_devices,
- (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id) as total_devices
- FROM rooms r
- ORDER BY r.floor_id, r.room_number
- `;
- const rooms = await (0, connection_1.executeQuery)(query);
- return res.status(200).json({
- success: true,
- data: rooms
- });
- }
- catch (error) {
- console.error('Error fetching rooms:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to fetch rooms',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.getAllRooms = getAllRooms;
- const getRoomById = async (req, res) => {
- try {
- const roomId = req.params.id;
- const roomQuery = 'SELECT * FROM rooms WHERE id = ?';
- const room = await (0, connection_1.executeQuery)(roomQuery, [roomId]);
- if (!room || (Array.isArray(room) && room.length === 0)) {
- return res.status(404).json({
- success: false,
- message: 'Room not found'
- });
- }
- const devicesQuery = 'SELECT * FROM room_devices WHERE room_id = ?';
- const devices = await (0, connection_1.executeQuery)(devicesQuery, [roomId]);
- return res.status(200).json({
- success: true,
- data: {
- room: Array.isArray(room) ? room[0] : room,
- devices: devices || []
- }
- });
- }
- catch (error) {
- console.error('Error fetching room:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to fetch room',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.getRoomById = getRoomById;
- const getRoomsByFloor = async (req, res) => {
- try {
- const floorId = req.params.floorId;
- const query = `
- SELECT r.*,
- (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id AND status = 'online') as online_devices,
- (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id) as total_devices
- FROM rooms r
- WHERE r.floor_id = ?
- ORDER BY r.room_number
- `;
- const rooms = await (0, connection_1.executeQuery)(query, [floorId]);
- return res.status(200).json({
- success: true,
- data: rooms
- });
- }
- catch (error) {
- console.error('Error fetching rooms by floor:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to fetch rooms by floor',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.getRoomsByFloor = getRoomsByFloor;
- const createRoom = async (req, res) => {
- try {
- const { name, floor_id, room_number, room_type, area, description, status, orientation } = req.body;
- const query = `
- INSERT INTO rooms (name, floor_id, room_number, room_type, area, description, status, orientation)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
- `;
- const result = await (0, connection_1.executeQuery)(query, [
- name,
- floor_id,
- room_number,
- room_type,
- area || null,
- description || null,
- status || 'active',
- orientation || '东'
- ]);
- return res.status(201).json({
- success: true,
- message: 'Room created successfully',
- data: {
- id: result.insertId,
- name,
- floor_id,
- room_number,
- room_type,
- area: area || null,
- description: description || null,
- status: status || 'active',
- orientation: orientation || '东'
- }
- });
- }
- catch (error) {
- console.error('Error creating room:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to create room',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.createRoom = createRoom;
- const updateRoom = async (req, res) => {
- try {
- const roomId = req.params.id;
- const { name, floor_id, room_number, room_type, area, description, status, orientation } = req.body;
- const query = `
- UPDATE rooms
- SET name = ?, floor_id = ?, room_number = ?, room_type = ?, area = ?, description = ?, status = ?, orientation = ?
- WHERE id = ?
- `;
- await (0, connection_1.executeQuery)(query, [
- name,
- floor_id,
- room_number,
- room_type,
- area || null,
- description || null,
- status || 'active',
- orientation || '东',
- roomId
- ]);
- return res.status(200).json({
- success: true,
- message: 'Room updated successfully'
- });
- }
- catch (error) {
- console.error('Error updating room:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to update room',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.updateRoom = updateRoom;
- const deleteRoom = async (req, res) => {
- try {
- const roomId = req.params.id;
- const checkDevicesQuery = 'SELECT COUNT(*) as deviceCount FROM room_devices WHERE room_id = ?';
- const deviceCountResult = await (0, connection_1.executeQuery)(checkDevicesQuery, [roomId]);
- const deviceCount = deviceCountResult[0]?.deviceCount || 0;
- if (deviceCount > 0) {
- return res.status(400).json({
- success: false,
- message: `该房间有 ${deviceCount} 个设备绑定,请先解绑设备后再删除房间`
- });
- }
- const query = 'DELETE FROM rooms WHERE id = ?';
- await (0, connection_1.executeQuery)(query, [roomId]);
- return res.status(200).json({
- success: true,
- message: 'Room deleted successfully'
- });
- }
- catch (error) {
- console.error('Error deleting room:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to delete room',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.deleteRoom = deleteRoom;
- const getRoomStats = async (req, res) => {
- try {
- const totalRoomsQuery = 'SELECT COUNT(*) as total FROM rooms';
- const totalRoomsResult = await (0, connection_1.executeQuery)(totalRoomsQuery);
- const totalRooms = totalRoomsResult[0].total;
- const activeRoomsQuery = 'SELECT COUNT(*) as active FROM rooms WHERE status = "active"';
- const activeRoomsResult = await (0, connection_1.executeQuery)(activeRoomsQuery);
- const activeRooms = activeRoomsResult[0].active;
- const totalDevicesQuery = 'SELECT COUNT(*) as total FROM room_devices';
- const totalDevicesResult = await (0, connection_1.executeQuery)(totalDevicesQuery);
- const totalDevices = totalDevicesResult[0].total;
- const onlineDevicesQuery = 'SELECT COUNT(*) as online FROM room_devices WHERE status = "online"';
- const onlineDevicesResult = await (0, connection_1.executeQuery)(onlineDevicesQuery);
- const onlineDevices = onlineDevicesResult[0].online;
- const floorsQuery = 'SELECT DISTINCT floor_id FROM rooms ORDER BY floor_id';
- const floorsResult = await (0, connection_1.executeQuery)(floorsQuery);
- const totalFloors = floorsResult.length;
- return res.status(200).json({
- success: true,
- data: {
- totalRooms,
- activeRooms,
- totalDevices,
- onlineDevices,
- totalFloors,
- offlineDevices: totalDevices - onlineDevices
- }
- });
- }
- catch (error) {
- console.error('Error fetching room stats:', error);
- return res.status(500).json({
- success: false,
- message: 'Failed to fetch room statistics',
- error: error instanceof Error ? error.message : 'Unknown error'
- });
- }
- };
- exports.getRoomStats = getRoomStats;
- //# sourceMappingURL=roomController.js.map
|