const { Room, Apartment, Rental } = require('../models'); const { Op } = require('sequelize'); // 格式化时间(考虑时区,转换为北京时间) const formatDate = (date) => { if (!date) return null; // 创建一个新的Date对象,加上8小时的时区偏移 const beijingDate = new Date(date.getTime() + 8 * 60 * 60 * 1000); return beijingDate.toISOString().split('T')[0]; }; // 格式化房间数据 const formatRoomData = (room) => { const formattedRoom = { ...room.toJSON(), createTime: formatDate(room.createTime) }; // 格式化关联数据 if (formattedRoom.Apartment) { formattedRoom.Apartment = { ...formattedRoom.Apartment, createTime: formatDate(formattedRoom.Apartment.createTime) }; } // 格式化租房信息 if (formattedRoom.Rentals && formattedRoom.Rentals.length > 0) { formattedRoom.Rentals = formattedRoom.Rentals.map(rental => { const formattedRental = { ...rental, startDate: formatDate(rental.startDate), endDate: formatDate(rental.endDate), createTime: formatDate(rental.createTime) }; // 格式化租客信息 if (formattedRental.Tenant) { formattedRental.Tenant = { ...formattedRental.Tenant, createTime: formatDate(formattedRental.Tenant.createTime) }; } return formattedRental; }); } return formattedRoom; }; // 检查并更新租房状态 const checkAndUpdateRentalStatus = async () => { try { // 获取当前日期 const currentDate = new Date(); // 计算10天后的日期 const tenDaysLater = new Date(); tenDaysLater.setDate(currentDate.getDate() + 10); // 查找所有活跃的租房记录 const rentals = await Rental.findAll({ where: { status: 'active' } }); // 检查每个租房记录的状态 for (const rental of rentals) { const endDate = new Date(rental.endDate); // 检查是否已到期 if (endDate < currentDate) { // 更新租房状态为已到期 await rental.update({ status: 'expired' }); // 更新房间状态为空房 const room = await Room.findByPk(rental.roomId); if (room) { await room.update({ status: 'empty' }); } } else if (endDate <= tenDaysLater) { // 更新房间状态为即将到期 const room = await Room.findByPk(rental.roomId); if (room && room.status === 'rented') { await room.update({ status: 'soon_expire' }); } } } console.log('租房状态检查和更新完成'); } catch (error) { console.error('检查和更新租房状态时出错:', error); } }; // 获取所有房间(支持搜索和分页) const getAllRooms = async (req, res) => { try { // 先检查并更新租房状态 await checkAndUpdateRentalStatus(); const { apartmentId, roomNumber, status, page = 1, pageSize = 10 } = req.query; // 构建查询条件 const where = {}; if (apartmentId) { where.apartmentId = apartmentId; } if (roomNumber) { where.roomNumber = { [Op.like]: `%${roomNumber}%` }; } if (status) { where.status = status; } // 计算偏移量 const offset = (page - 1) * pageSize; // 查询房间数据 const { count, rows } = await Room.findAndCountAll({ where, include: [ Apartment ], limit: parseInt(pageSize), offset: parseInt(offset) }); // 为每个房间获取非过期的租房信息 const formattedRooms = await Promise.all(rows.map(async (room) => { const formattedRoom = formatRoomData(room); // 查询非过期的租房信息 const rentals = await Rental.findAll({ where: { roomId: room.id, status: { [Op.ne]: 'expired' } }, include: ['Tenant'] }); // 格式化租房信息 if (rentals.length > 0) { formattedRoom.Rentals = rentals.map(rental => { const formattedRental = { ...rental.toJSON(), startDate: formatDate(rental.startDate), endDate: formatDate(rental.endDate), createTime: formatDate(rental.createTime) }; // 格式化租客信息 if (formattedRental.Tenant) { formattedRental.Tenant = { ...formattedRental.Tenant, createTime: formatDate(formattedRental.Tenant.createTime) }; } return formattedRental; }); } else { formattedRoom.Rentals = []; } return formattedRoom; })); // 返回结果 res.status(200).json({ data: formattedRooms, total: count, page: parseInt(page), pageSize: parseInt(pageSize) }); } catch (error) { res.status(500).json({ error: error.message }); } }; // 获取单个房间 const getRoomById = async (req, res) => { try { // 先检查并更新租房状态 await checkAndUpdateRentalStatus(); const { id } = req.params; const room = await Room.findByPk(id, { include: [Apartment] }); if (!room) { return res.status(404).json({ error: '房间不存在' }); } // 格式化房间数据 const formattedRoom = formatRoomData(room); // 查询非过期的租房信息 const rentals = await Rental.findAll({ where: { roomId: room.id, status: { [Op.ne]: 'expired' } }, include: ['Tenant'] }); // 格式化租房信息 if (rentals.length > 0) { formattedRoom.Rentals = rentals.map(rental => { const formattedRental = { ...rental.toJSON(), startDate: formatDate(rental.startDate), endDate: formatDate(rental.endDate), createTime: formatDate(rental.createTime) }; // 格式化租客信息 if (formattedRental.Tenant) { formattedRental.Tenant = { ...formattedRental.Tenant, createTime: formatDate(formattedRental.Tenant.createTime) }; } return formattedRental; }); } else { formattedRoom.Rentals = []; } res.status(200).json(formattedRoom); } catch (error) { res.status(500).json({ error: error.message }); } }; // 创建房间 const createRoom = async (req, res) => { try { const { apartmentId, roomNumber, area, price, status } = req.body; const room = await Room.create({ apartmentId, roomNumber, area, price, status }); res.status(201).json(room); } catch (error) { res.status(500).json({ error: error.message }); } }; // 更新房间 const updateRoom = async (req, res) => { try { const { id } = req.params; const { apartmentId, roomNumber, area, price, status } = req.body; const room = await Room.findByPk(id); if (!room) { return res.status(404).json({ error: '房间不存在' }); } await room.update({ apartmentId, roomNumber, area, price, status }); res.status(200).json(room); } catch (error) { res.status(500).json({ error: error.message }); } }; // 删除房间 const deleteRoom = async (req, res) => { try { const { id } = req.params; const room = await Room.findByPk(id); if (!room) { return res.status(404).json({ error: '房间不存在' }); } await room.destroy(); res.status(200).json({ message: '房间删除成功' }); } catch (error) { res.status(500).json({ error: error.message }); } }; module.exports = { getAllRooms, getRoomById, createRoom, updateRoom, deleteRoom };