const { DataTypes } = require('sequelize'); const sequelize = require('../config/db'); const Apartment = require('./Apartment'); const Room = sequelize.define('Room', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, apartmentId: { type: DataTypes.INTEGER, allowNull: false, references: { model: Apartment, key: 'id' } }, roomNumber: { type: DataTypes.STRING(20), allowNull: false }, area: { type: DataTypes.DECIMAL(10, 2), allowNull: false }, monthlyPrice: { type: DataTypes.DECIMAL(10, 2), allowNull: false }, yearlyPrice: { type: DataTypes.DECIMAL(10, 2), allowNull: false }, status: { type: DataTypes.ENUM('empty', 'rented'), allowNull: false, defaultValue: 'empty' }, subStatus: { type: DataTypes.ENUM('normal', 'soon_expire', 'expired'), allowNull: false, defaultValue: 'normal' }, otherStatus: { type: DataTypes.ENUM('', 'cleaning', 'maintenance'), allowNull: false, defaultValue: '' }, 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: 'rooms', timestamps: false }); // 建立关联 Room.belongsTo(Apartment, { foreignKey: 'apartmentId' }); Apartment.hasMany(Room, { foreignKey: 'apartmentId' }); module.exports = Room;