const { DataTypes } = require('sequelize'); const sequelize = require('../config/db'); const Room = require('./Room'); const Renter = require('./Renter'); const Rental = require('./Rental'); const Bill = require('./Bill'); const MeterReading = sequelize.define('MeterReading', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, comment: '记录ID' }, roomId: { type: DataTypes.INTEGER, allowNull: false, references: { model: Room, key: 'id' }, comment: '房间ID' }, renterId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Renter, key: 'id' }, comment: '租客ID(可选)' }, rentalId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Rental, key: 'id' }, comment: '租赁ID(可选)' }, meterType: { type: DataTypes.ENUM('water', 'electricity', 'gas'), allowNull: false, comment: '表类型(water-水表, electricity-电表, gas-燃气表)' }, previousReading: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '上期读数' }, currentReading: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '本期读数' }, usage: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '用量' }, unitPrice: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '单价' }, amount: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '金额' }, billMonth: { type: DataTypes.STRING(7), allowNull: false, comment: '账单月份(格式:YYYY-MM)' }, readingDate: { type: DataTypes.DATEONLY, allowNull: false, comment: '抄表日期' }, billId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Bill, key: 'id' }, comment: '关联账单ID' }, remark: { type: DataTypes.TEXT, allowNull: true, comment: '备注' }, createBy: { type: DataTypes.INTEGER, allowNull: true, comment: '创建人ID' }, createTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, comment: '创建时间' }, updateBy: { type: DataTypes.INTEGER, allowNull: true, comment: '修改人ID' }, updateTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, onUpdate: DataTypes.NOW, comment: '更新时间' }, tenantId: { type: DataTypes.INTEGER, allowNull: false, comment: '租户ID' }, isDeleted: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: '删除状态(0:未删除,1:已删除)' } }, { tableName: 'meter_readings', timestamps: false }); // 建立关联 MeterReading.belongsTo(Room, { foreignKey: 'roomId' }); MeterReading.belongsTo(Renter, { foreignKey: 'renterId' }); MeterReading.belongsTo(Rental, { foreignKey: 'rentalId' }); MeterReading.belongsTo(Bill, { foreignKey: 'billId' }); module.exports = MeterReading;