From c99428204af9c08d7abe7c984b592518b9ac0bb2 Mon Sep 17 00:00:00 2001 From: xiaoxian <1094175543@qq.com> Date: Sat, 23 May 2026 15:55:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=9F=E6=88=BF=E6=97=A5=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/rentalController.js | 83 ++++++++++++++++++++++++++++++++- routes/rental.js | 1 + 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/controllers/rentalController.js b/controllers/rentalController.js index 4428692..93306aa 100644 --- a/controllers/rentalController.js +++ b/controllers/rentalController.js @@ -386,11 +386,92 @@ const listRentals = async (req, res) => { } }; +// 获取日历数据(按月查询到期租约) +const getCalendarRentals = async (req, res) => { + try { + const { year, month, apartmentId, tenantName } = req.query; + const currentYear = parseInt(year) || new Date().getFullYear(); + const currentMonth = parseInt(month) || (new Date().getMonth() + 1); + + const startDate = new Date(currentYear, currentMonth - 1, 1); + const endDate = new Date(currentYear, currentMonth, 0, 23, 59, 59); + + // 1. 以房间为主表查询(在租房间) + const roomWhere = { isDeleted: 0, status: 'rented' }; + if (apartmentId) roomWhere.apartmentId = apartmentId; + + const rooms = await Room.findAll({ + where: roomWhere, + include: [{ model: Apartment, where: { isDeleted: 0 } }] + }); + + if (rooms.length === 0) { + return res.status(200).json({ data: [], year: currentYear, month: currentMonth }); + } + + // 2. 查这些房间的所有租约(不限 endDate),按 id 降序 + const roomIds = rooms.map(r => r.id); + const rentals = await Rental.findAll({ + where: { isDeleted: 0, roomId: { [Op.in]: roomIds } }, + order: [['id', 'DESC']] + }); + + // 3. 每个房间只保留最新一条租约 + const latestByRoom = new Map(); + for (const rental of rentals) { + if (!latestByRoom.has(rental.roomId)) { + latestByRoom.set(rental.roomId, rental); + } + } + + // 4. 租客过滤(在去重之后,只按最新租约的 tenantName 匹配) + const latestRentals = Array.from(latestByRoom.values()); + const filteredLatest = tenantName + ? latestRentals.filter(r => r.tenantName === tenantName) + : latestRentals; + + // 5. 从最新租约中过滤出当月到期的 + const monthRentals = filteredLatest.filter(rental => { + return rental.endDate >= startDate && rental.endDate <= endDate; + }); + + // 6. 组装数据 + const roomMap = new Map(rooms.map(r => [r.id, r])); + const data = monthRentals.map(rental => { + const room = roomMap.get(rental.roomId) || {}; + return { + id: rental.id, + tenantName: rental.tenantName, + endDate: formatDate(rental.endDate), + status: rental.status, + rent: rental.rent, + deposit: rental.deposit, + roomId: room.id || null, + roomNumber: room.roomNumber || '', + apartmentName: room.Apartment?.name || '', + subStatus: room.subStatus || 'normal' + }; + }); + + // 7. 按到期日排序 + data.sort((a, b) => a.endDate.localeCompare(b.endDate)); + + res.status(200).json({ + data, + year: currentYear, + month: currentMonth + }); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllRentals, listRentals, getRentalById, createRental, updateRental, - deleteRental + deleteRental, + getCalendarRentals }; diff --git a/routes/rental.js b/routes/rental.js index b3badc9..d64417a 100644 --- a/routes/rental.js +++ b/routes/rental.js @@ -5,6 +5,7 @@ const rentalController = require('../controllers/rentalController'); // 路由 router.get('/', rentalController.getAllRentals); router.get('/list', rentalController.listRentals); +router.get('/calendar', rentalController.getCalendarRentals); router.get('/:id', rentalController.getRentalById); router.post('/', rentalController.createRental); router.put('/:id', rentalController.updateRental);