This commit is contained in:
parent
c0aee7e17f
commit
22d61509d1
|
|
@ -234,7 +234,8 @@ const createRental = async (req, res) => {
|
|||
rent: body.rent,
|
||||
deposit: deposit,
|
||||
refundedDeposit: refundedDeposit,
|
||||
status: body.status || 'active'
|
||||
status: body.status || 'active',
|
||||
remark: body.remark
|
||||
});
|
||||
|
||||
const rental = await Rental.create({
|
||||
|
|
@ -246,6 +247,7 @@ const createRental = async (req, res) => {
|
|||
deposit: deposit,
|
||||
refundedDeposit: refundedDeposit,
|
||||
status: body.status || 'active',
|
||||
remark: body.remark,
|
||||
createBy: req.user.id,
|
||||
updateBy: req.user.id
|
||||
});
|
||||
|
|
|
|||
|
|
@ -234,9 +234,157 @@ const getApartmentRoomStatusStatistics = async (req, res) => {
|
|||
}
|
||||
};
|
||||
|
||||
// 空房分布统计(按公寓分组)
|
||||
const getEmptyRoomsByApartment = async (req, res) => {
|
||||
try {
|
||||
// 获取所有公寓(排除已删除的)
|
||||
const apartments = await Apartment.findAll({ where: { isDeleted: 0 } });
|
||||
|
||||
// 获取所有空房(排除已删除的)
|
||||
const emptyRooms = await Room.findAll({
|
||||
where: {
|
||||
status: 'empty',
|
||||
isDeleted: 0
|
||||
}
|
||||
});
|
||||
|
||||
// 构建按公寓分组的空房数据
|
||||
const emptyRoomsByApartment = apartments.map(apartment => {
|
||||
const apartmentEmptyRooms = emptyRooms
|
||||
.filter(room => room.apartmentId === apartment.id)
|
||||
.map(room => ({
|
||||
id: room.id,
|
||||
roomNumber: room.roomNumber,
|
||||
type: room.type,
|
||||
area: room.area
|
||||
}));
|
||||
|
||||
return {
|
||||
apartmentId: apartment.id,
|
||||
apartmentName: apartment.name,
|
||||
emptyRooms: apartmentEmptyRooms
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json(emptyRoomsByApartment);
|
||||
} catch (error) {
|
||||
console.error('获取空房分布数据时出错:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// 在租分布统计(按公寓分组)
|
||||
const getRentedRoomsByApartment = async (req, res) => {
|
||||
try {
|
||||
// 获取所有公寓(排除已删除的)
|
||||
const apartments = await Apartment.findAll({ where: { isDeleted: 0 } });
|
||||
|
||||
// 获取所有在租房(排除已删除的)
|
||||
const rentedRooms = await Room.findAll({
|
||||
where: {
|
||||
status: 'rented',
|
||||
isDeleted: 0
|
||||
}
|
||||
});
|
||||
|
||||
// 构建按公寓分组的在租数据
|
||||
const rentedRoomsByApartment = apartments.map(apartment => {
|
||||
const apartmentRentedRooms = rentedRooms
|
||||
.filter(room => room.apartmentId === apartment.id)
|
||||
.map(room => ({
|
||||
id: room.id,
|
||||
roomNumber: room.roomNumber,
|
||||
type: room.type,
|
||||
area: room.area
|
||||
}));
|
||||
|
||||
return {
|
||||
apartmentId: apartment.id,
|
||||
apartmentName: apartment.name,
|
||||
rentedRooms: apartmentRentedRooms
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json(rentedRoomsByApartment);
|
||||
} catch (error) {
|
||||
console.error('获取在租分布数据时出错:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// 租客在租统计
|
||||
const getTenantRentalStats = async (req, res) => {
|
||||
try {
|
||||
// 获取所有在租的租赁记录(排除已删除的)
|
||||
const rentals = await Rental.findAll({
|
||||
where: {
|
||||
status: 'active',
|
||||
isDeleted: 0
|
||||
},
|
||||
include: [{
|
||||
model: Room,
|
||||
where: { isDeleted: 0 }
|
||||
}]
|
||||
});
|
||||
|
||||
// 获取所有公寓(排除已删除的)
|
||||
const apartments = await Apartment.findAll({ where: { isDeleted: 0 } });
|
||||
|
||||
// 按租客分组
|
||||
const tenantMap = new Map();
|
||||
|
||||
rentals.forEach(rental => {
|
||||
const tenantName = rental.tenantName;
|
||||
const room = rental.Room;
|
||||
|
||||
if (!tenantMap.has(tenantName)) {
|
||||
tenantMap.set(tenantName, {
|
||||
tenantName,
|
||||
count: 0,
|
||||
apartments: []
|
||||
});
|
||||
}
|
||||
|
||||
const tenantData = tenantMap.get(tenantName);
|
||||
tenantData.count++;
|
||||
|
||||
// 查找房间所属的公寓
|
||||
const apartment = apartments.find(apt => apt.id === room.apartmentId);
|
||||
if (apartment) {
|
||||
let apartmentData = tenantData.apartments.find(apt => apt.apartmentId === apartment.id);
|
||||
if (!apartmentData) {
|
||||
apartmentData = {
|
||||
apartmentId: apartment.id,
|
||||
apartmentName: apartment.name,
|
||||
rooms: []
|
||||
};
|
||||
tenantData.apartments.push(apartmentData);
|
||||
}
|
||||
apartmentData.rooms.push({
|
||||
id: room.id,
|
||||
roomNumber: room.roomNumber,
|
||||
type: room.type,
|
||||
area: room.area
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 转换为数组
|
||||
const tenantRentalStats = Array.from(tenantMap.values());
|
||||
|
||||
res.status(200).json(tenantRentalStats);
|
||||
} catch (error) {
|
||||
console.error('获取租客在租统计数据时出错:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getRentStatistics,
|
||||
getRoomStatusStatistics,
|
||||
getDashboardStatistics,
|
||||
getApartmentRoomStatusStatistics
|
||||
getApartmentRoomStatusStatistics,
|
||||
getEmptyRoomsByApartment,
|
||||
getRentedRoomsByApartment,
|
||||
getTenantRentalStats
|
||||
};
|
||||
|
|
@ -7,5 +7,8 @@ router.get('/rent', statisticsController.getRentStatistics);
|
|||
router.get('/room-status', statisticsController.getRoomStatusStatistics);
|
||||
router.get('/dashboard', statisticsController.getDashboardStatistics);
|
||||
router.get('/apartment-room-status', statisticsController.getApartmentRoomStatusStatistics);
|
||||
router.get('/empty-rooms-by-apartment', statisticsController.getEmptyRoomsByApartment);
|
||||
router.get('/rented-rooms-by-apartment', statisticsController.getRentedRoomsByApartment);
|
||||
router.get('/tenant-rental-stats', statisticsController.getTenantRentalStats);
|
||||
|
||||
module.exports = router;
|
||||
Loading…
Reference in New Issue