租房日历
This commit is contained in:
parent
7f90cf581a
commit
c99428204a
|
|
@ -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 = {
|
module.exports = {
|
||||||
getAllRentals,
|
getAllRentals,
|
||||||
listRentals,
|
listRentals,
|
||||||
getRentalById,
|
getRentalById,
|
||||||
createRental,
|
createRental,
|
||||||
updateRental,
|
updateRental,
|
||||||
deleteRental
|
deleteRental,
|
||||||
|
getCalendarRentals
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ const rentalController = require('../controllers/rentalController');
|
||||||
// 路由
|
// 路由
|
||||||
router.get('/', rentalController.getAllRentals);
|
router.get('/', rentalController.getAllRentals);
|
||||||
router.get('/list', rentalController.listRentals);
|
router.get('/list', rentalController.listRentals);
|
||||||
|
router.get('/calendar', rentalController.getCalendarRentals);
|
||||||
router.get('/:id', rentalController.getRentalById);
|
router.get('/:id', rentalController.getRentalById);
|
||||||
router.post('/', rentalController.createRental);
|
router.post('/', rentalController.createRental);
|
||||||
router.put('/:id', rentalController.updateRental);
|
router.put('/:id', rentalController.updateRental);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue