From 2e8bddab4e01e8e4b5a632e7519f53dab199a5eb Mon Sep 17 00:00:00 2001 From: wangxiaoxian <1094175543@qq.com> Date: Fri, 6 Mar 2026 00:05:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=9F=E6=88=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/rentalController.js | 46 ++++++++++++++++++-- controllers/waterBillController.js | 70 ++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 28 deletions(-) diff --git a/controllers/rentalController.js b/controllers/rentalController.js index f9ff68e..6507ac8 100644 --- a/controllers/rentalController.js +++ b/controllers/rentalController.js @@ -80,7 +80,18 @@ const getAllRentals = async (req, res) => { // 先检查并更新租房状态 await checkAndUpdateRentalStatus(); - const { roomId, tenantName, status, page = 1, pageSize = 10 } = req.query; + const { + apartmentId, + roomId, + tenantName, + status, + startDateFrom, + startDateTo, + endDateFrom, + endDateTo, + page = 1, + pageSize = 10 + } = req.query; // 构建查询条件 const where = { isDeleted: 0 }; @@ -93,12 +104,21 @@ const getAllRentals = async (req, res) => { if (tenantName) { where.tenantName = { [Op.like]: `%${tenantName}%` }; } + if (startDateFrom && startDateTo) { + where.startDate = { [Op.between]: [new Date(startDateFrom), new Date(startDateTo)] }; + } + if (endDateFrom && endDateTo) { + where.endDate = { [Op.between]: [new Date(endDateFrom), new Date(endDateTo)] }; + } // 构建包含关系 const include = [ { model: Room, - where: { isDeleted: 0 }, + where: { + isDeleted: 0, + ...(apartmentId ? { apartmentId } : {}) + }, include: [ { model: Apartment, @@ -291,7 +311,16 @@ const listRentals = async (req, res) => { // 先检查并更新租房状态 await checkAndUpdateRentalStatus(); - const { roomId, tenantName, status } = req.query; + const { + apartmentId, + roomId, + tenantName, + status, + startDateFrom, + startDateTo, + endDateFrom, + endDateTo + } = req.query; // 构建查询条件 const where = { isDeleted: 0 }; @@ -304,12 +333,21 @@ const listRentals = async (req, res) => { if (tenantName) { where.tenantName = { [Op.like]: `%${tenantName}%` }; } + if (startDateFrom && startDateTo) { + where.startDate = { [Op.between]: [new Date(startDateFrom), new Date(startDateTo)] }; + } + if (endDateFrom && endDateTo) { + where.endDate = { [Op.between]: [new Date(endDateFrom), new Date(endDateTo)] }; + } // 构建包含关系 const include = [ { model: Room, - where: { isDeleted: 0 }, + where: { + isDeleted: 0, + ...(apartmentId ? { apartmentId } : {}) + }, include: [ { model: Apartment, diff --git a/controllers/waterBillController.js b/controllers/waterBillController.js index 32eb9bb..61c5a15 100644 --- a/controllers/waterBillController.js +++ b/controllers/waterBillController.js @@ -34,7 +34,15 @@ const formatWaterBillData = (bill) => { // 获取所有水费记录(支持搜索和分页) const getAllWaterBills = async (req, res) => { try { - const { roomId, status, startDate, endDate, page = 1, pageSize = 10 } = req.query; + const { + apartmentId, + roomId, + status, + startDateFrom, + endDateTo, + page = 1, + pageSize = 10 + } = req.query; // 构建查询条件 const where = { isDeleted: 0 }; @@ -44,25 +52,29 @@ const getAllWaterBills = async (req, res) => { if (status) { where.status = status; } - if (startDate) { - where.startDate = { [Op.gte]: new Date(startDate) }; - } - if (endDate) { - where.endDate = { [Op.lte]: new Date(endDate) }; + if (startDateFrom && endDateTo) { + where.startDate = { [Op.gte]: new Date(startDateFrom) }; + where.endDate = { [Op.lte]: new Date(endDateTo) }; } + // 构建包含关系 + const include = [ + { + model: Room, + where: { + isDeleted: 0, + ...(apartmentId ? { apartmentId } : {}) + } + } + ]; + // 计算偏移量 const offset = (page - 1) * pageSize; // 查询水费数据 const { count, rows } = await WaterBill.findAndCountAll({ where, - include: [ - { - model: Room, - where: { isDeleted: 0 } - } - ], + include, limit: parseInt(pageSize), offset: parseInt(offset), order: [['createTime', 'DESC']] // 按创建时间倒序排序 @@ -192,7 +204,13 @@ const deleteWaterBill = async (req, res) => { // 获取所有水费记录(不分页) const listWaterBills = async (req, res) => { try { - const { roomId, status, startDate, endDate } = req.query; + const { + apartmentId, + roomId, + status, + startDateFrom, + endDateTo + } = req.query; // 构建查询条件 const where = { isDeleted: 0 }; @@ -202,22 +220,26 @@ const listWaterBills = async (req, res) => { if (status) { where.status = status; } - if (startDate) { - where.startDate = { [Op.gte]: new Date(startDate) }; - } - if (endDate) { - where.endDate = { [Op.lte]: new Date(endDate) }; + if (startDateFrom && endDateTo) { + where.startDate = { [Op.gte]: new Date(startDateFrom) }; + where.endDate = { [Op.lte]: new Date(endDateTo) }; } + // 构建包含关系 + const include = [ + { + model: Room, + where: { + isDeleted: 0, + ...(apartmentId ? { apartmentId } : {}) + } + } + ]; + // 查询水费数据 const bills = await WaterBill.findAll({ where, - include: [ - { - model: Room, - where: { isDeleted: 0 } - } - ], + include, order: [['createTime', 'DESC']] // 按创建时间倒序排序 });