const { DataTypes } = require('sequelize'); const sequelize = require('../config/db'); const Bill = require('./Bill'); const Transaction = sequelize.define('Transaction', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, comment: '流水ID' }, billId: { type: DataTypes.INTEGER, allowNull: false, references: { model: Bill, key: 'id' }, comment: '关联账单ID' }, type: { type: DataTypes.ENUM('income', 'expense'), allowNull: false, comment: '流水类型(income:收入,expense:支出)' }, amount: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '金额' }, paymentMethod: { type: DataTypes.ENUM('cash', 'bank', 'wechat', 'alipay', 'other'), allowNull: false, defaultValue: 'cash', comment: '支付方式(cash:现金,bank:银行转账,wechat:微信,alipay:支付宝,other:其他)' }, transactionDate: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, comment: '交易日期' }, remark: { type: DataTypes.TEXT, allowNull: true, comment: '备注' }, createBy: { type: DataTypes.INTEGER, allowNull: true, comment: '创建人ID' }, createTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, comment: '创建时间' }, tenantId: { type: DataTypes.INTEGER, allowNull: false, comment: '租户ID' }, isDeleted: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: '删除状态(0:未删除,1:已删除)' } }, { tableName: 'transactions', timestamps: false }); // 建立关联 Transaction.belongsTo(Bill, { foreignKey: 'billId' }); Bill.hasMany(Transaction, { foreignKey: 'billId' }); module.exports = Transaction;