roomController.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getRoomStats = exports.deleteRoom = exports.updateRoom = exports.createRoom = exports.getRoomsByFloor = exports.getRoomById = exports.getAllRooms = void 0;
  4. const connection_1 = require("../database/connection");
  5. const getAllRooms = async (req, res) => {
  6. try {
  7. const query = `
  8. SELECT r.*,
  9. (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id AND status = 'online') as online_devices,
  10. (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id) as total_devices
  11. FROM rooms r
  12. ORDER BY r.floor_id, r.room_number
  13. `;
  14. const rooms = await (0, connection_1.executeQuery)(query);
  15. return res.status(200).json({
  16. success: true,
  17. data: rooms
  18. });
  19. }
  20. catch (error) {
  21. console.error('Error fetching rooms:', error);
  22. return res.status(500).json({
  23. success: false,
  24. message: 'Failed to fetch rooms',
  25. error: error instanceof Error ? error.message : 'Unknown error'
  26. });
  27. }
  28. };
  29. exports.getAllRooms = getAllRooms;
  30. const getRoomById = async (req, res) => {
  31. try {
  32. const roomId = req.params.id;
  33. const roomQuery = 'SELECT * FROM rooms WHERE id = ?';
  34. const room = await (0, connection_1.executeQuery)(roomQuery, [roomId]);
  35. if (!room || (Array.isArray(room) && room.length === 0)) {
  36. return res.status(404).json({
  37. success: false,
  38. message: 'Room not found'
  39. });
  40. }
  41. const devicesQuery = 'SELECT * FROM room_devices WHERE room_id = ?';
  42. const devices = await (0, connection_1.executeQuery)(devicesQuery, [roomId]);
  43. return res.status(200).json({
  44. success: true,
  45. data: {
  46. room: Array.isArray(room) ? room[0] : room,
  47. devices: devices || []
  48. }
  49. });
  50. }
  51. catch (error) {
  52. console.error('Error fetching room:', error);
  53. return res.status(500).json({
  54. success: false,
  55. message: 'Failed to fetch room',
  56. error: error instanceof Error ? error.message : 'Unknown error'
  57. });
  58. }
  59. };
  60. exports.getRoomById = getRoomById;
  61. const getRoomsByFloor = async (req, res) => {
  62. try {
  63. const floorId = req.params.floorId;
  64. const query = `
  65. SELECT r.*,
  66. (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id AND status = 'online') as online_devices,
  67. (SELECT COUNT(*) FROM room_devices WHERE room_id = r.id) as total_devices
  68. FROM rooms r
  69. WHERE r.floor_id = ?
  70. ORDER BY r.room_number
  71. `;
  72. const rooms = await (0, connection_1.executeQuery)(query, [floorId]);
  73. return res.status(200).json({
  74. success: true,
  75. data: rooms
  76. });
  77. }
  78. catch (error) {
  79. console.error('Error fetching rooms by floor:', error);
  80. return res.status(500).json({
  81. success: false,
  82. message: 'Failed to fetch rooms by floor',
  83. error: error instanceof Error ? error.message : 'Unknown error'
  84. });
  85. }
  86. };
  87. exports.getRoomsByFloor = getRoomsByFloor;
  88. const createRoom = async (req, res) => {
  89. try {
  90. const { name, floor_id, room_number, room_type, area, description, status, orientation } = req.body;
  91. const query = `
  92. INSERT INTO rooms (name, floor_id, room_number, room_type, area, description, status, orientation)
  93. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  94. `;
  95. const result = await (0, connection_1.executeQuery)(query, [
  96. name,
  97. floor_id,
  98. room_number,
  99. room_type,
  100. area || null,
  101. description || null,
  102. status || 'active',
  103. orientation || '东'
  104. ]);
  105. return res.status(201).json({
  106. success: true,
  107. message: 'Room created successfully',
  108. data: {
  109. id: result.insertId,
  110. name,
  111. floor_id,
  112. room_number,
  113. room_type,
  114. area: area || null,
  115. description: description || null,
  116. status: status || 'active',
  117. orientation: orientation || '东'
  118. }
  119. });
  120. }
  121. catch (error) {
  122. console.error('Error creating room:', error);
  123. return res.status(500).json({
  124. success: false,
  125. message: 'Failed to create room',
  126. error: error instanceof Error ? error.message : 'Unknown error'
  127. });
  128. }
  129. };
  130. exports.createRoom = createRoom;
  131. const updateRoom = async (req, res) => {
  132. try {
  133. const roomId = req.params.id;
  134. const { name, floor_id, room_number, room_type, area, description, status, orientation } = req.body;
  135. const query = `
  136. UPDATE rooms
  137. SET name = ?, floor_id = ?, room_number = ?, room_type = ?, area = ?, description = ?, status = ?, orientation = ?
  138. WHERE id = ?
  139. `;
  140. await (0, connection_1.executeQuery)(query, [
  141. name,
  142. floor_id,
  143. room_number,
  144. room_type,
  145. area || null,
  146. description || null,
  147. status || 'active',
  148. orientation || '东',
  149. roomId
  150. ]);
  151. return res.status(200).json({
  152. success: true,
  153. message: 'Room updated successfully'
  154. });
  155. }
  156. catch (error) {
  157. console.error('Error updating room:', error);
  158. return res.status(500).json({
  159. success: false,
  160. message: 'Failed to update room',
  161. error: error instanceof Error ? error.message : 'Unknown error'
  162. });
  163. }
  164. };
  165. exports.updateRoom = updateRoom;
  166. const deleteRoom = async (req, res) => {
  167. try {
  168. const roomId = req.params.id;
  169. const checkDevicesQuery = 'SELECT COUNT(*) as deviceCount FROM room_devices WHERE room_id = ?';
  170. const deviceCountResult = await (0, connection_1.executeQuery)(checkDevicesQuery, [roomId]);
  171. const deviceCount = deviceCountResult[0]?.deviceCount || 0;
  172. if (deviceCount > 0) {
  173. return res.status(400).json({
  174. success: false,
  175. message: `该房间有 ${deviceCount} 个设备绑定,请先解绑设备后再删除房间`
  176. });
  177. }
  178. const query = 'DELETE FROM rooms WHERE id = ?';
  179. await (0, connection_1.executeQuery)(query, [roomId]);
  180. return res.status(200).json({
  181. success: true,
  182. message: 'Room deleted successfully'
  183. });
  184. }
  185. catch (error) {
  186. console.error('Error deleting room:', error);
  187. return res.status(500).json({
  188. success: false,
  189. message: 'Failed to delete room',
  190. error: error instanceof Error ? error.message : 'Unknown error'
  191. });
  192. }
  193. };
  194. exports.deleteRoom = deleteRoom;
  195. const getRoomStats = async (req, res) => {
  196. try {
  197. const totalRoomsQuery = 'SELECT COUNT(*) as total FROM rooms';
  198. const totalRoomsResult = await (0, connection_1.executeQuery)(totalRoomsQuery);
  199. const totalRooms = totalRoomsResult[0].total;
  200. const activeRoomsQuery = 'SELECT COUNT(*) as active FROM rooms WHERE status = "active"';
  201. const activeRoomsResult = await (0, connection_1.executeQuery)(activeRoomsQuery);
  202. const activeRooms = activeRoomsResult[0].active;
  203. const totalDevicesQuery = 'SELECT COUNT(*) as total FROM room_devices';
  204. const totalDevicesResult = await (0, connection_1.executeQuery)(totalDevicesQuery);
  205. const totalDevices = totalDevicesResult[0].total;
  206. const onlineDevicesQuery = 'SELECT COUNT(*) as online FROM room_devices WHERE status = "online"';
  207. const onlineDevicesResult = await (0, connection_1.executeQuery)(onlineDevicesQuery);
  208. const onlineDevices = onlineDevicesResult[0].online;
  209. const floorsQuery = 'SELECT DISTINCT floor_id FROM rooms ORDER BY floor_id';
  210. const floorsResult = await (0, connection_1.executeQuery)(floorsQuery);
  211. const totalFloors = floorsResult.length;
  212. return res.status(200).json({
  213. success: true,
  214. data: {
  215. totalRooms,
  216. activeRooms,
  217. totalDevices,
  218. onlineDevices,
  219. totalFloors,
  220. offlineDevices: totalDevices - onlineDevices
  221. }
  222. });
  223. }
  224. catch (error) {
  225. console.error('Error fetching room stats:', error);
  226. return res.status(500).json({
  227. success: false,
  228. message: 'Failed to fetch room statistics',
  229. error: error instanceof Error ? error.message : 'Unknown error'
  230. });
  231. }
  232. };
  233. exports.getRoomStats = getRoomStats;
  234. //# sourceMappingURL=roomController.js.map