Files
yunrui_asset/backend/models/Cable.js
T
zhang1106 7f1df28858 feat: 实现ID生成器并重构模型ID生成逻辑
refactor: 统一ID生成方式,使用新的idGenerator模块
refactor: 重构模型ID生成逻辑,允许空ID并在创建前自动生成
fix(roomSchema): 使机房ID字段变为可选并更新前端表单验证
style: 格式化代码并优化导入语句
test: 添加操作日志集成测试文件
docs: 添加错误处理模块文档
2026-04-02 10:34:13 +08:00

108 lines
2.4 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const Device = require('./Device');
const { generateId } = require('../utils/idGenerator');
const Cable = sequelize.define(
'Cable',
{
cableId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: true,
unique: true,
},
sourceDeviceId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: 'devices',
key: 'deviceId',
},
},
sourcePort: {
type: DataTypes.STRING,
allowNull: false,
},
targetDeviceId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: 'devices',
key: 'deviceId',
},
},
targetPort: {
type: DataTypes.STRING,
allowNull: false,
},
cableType: {
type: DataTypes.ENUM('ethernet', 'fiber', 'copper'),
defaultValue: 'ethernet',
allowNull: false,
},
cableLength: {
type: DataTypes.DECIMAL(5, 2),
allowNull: true,
},
status: {
type: DataTypes.ENUM('normal', 'fault', 'disconnected'),
defaultValue: 'normal',
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
cableLabel: {
type: DataTypes.STRING,
allowNull: true,
comment: '线缆标签/编号',
},
cableColor: {
type: DataTypes.STRING,
allowNull: true,
comment: '线缆颜色(便于识别)',
},
installedBy: {
type: DataTypes.STRING,
allowNull: true,
comment: '安装人',
},
installedAt: {
type: DataTypes.DATE,
allowNull: true,
comment: '安装时间',
},
lastTestedAt: {
type: DataTypes.DATE,
allowNull: true,
comment: '上次测试时间',
},
},
{
tableName: 'cables',
timestamps: true,
indexes: [
{ fields: ['sourceDeviceId'] },
{ fields: ['targetDeviceId'] },
{ fields: ['status'] },
{ fields: ['cableType'] },
{ fields: ['sourceDeviceId', 'targetDeviceId'] },
{ fields: ['cableLabel'] },
],
hooks: {
beforeCreate: (cable) => {
if (!cable.cableId) {
cable.cableId = generateId({ prefix: 'CBL' });
}
},
},
}
);
Cable.belongsTo(Device, { foreignKey: 'sourceDeviceId', as: 'sourceDevice' });
Cable.belongsTo(Device, { foreignKey: 'targetDeviceId', as: 'targetDevice' });
module.exports = Cable;