const { DataTypes } = require('sequelize'); const sequelize = require('../config/db'); const Room = require('./Room'); const Tenant = require('./Tenant'); const Contract = sequelize.define('Contract', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, roomId: { type: DataTypes.INTEGER, allowNull: false, references: { model: Room, key: 'id' } }, tenantId: { type: DataTypes.INTEGER, allowNull: false, references: { model: Tenant, key: 'id' } }, startDate: { type: DataTypes.DATE, allowNull: false }, endDate: { type: DataTypes.DATE, allowNull: false }, rent: { type: DataTypes.DECIMAL(10, 2), allowNull: false }, deposit: { type: DataTypes.DECIMAL(10, 2), allowNull: false }, status: { type: DataTypes.ENUM('active', 'expired'), allowNull: false, defaultValue: 'active' }, 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: 'contracts', timestamps: false }); // 建立关联 Contract.belongsTo(Room, { foreignKey: 'roomId' }); Contract.belongsTo(Tenant, { foreignKey: 'tenantId' }); module.exports = Contract;