2025-09-17 18:04:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 模型索引文件
|
|
|
|
|
|
* @file index.js
|
|
|
|
|
|
* @description 导出所有模型并建立关联关系
|
|
|
|
|
|
*/
|
|
|
|
|
|
const { sequelize } = require('../config/database');
|
|
|
|
|
|
|
|
|
|
|
|
// 导入所有模型
|
|
|
|
|
|
const User = require('./User');
|
|
|
|
|
|
const Role = require('./Role');
|
|
|
|
|
|
const Account = require('./Account');
|
|
|
|
|
|
const Transaction = require('./Transaction');
|
2025-09-22 17:56:30 +08:00
|
|
|
|
const LoanProduct = require('./LoanProduct');
|
2025-09-25 15:53:44 +08:00
|
|
|
|
const Employee = require('./Employee')(sequelize);
|
2025-09-22 17:56:30 +08:00
|
|
|
|
const Department = require('./Department');
|
|
|
|
|
|
const Position = require('./Position');
|
|
|
|
|
|
const Report = require('./Report');
|
2025-09-23 17:57:18 +08:00
|
|
|
|
const Project = require('./Project');
|
|
|
|
|
|
const SupervisionTask = require('./SupervisionTask');
|
2025-09-24 17:49:32 +08:00
|
|
|
|
const InstallationTask = require('./InstallationTask');
|
|
|
|
|
|
const CompletedSupervision = require('./CompletedSupervision');
|
|
|
|
|
|
const LoanApplication = require('./LoanApplication');
|
|
|
|
|
|
const AuditRecord = require('./AuditRecord');
|
|
|
|
|
|
const LoanContract = require('./LoanContract');
|
2025-09-17 18:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
// 定义模型关联关系
|
|
|
|
|
|
|
|
|
|
|
|
// 用户与角色关联
|
|
|
|
|
|
User.belongsTo(Role, {
|
|
|
|
|
|
foreignKey: 'role_id',
|
|
|
|
|
|
as: 'role',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Role.hasMany(User, {
|
|
|
|
|
|
foreignKey: 'role_id',
|
|
|
|
|
|
as: 'users'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 用户与账户关联
|
|
|
|
|
|
User.hasMany(Account, {
|
|
|
|
|
|
foreignKey: 'user_id',
|
|
|
|
|
|
as: 'accounts'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Account.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'user_id',
|
|
|
|
|
|
as: 'user'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 账户与交易记录关联
|
|
|
|
|
|
Account.hasMany(Transaction, {
|
|
|
|
|
|
foreignKey: 'account_id',
|
|
|
|
|
|
as: 'transactions'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Transaction.belongsTo(Account, {
|
|
|
|
|
|
foreignKey: 'account_id',
|
|
|
|
|
|
as: 'account'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 交易记录与用户关联(通过账户)
|
|
|
|
|
|
// 移除不合理的Transaction->User through Account的belongsTo定义,避免错误外键映射
|
|
|
|
|
|
|
2025-09-25 15:53:44 +08:00
|
|
|
|
// 员工关联关系(Employee模型使用字符串字段存储部门和职位,不需要关联)
|
2025-09-22 17:56:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 报表与用户关联
|
|
|
|
|
|
Report.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(Report, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'reports'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-23 17:57:18 +08:00
|
|
|
|
// 项目与用户关联(创建人)
|
|
|
|
|
|
Project.belongsTo(User, {
|
2025-09-25 15:53:44 +08:00
|
|
|
|
foreignKey: { name: 'createdBy', field: 'createdBy' },
|
2025-09-23 17:57:18 +08:00
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(Project, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'createdProjects'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 项目与用户关联(更新人)
|
|
|
|
|
|
Project.belongsTo(User, {
|
2025-09-25 15:53:44 +08:00
|
|
|
|
foreignKey: { name: 'updatedBy', field: 'updatedBy' },
|
2025-09-23 17:57:18 +08:00
|
|
|
|
as: 'updater',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(Project, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updatedProjects'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 监管任务与用户关联(创建人)
|
|
|
|
|
|
SupervisionTask.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(SupervisionTask, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'createdSupervisionTasks'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 监管任务与用户关联(更新人)
|
|
|
|
|
|
SupervisionTask.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updater',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(SupervisionTask, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updatedSupervisionTasks'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-24 17:49:32 +08:00
|
|
|
|
// 待安装任务与用户关联(创建人)
|
|
|
|
|
|
InstallationTask.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(InstallationTask, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'createdInstallationTasks'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 待安装任务与用户关联(更新人)
|
|
|
|
|
|
InstallationTask.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updater',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(InstallationTask, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updatedInstallationTasks'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 监管任务已结项与用户关联(创建人)
|
|
|
|
|
|
CompletedSupervision.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(CompletedSupervision, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'createdCompletedSupervisions'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 监管任务已结项与用户关联(更新人)
|
|
|
|
|
|
CompletedSupervision.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updater',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(CompletedSupervision, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updatedCompletedSupervisions'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 贷款商品与用户关联(创建人)
|
|
|
|
|
|
LoanProduct.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'creator',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(LoanProduct, {
|
|
|
|
|
|
foreignKey: 'createdBy',
|
|
|
|
|
|
as: 'createdLoanProducts'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 贷款商品与用户关联(更新人)
|
|
|
|
|
|
LoanProduct.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updater',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(LoanProduct, {
|
|
|
|
|
|
foreignKey: 'updatedBy',
|
|
|
|
|
|
as: 'updatedLoanProducts'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-25 15:53:44 +08:00
|
|
|
|
// 贷款申请暂时不关联用户表,因为当前表结构中没有外键字段
|
|
|
|
|
|
// 如果需要关联,需要先添加相应的外键字段到数据库表中
|
2025-09-24 17:49:32 +08:00
|
|
|
|
|
|
|
|
|
|
// 审核记录与贷款申请关联
|
|
|
|
|
|
AuditRecord.belongsTo(LoanApplication, {
|
|
|
|
|
|
foreignKey: 'applicationId',
|
|
|
|
|
|
as: 'application',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
LoanApplication.hasMany(AuditRecord, {
|
|
|
|
|
|
foreignKey: 'applicationId',
|
|
|
|
|
|
as: 'auditRecords'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 审核记录与用户关联(审核人)
|
|
|
|
|
|
AuditRecord.belongsTo(User, {
|
|
|
|
|
|
foreignKey: 'auditorId',
|
|
|
|
|
|
as: 'auditorUser',
|
|
|
|
|
|
targetKey: 'id'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.hasMany(AuditRecord, {
|
|
|
|
|
|
foreignKey: 'auditorId',
|
|
|
|
|
|
as: 'auditRecords'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-25 15:53:44 +08:00
|
|
|
|
// 贷款合同暂时不关联用户表,因为当前表结构中没有外键字段
|
|
|
|
|
|
// 如果需要关联,需要先添加相应的外键字段到数据库表中
|
2025-09-24 17:49:32 +08:00
|
|
|
|
|
2025-09-17 18:04:28 +08:00
|
|
|
|
// 导出所有模型和数据库实例
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
sequelize,
|
|
|
|
|
|
User,
|
|
|
|
|
|
Role,
|
|
|
|
|
|
Account,
|
2025-09-22 17:56:30 +08:00
|
|
|
|
Transaction,
|
|
|
|
|
|
LoanProduct,
|
|
|
|
|
|
Employee,
|
|
|
|
|
|
Department,
|
|
|
|
|
|
Position,
|
2025-09-23 17:57:18 +08:00
|
|
|
|
Report,
|
|
|
|
|
|
Project,
|
2025-09-24 17:49:32 +08:00
|
|
|
|
SupervisionTask,
|
|
|
|
|
|
InstallationTask,
|
|
|
|
|
|
CompletedSupervision,
|
|
|
|
|
|
LoanApplication,
|
|
|
|
|
|
AuditRecord,
|
|
|
|
|
|
LoanContract
|
2025-09-17 18:04:28 +08:00
|
|
|
|
};
|