You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
356 lines
7.5 KiB
356 lines
7.5 KiB
|
3 months ago
|
// 注意:此文件是MongoDB版本的扩展实现,已被禁用
|
||
|
|
// 数据库扩展 - 用于连接userlogin数据库并关联表
|
||
|
|
const { Sequelize, DataTypes, Model } = require('sequelize');
|
||
|
|
const path = require('path');
|
||
|
|
require('dotenv').config({ path: path.resolve(__dirname, '.env') });
|
||
|
|
|
||
|
|
// 注意:不再直接导入User模型以避免循环依赖
|
||
|
|
// User模型将通过setupAssociations函数的参数传入
|
||
|
|
let User = null;
|
||
|
|
|
||
|
|
// 创建到userlogin数据库的连接
|
||
|
|
const sequelizeUserLogin = new Sequelize(
|
||
|
|
process.env.DB_DATABASE_USERLOGIN || 'userlogin',
|
||
|
|
process.env.DB_USER || 'root',
|
||
|
|
process.env.DB_PASSWORD,
|
||
|
|
{
|
||
|
|
host: process.env.DB_HOST || 'localhost',
|
||
|
|
port: process.env.DB_PORT || 3306,
|
||
|
|
dialect: 'mysql',
|
||
|
|
pool: {
|
||
|
|
max: 10,
|
||
|
|
min: 0,
|
||
|
|
acquire: 30000,
|
||
|
|
idle: 10000
|
||
|
|
},
|
||
|
|
timezone: '+08:00' // 设置时区为UTC+8
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 测试userlogin数据库连接
|
||
|
|
async function testUserLoginDbConnection() {
|
||
|
|
try {
|
||
|
|
await sequelizeUserLogin.authenticate();
|
||
|
|
console.log('userlogin数据库连接成功');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('userlogin数据库连接失败:', error);
|
||
|
|
console.error('请注意:如果不需要使用userlogin数据库,可以忽略此错误');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 定义userlogin数据库中的表模型
|
||
|
|
|
||
|
|
// contact表模型
|
||
|
|
class Contact extends Model { }
|
||
|
|
Contact.init({
|
||
|
|
id: {
|
||
|
|
type: DataTypes.INTEGER,
|
||
|
|
autoIncrement: true,
|
||
|
|
primaryKey: true
|
||
|
|
},
|
||
|
|
userId: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
name: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
phone: {
|
||
|
|
type: DataTypes.STRING(20),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
email: {
|
||
|
|
type: DataTypes.STRING(100)
|
||
|
|
},
|
||
|
|
address: {
|
||
|
|
type: DataTypes.TEXT
|
||
|
|
},
|
||
|
|
created_at: {
|
||
|
|
type: DataTypes.DATE,
|
||
|
|
defaultValue: Sequelize.NOW
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
sequelize: sequelizeUserLogin,
|
||
|
|
modelName: 'Contact',
|
||
|
|
tableName: 'contact',
|
||
|
|
timestamps: false
|
||
|
|
});
|
||
|
|
|
||
|
|
// enterprise表模型
|
||
|
|
class Enterprise extends Model { }
|
||
|
|
Enterprise.init({
|
||
|
|
id: {
|
||
|
|
type: DataTypes.INTEGER,
|
||
|
|
autoIncrement: true,
|
||
|
|
primaryKey: true
|
||
|
|
},
|
||
|
|
userId: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
enterpriseName: {
|
||
|
|
type: DataTypes.STRING(255),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
businessLicense: {
|
||
|
|
type: DataTypes.STRING(255)
|
||
|
|
},
|
||
|
|
address: {
|
||
|
|
type: DataTypes.TEXT
|
||
|
|
},
|
||
|
|
contactPerson: {
|
||
|
|
type: DataTypes.STRING(100)
|
||
|
|
},
|
||
|
|
contactPhone: {
|
||
|
|
type: DataTypes.STRING(20)
|
||
|
|
},
|
||
|
|
created_at: {
|
||
|
|
type: DataTypes.DATE,
|
||
|
|
defaultValue: Sequelize.NOW
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
sequelize: sequelizeUserLogin,
|
||
|
|
modelName: 'Enterprise',
|
||
|
|
tableName: 'enterprise',
|
||
|
|
timestamps: false
|
||
|
|
});
|
||
|
|
|
||
|
|
// managers表模型
|
||
|
|
class Manager extends Model { }
|
||
|
|
Manager.init({
|
||
|
|
id: {
|
||
|
|
type: DataTypes.INTEGER,
|
||
|
|
autoIncrement: true,
|
||
|
|
primaryKey: true
|
||
|
|
},
|
||
|
|
userId: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
managerName: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
managerPhone: {
|
||
|
|
type: DataTypes.STRING(20),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
role: {
|
||
|
|
type: DataTypes.STRING(50),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
created_at: {
|
||
|
|
type: DataTypes.DATE,
|
||
|
|
defaultValue: Sequelize.NOW
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
sequelize: sequelizeUserLogin,
|
||
|
|
modelName: 'Manager',
|
||
|
|
tableName: 'managers',
|
||
|
|
timestamps: false
|
||
|
|
});
|
||
|
|
|
||
|
|
// publicseademand表模型
|
||
|
|
class PublicSeaDemand extends Model { }
|
||
|
|
PublicSeaDemand.init({
|
||
|
|
id: {
|
||
|
|
type: DataTypes.INTEGER,
|
||
|
|
autoIncrement: true,
|
||
|
|
primaryKey: true
|
||
|
|
},
|
||
|
|
userId: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
demandType: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
description: {
|
||
|
|
type: DataTypes.TEXT
|
||
|
|
},
|
||
|
|
status: {
|
||
|
|
type: DataTypes.STRING(50),
|
||
|
|
defaultValue: 'pending'
|
||
|
|
},
|
||
|
|
created_at: {
|
||
|
|
type: DataTypes.DATE,
|
||
|
|
defaultValue: Sequelize.NOW
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
sequelize: sequelizeUserLogin,
|
||
|
|
modelName: 'PublicSeaDemand',
|
||
|
|
tableName: 'publicseademand',
|
||
|
|
timestamps: false
|
||
|
|
});
|
||
|
|
|
||
|
|
// rootdb表模型
|
||
|
|
class RootDb extends Model { }
|
||
|
|
RootDb.init({
|
||
|
|
id: {
|
||
|
|
type: DataTypes.INTEGER,
|
||
|
|
autoIncrement: true,
|
||
|
|
primaryKey: true
|
||
|
|
},
|
||
|
|
userId: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
dataKey: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
dataValue: {
|
||
|
|
type: DataTypes.TEXT
|
||
|
|
},
|
||
|
|
created_at: {
|
||
|
|
type: DataTypes.DATE,
|
||
|
|
defaultValue: Sequelize.NOW
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
sequelize: sequelizeUserLogin,
|
||
|
|
modelName: 'RootDb',
|
||
|
|
tableName: 'rootdb',
|
||
|
|
timestamps: false
|
||
|
|
});
|
||
|
|
|
||
|
|
// login表模型
|
||
|
|
class Login extends Model { }
|
||
|
|
Login.init({
|
||
|
|
id: {
|
||
|
|
type: DataTypes.INTEGER,
|
||
|
|
autoIncrement: true,
|
||
|
|
primaryKey: true
|
||
|
|
},
|
||
|
|
userId: {
|
||
|
|
type: DataTypes.STRING(100),
|
||
|
|
allowNull: false
|
||
|
|
},
|
||
|
|
loginTime: {
|
||
|
|
type: DataTypes.DATE,
|
||
|
|
defaultValue: Sequelize.NOW
|
||
|
|
},
|
||
|
|
loginIp: {
|
||
|
|
type: DataTypes.STRING(50)
|
||
|
|
},
|
||
|
|
deviceInfo: {
|
||
|
|
type: DataTypes.TEXT
|
||
|
|
},
|
||
|
|
status: {
|
||
|
|
type: DataTypes.STRING(20),
|
||
|
|
defaultValue: 'success'
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
sequelize: sequelizeUserLogin,
|
||
|
|
modelName: 'Login',
|
||
|
|
tableName: 'login',
|
||
|
|
timestamps: false
|
||
|
|
});
|
||
|
|
|
||
|
|
// 设置模型关联关系
|
||
|
|
function setupAssociations(mainUserModel) {
|
||
|
|
// 确保使用传入的User模型,不再依赖默认的User变量
|
||
|
|
if (!mainUserModel) {
|
||
|
|
console.error('User模型未提供,无法设置关联关系');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 关联User与Contact(一对多)
|
||
|
|
// 使用唯一的别名userContacts以避免可能的冲突
|
||
|
|
mainUserModel.hasMany(Contact, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
sourceKey: 'userId',
|
||
|
|
as: 'userContacts'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 反向关联Contact与User
|
||
|
|
Contact.belongsTo(mainUserModel, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
targetKey: 'userId',
|
||
|
|
as: 'user'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 关联User与Enterprise(一对多)
|
||
|
|
mainUserModel.hasMany(Enterprise, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
sourceKey: 'userId',
|
||
|
|
as: 'userEnterprises'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 反向关联Enterprise与User
|
||
|
|
Enterprise.belongsTo(mainUserModel, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
targetKey: 'userId',
|
||
|
|
as: 'user'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 关联User与Manager(一对多)
|
||
|
|
mainUserModel.hasMany(Manager, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
sourceKey: 'userId',
|
||
|
|
as: 'userManagers'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 反向关联Manager与User
|
||
|
|
Manager.belongsTo(mainUserModel, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
targetKey: 'userId',
|
||
|
|
as: 'user'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 关联User与PublicSeaDemand(一对多)
|
||
|
|
mainUserModel.hasMany(PublicSeaDemand, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
sourceKey: 'userId',
|
||
|
|
as: 'userPublicSeaDemands'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 反向关联PublicSeaDemand与User
|
||
|
|
PublicSeaDemand.belongsTo(mainUserModel, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
targetKey: 'userId',
|
||
|
|
as: 'user'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 关联User与RootDb(一对多)
|
||
|
|
mainUserModel.hasMany(RootDb, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
sourceKey: 'userId',
|
||
|
|
as: 'userRootDbs'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 反向关联RootDb与User
|
||
|
|
RootDb.belongsTo(mainUserModel, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
targetKey: 'userId',
|
||
|
|
as: 'user'
|
||
|
|
});
|
||
|
|
|
||
|
|
// 关联User与Login(一对多)
|
||
|
|
mainUserModel.hasMany(Login, {
|
||
|
|
foreignKey: 'userId',
|
||
|
|
sourceKey: 'userId',
|
||
|
|
as: 'userLoginRecords'
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('已设置wechat_app数据库的User模型与userlogin数据库表的关联关系');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('设置模型关联关系时出错:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 导出所有模型和连接
|
||
|
|
module.exports = {
|
||
|
|
sequelizeUserLogin,
|
||
|
|
testUserLoginDbConnection,
|
||
|
|
Contact,
|
||
|
|
Enterprise,
|
||
|
|
Manager,
|
||
|
|
PublicSeaDemand,
|
||
|
|
RootDb,
|
||
|
|
Login,
|
||
|
|
setupAssociations,
|
||
|
|
User // 导出User模型(可能是实际的模型或临时模型)
|
||
|
|
};
|