diff --git a/controllers/rentalController.js b/controllers/rentalController.js index d49a4e1..2f2845f 100644 --- a/controllers/rentalController.js +++ b/controllers/rentalController.js @@ -40,7 +40,7 @@ const checkAndUpdateRentalStatus = async () => { const currentDate = new Date(); // 计算10天后的日期 const tenDaysLater = new Date(); - tenDaysLater.setDate(currentDate.getDate() + 10); + tenDaysLater.setDate(currentDate.getDate() + 30); // 查找所有活跃的租房记录 const rentals = await Rental.findAll({ @@ -59,16 +59,22 @@ const checkAndUpdateRentalStatus = async () => { // 检查是否已到期 if (endDate < currentDate) { - // 更新租房状态为已到期 - await rental.update({ status: 'expired' }); - // 更新房间状态为空房 - if (rental.Room) { - await rental.Room.update({ status: 'empty' }); + // 更新房间附属状态为已到期 + const room = await Room.findByPk(rental.roomId); + if (room && room.status === 'rented') { + await room.update({ subStatus: 'expired' }); } } else if (endDate <= tenDaysLater) { - // 更新房间状态为即将到期 - if (rental.Room && rental.Room.status === 'rented') { - await rental.Room.update({ status: 'soon_expire' }); + // 更新房间附属状态为即将到期 + const room = await Room.findByPk(rental.roomId); + if (room && room.status === 'rented') { + await room.update({ subStatus: 'soon_expire' }); + } + } else { + // 更新房间附属状态为正常 + const room = await Room.findByPk(rental.roomId); + if (room && room.status === 'rented') { + await room.update({ subStatus: 'normal' }); } } } diff --git a/sync-db.js b/sync-db.js index 64260a9..e3ce82b 100644 --- a/sync-db.js +++ b/sync-db.js @@ -52,6 +52,16 @@ const WaterBill = sequelize.define('WaterBill', { createTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW + }, + updateTime: { + type: DataTypes.DATE, + defaultValue: DataTypes.NOW, + onUpdate: DataTypes.NOW + }, + isDeleted: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0 } }, { tableName: 'water_bills', @@ -109,6 +119,16 @@ const ElectricityBill = sequelize.define('ElectricityBill', { createTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW + }, + updateTime: { + type: DataTypes.DATE, + defaultValue: DataTypes.NOW, + onUpdate: DataTypes.NOW + }, + isDeleted: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0 } }, { tableName: 'electricity_bills', @@ -174,6 +194,16 @@ const Rental = sequelize.define('Rental', { createTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW + }, + updateTime: { + type: DataTypes.DATE, + defaultValue: DataTypes.NOW, + onUpdate: DataTypes.NOW + }, + isDeleted: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0 } }, { tableName: 'rentals',