From 6195a342121fec581980cc71989becd7de5d8288 Mon Sep 17 00:00:00 2001 From: wangxiaoxian <1094175543@qq.com> Date: Wed, 4 Mar 2026 15:39:06 +0800 Subject: [PATCH] =?UTF-8?q?list=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/sync-db.js | 71 ------------------------------ controllers/apartmentController.js | 33 ++++++++++++++ controllers/regionController.js | 23 ++++++++++ controllers/rentalController.js | 47 ++++++++++++++++++++ controllers/roomController.js | 47 ++++++++++++++++++++ controllers/tenantController.js | 33 ++++++++++++++ controllers/waterBillController.js | 43 ++++++++++++++++++ routes/apartment.js | 1 + routes/region.js | 1 + routes/rental.js | 1 + routes/room.js | 1 + routes/tenant.js | 1 + routes/waterBill.js | 5 ++- 13 files changed, 235 insertions(+), 72 deletions(-) diff --git a/config/sync-db.js b/config/sync-db.js index 25815f5..04454a4 100644 --- a/config/sync-db.js +++ b/config/sync-db.js @@ -7,9 +7,6 @@ const syncDatabase = async () => { console.log('正在同步数据库表结构...'); await sequelize.sync({ force: true }); console.log('数据库表结构同步成功'); - - // 插入初始数据 - await insertInitialData(); } catch (error) { console.error('数据库表结构同步失败:', error); } finally { @@ -17,72 +14,4 @@ const syncDatabase = async () => { } }; -// 插入初始数据 -const insertInitialData = async () => { - try { - // 插入区域数据 - const regions = await Region.bulkCreate([ - { id: 1, name: '大商汇', description: '大商汇区域', createTime: '2023-01-06' }, - { id: 2, name: '丰源市场', description: '丰源市场区域', createTime: '2023-01-07' } - ]); - - // 插入公寓数据 - const apartments = await Apartment.bulkCreate([ - { id: 1, regionId: 1, name: '爱奇艺公寓', address: '大商汇区域', createTime: '2023-01-18' }, - { id: 2, regionId: 2, name: '碧云公寓', address: '丰源市场区域', createTime: '2023-01-19' } - ]); - - // 插入房间数据 - const rooms = await Room.bulkCreate([ - // 大商汇 - 爱奇艺公寓 - { id: 1, apartmentId: 1, roomNumber: '401', area: 40, price: 2500, status: 'empty', createTime: '2023-01-18' }, - { id: 2, apartmentId: 1, roomNumber: '402', area: 40, price: 2500, status: 'empty', createTime: '2023-01-18' }, - { id: 3, apartmentId: 1, roomNumber: '403', area: 45, price: 2800, status: 'rented', createTime: '2023-01-18' }, - { id: 4, apartmentId: 1, roomNumber: '404', area: 45, price: 2800, status: 'soon_expire', createTime: '2023-01-18' }, - { id: 5, apartmentId: 1, roomNumber: '405', area: 50, price: 3000, status: 'empty', createTime: '2023-01-18' }, - { id: 6, apartmentId: 1, roomNumber: '406', area: 50, price: 3000, status: 'expired', createTime: '2023-01-18' }, - { id: 7, apartmentId: 1, roomNumber: '407', area: 55, price: 3200, status: 'cleaning', createTime: '2023-01-18' }, - // 丰源市场 - 碧云公寓 - { id: 8, apartmentId: 2, roomNumber: '201', area: 35, price: 2200, status: 'empty', createTime: '2023-01-19' }, - { id: 9, apartmentId: 2, roomNumber: '202', area: 35, price: 2200, status: 'rented', createTime: '2023-01-19' }, - { id: 10, apartmentId: 2, roomNumber: '203', area: 40, price: 2400, status: 'empty', createTime: '2023-01-19' }, - { id: 11, apartmentId: 2, roomNumber: '205', area: 40, price: 2400, status: 'maintenance', createTime: '2023-01-19' }, - { id: 12, apartmentId: 2, roomNumber: '206', area: 45, price: 2600, status: 'rented', createTime: '2023-01-19' }, - { id: 13, apartmentId: 2, roomNumber: '208', area: 45, price: 2600, status: 'empty', createTime: '2023-01-19' }, - { id: 14, apartmentId: 2, roomNumber: '209', area: 50, price: 2800, status: 'soon_expire', createTime: '2023-01-19' } - ]); - - // 插入租客数据 - const tenants = await Tenant.bulkCreate([ - { id: 1, name: '张三', phone: '13800138001', idCard: '110101199001011234', createTime: '2023-02-01' }, - { id: 2, name: '李四', phone: '13800138002', idCard: '110101199001011235', createTime: '2023-02-02' }, - { id: 3, name: '王五', phone: '13800138003', idCard: '110101199001011236', createTime: '2023-02-03' }, - { id: 4, name: '赵六', phone: '13800138004', idCard: '110101199001011237', createTime: '2023-02-04' }, - { id: 5, name: '钱七', phone: '13800138005', idCard: '110101199001011238', createTime: '2023-02-05' } - ]); - - // 插入合同数据 - const contracts = await Contract.bulkCreate([ - { id: 1, roomId: 3, tenantId: 1, startDate: '2023-03-01', endDate: '2024-03-01', rent: 2800, deposit: 5600, status: 'active', createTime: '2023-02-28' }, - { id: 2, roomId: 9, tenantId: 2, startDate: '2023-04-01', endDate: '2024-04-01', rent: 2200, deposit: 4400, status: 'active', createTime: '2023-03-31' }, - { id: 3, roomId: 12, tenantId: 3, startDate: '2023-05-01', endDate: '2024-05-01', rent: 2600, deposit: 5200, status: 'active', createTime: '2023-04-30' }, - { id: 4, roomId: 4, tenantId: 4, startDate: '2022-03-01', endDate: '2023-03-01', rent: 2800, deposit: 5600, status: 'expired', createTime: '2022-02-28' }, - { id: 5, roomId: 6, tenantId: 5, startDate: '2022-04-01', endDate: '2023-04-01', rent: 3000, deposit: 6000, status: 'expired', createTime: '2022-03-31' } - ]); - - // 插入租房数据 - const rentals = await Rental.bulkCreate([ - { id: 1, roomId: 3, tenantId: 1, contractId: 1, startDate: '2023-03-01', endDate: '2024-03-01', rent: 2800, deposit: 5600, status: 'active', createTime: '2023-02-28' }, - { id: 2, roomId: 9, tenantId: 2, contractId: 2, startDate: '2023-04-01', endDate: '2024-04-01', rent: 2200, deposit: 4400, status: 'active', createTime: '2023-03-31' }, - { id: 3, roomId: 12, tenantId: 3, contractId: 3, startDate: '2023-05-01', endDate: '2024-05-01', rent: 2600, deposit: 5200, status: 'active', createTime: '2023-04-30' }, - { id: 4, roomId: 4, tenantId: 4, contractId: 4, startDate: '2022-03-01', endDate: '2023-03-01', rent: 2800, deposit: 5600, status: 'expired', createTime: '2022-02-28' }, - { id: 5, roomId: 6, tenantId: 5, contractId: 5, startDate: '2022-04-01', endDate: '2023-04-01', rent: 3000, deposit: 6000, status: 'expired', createTime: '2022-03-31' } - ]); - - console.log('初始数据插入成功'); - } catch (error) { - console.error('初始数据插入失败:', error); - } -}; - syncDatabase(); \ No newline at end of file diff --git a/controllers/apartmentController.js b/controllers/apartmentController.js index 19fa133..d1245e5 100644 --- a/controllers/apartmentController.js +++ b/controllers/apartmentController.js @@ -135,8 +135,41 @@ const deleteApartment = async (req, res) => { } }; +// 获取所有公寓(不分页) +const listApartments = async (req, res) => { + try { + const { regionId, name } = req.query; + + // 构建查询条件 + const where = { isDeleted: 0 }; + if (regionId) { + where.regionId = regionId; + } + if (name) { + where.name = { + [Op.like]: `%${name}%` + }; + } + + // 查询公寓数据 + const apartments = await Apartment.findAll({ + where, + include: [Region] + }); + + // 格式化数据 + const formattedApartments = apartments.map(formatApartmentData); + + // 返回结果 + res.status(200).json(formattedApartments); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllApartments, + listApartments, getApartmentById, createApartment, updateApartment, diff --git a/controllers/regionController.js b/controllers/regionController.js index 0b0e1f4..9cec70f 100644 --- a/controllers/regionController.js +++ b/controllers/regionController.js @@ -1,4 +1,5 @@ const { Region } = require('../models'); +const { Op } = require('sequelize'); // 格式化时间(考虑时区,转换为北京时间) const formatDate = (date) => { @@ -93,8 +94,30 @@ const deleteRegion = async (req, res) => { } }; +// 获取所有区域(不分页) +const listRegions = async (req, res) => { + try { + const { name } = req.query; + + // 构建查询条件 + const where = { isDeleted: 0 }; + if (name) { + where.name = { + [Op.like]: `%${name}%` + }; + } + + const regions = await Region.findAll({ where }); + const formattedRegions = regions.map(formatRegionData); + res.status(200).json(formattedRegions); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllRegions, + listRegions, getRegionById, createRegion, updateRegion, diff --git a/controllers/rentalController.js b/controllers/rentalController.js index e923eb1..8ad7050 100644 --- a/controllers/rentalController.js +++ b/controllers/rentalController.js @@ -342,8 +342,55 @@ const deleteRental = async (req, res) => { } }; +// 获取所有租房(不分页) +const listRentals = async (req, res) => { + try { + // 先检查并更新租房状态 + await checkAndUpdateRentalStatus(); + + const { roomId, tenantName, status } = req.query; + + // 构建查询条件 + const where = { isDeleted: 0 }; + if (status) { + where.status = status; + } + if (roomId) { + where.roomId = roomId; + } + + // 构建包含关系 - 关联租客信息和合同信息 + const include = [ + { + model: Tenant, + where: tenantName ? { name: { [Op.like]: `%${tenantName}%` }, isDeleted: 0 } : { isDeleted: 0 } + }, + { + model: Contract, + where: { isDeleted: 0 } + } + ]; + + // 查询租房数据 + const rentals = await Rental.findAll({ + where, + include, + order: [['createTime', 'DESC']] // 按创建时间倒序排序 + }); + + // 格式化数据 + const formattedRentals = rentals.map(formatRentalData); + + // 返回结果 + res.status(200).json(formattedRentals); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllRentals, + listRentals, getRentalById, createRental, updateRental, diff --git a/controllers/roomController.js b/controllers/roomController.js index bfaad92..b595064 100644 --- a/controllers/roomController.js +++ b/controllers/roomController.js @@ -296,8 +296,55 @@ const deleteRoom = async (req, res) => { } }; +// 获取所有房间(不分页) +const listRooms = async (req, res) => { + try { + // 先检查并更新租房状态 + await checkAndUpdateRentalStatus(); + + const { apartmentId, roomNumber, status, otherStatus, subStatus } = req.query; + + // 构建查询条件 + const where = { isDeleted: 0 }; + if (apartmentId) { + where.apartmentId = apartmentId; + } + if (roomNumber) { + where.roomNumber = { + [Op.like]: `%${roomNumber}%` + }; + } + if (status) { + where.status = status; + } + if (otherStatus) { + where.otherStatus = otherStatus; + } + if (subStatus) { + where.subStatus = subStatus; + } + + // 查询房间数据 + const rooms = await Room.findAll({ + where, + include: [ + Apartment + ] + }); + + // 格式化房间数据 + const formattedRooms = rooms.map(formatRoomData); + + // 返回结果 + res.status(200).json(formattedRooms); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllRooms, + listRooms, getRoomById, createRoom, updateRoom, diff --git a/controllers/tenantController.js b/controllers/tenantController.js index 9f0ccda..c19e2aa 100644 --- a/controllers/tenantController.js +++ b/controllers/tenantController.js @@ -1,4 +1,5 @@ const { Tenant } = require('../models'); +const { Op } = require('sequelize'); // 格式化时间(考虑时区,转换为北京时间) const formatDate = (date) => { @@ -93,8 +94,40 @@ const deleteTenant = async (req, res) => { } }; +// 获取所有租客(不分页) +const listTenants = async (req, res) => { + try { + const { name, phone, idCard } = req.query; + + // 构建查询条件 + const where = { isDeleted: 0 }; + if (name) { + where.name = { + [Op.like]: `%${name}%` + }; + } + if (phone) { + where.phone = { + [Op.like]: `%${phone}%` + }; + } + if (idCard) { + where.idCard = { + [Op.like]: `%${idCard}%` + }; + } + + const tenants = await Tenant.findAll({ where }); + const formattedTenants = tenants.map(formatTenantData); + res.status(200).json(formattedTenants); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllTenants, + listTenants, getTenantById, createTenant, updateTenant, diff --git a/controllers/waterBillController.js b/controllers/waterBillController.js index 83a43ff..32eb9bb 100644 --- a/controllers/waterBillController.js +++ b/controllers/waterBillController.js @@ -189,8 +189,51 @@ const deleteWaterBill = async (req, res) => { } }; +// 获取所有水费记录(不分页) +const listWaterBills = async (req, res) => { + try { + const { roomId, status, startDate, endDate } = req.query; + + // 构建查询条件 + const where = { isDeleted: 0 }; + if (roomId) { + where.roomId = roomId; + } + if (status) { + where.status = status; + } + if (startDate) { + where.startDate = { [Op.gte]: new Date(startDate) }; + } + if (endDate) { + where.endDate = { [Op.lte]: new Date(endDate) }; + } + + // 查询水费数据 + const bills = await WaterBill.findAll({ + where, + include: [ + { + model: Room, + where: { isDeleted: 0 } + } + ], + order: [['createTime', 'DESC']] // 按创建时间倒序排序 + }); + + // 格式化数据 + const formattedBills = bills.map(formatWaterBillData); + + // 返回结果 + res.status(200).json(formattedBills); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; + module.exports = { getAllWaterBills, + listWaterBills, getWaterBillById, createWaterBill, updateWaterBill, diff --git a/routes/apartment.js b/routes/apartment.js index c0dbedc..e490776 100644 --- a/routes/apartment.js +++ b/routes/apartment.js @@ -4,6 +4,7 @@ const apartmentController = require('../controllers/apartmentController'); // 路由 router.get('/', apartmentController.getAllApartments); +router.get('/list', apartmentController.listApartments); router.get('/:id', apartmentController.getApartmentById); router.post('/', apartmentController.createApartment); router.put('/:id', apartmentController.updateApartment); diff --git a/routes/region.js b/routes/region.js index fee52c0..fb79b83 100644 --- a/routes/region.js +++ b/routes/region.js @@ -4,6 +4,7 @@ const regionController = require('../controllers/regionController'); // 路由 router.get('/', regionController.getAllRegions); +router.get('/list', regionController.listRegions); router.get('/:id', regionController.getRegionById); router.post('/', regionController.createRegion); router.put('/:id', regionController.updateRegion); diff --git a/routes/rental.js b/routes/rental.js index 14a6fc7..b3badc9 100644 --- a/routes/rental.js +++ b/routes/rental.js @@ -4,6 +4,7 @@ const rentalController = require('../controllers/rentalController'); // 路由 router.get('/', rentalController.getAllRentals); +router.get('/list', rentalController.listRentals); router.get('/:id', rentalController.getRentalById); router.post('/', rentalController.createRental); router.put('/:id', rentalController.updateRental); diff --git a/routes/room.js b/routes/room.js index 8af79ef..a581b67 100644 --- a/routes/room.js +++ b/routes/room.js @@ -4,6 +4,7 @@ const roomController = require('../controllers/roomController'); // 路由 router.get('/', roomController.getAllRooms); +router.get('/list', roomController.listRooms); router.get('/:id', roomController.getRoomById); router.post('/', roomController.createRoom); router.put('/:id', roomController.updateRoom); diff --git a/routes/tenant.js b/routes/tenant.js index c0ae587..47d8ed8 100644 --- a/routes/tenant.js +++ b/routes/tenant.js @@ -4,6 +4,7 @@ const tenantController = require('../controllers/tenantController'); // 路由 router.get('/', tenantController.getAllTenants); +router.get('/list', tenantController.listTenants); router.get('/:id', tenantController.getTenantById); router.post('/', tenantController.createTenant); router.put('/:id', tenantController.updateTenant); diff --git a/routes/waterBill.js b/routes/waterBill.js index 8d3cdc4..da87a53 100644 --- a/routes/waterBill.js +++ b/routes/waterBill.js @@ -2,9 +2,12 @@ const express = require('express'); const router = express.Router(); const waterBillController = require('../controllers/waterBillController'); -// 获取所有水费记录 +// 获取所有水费记录(分页) router.get('/', waterBillController.getAllWaterBills); +// 获取所有水费记录(不分页) +router.get('/list', waterBillController.listWaterBills); + // 获取单个水费记录 router.get('/:id', waterBillController.getWaterBillById);