2025-12-18 10:45:13 +08:00
|
|
|
const { DataTypes } = require('sequelize');
|
|
|
|
|
const { sequelize } = require('../db');
|
2026-04-02 10:34:13 +08:00
|
|
|
const { generateId } = require('../utils/idGenerator');
|
2026-03-20 17:15:14 +08:00
|
|
|
const Rack = require('./Rack');
|
|
|
|
|
const Warehouse = require('./Warehouse');
|
2025-12-18 10:45:13 +08:00
|
|
|
|
2026-03-27 19:12:16 +08:00
|
|
|
const Device = sequelize.define(
|
|
|
|
|
'Device',
|
|
|
|
|
{
|
|
|
|
|
deviceId: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
primaryKey: true,
|
2026-04-02 10:34:13 +08:00
|
|
|
allowNull: true,
|
2026-03-27 19:12:16 +08:00
|
|
|
unique: true,
|
|
|
|
|
},
|
|
|
|
|
name: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
type: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
model: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
serialNumber: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
unique: true,
|
|
|
|
|
},
|
|
|
|
|
rackId: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
position: {
|
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
height: {
|
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: 1,
|
|
|
|
|
},
|
|
|
|
|
powerConsumption: {
|
|
|
|
|
type: DataTypes.FLOAT,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: 0,
|
|
|
|
|
},
|
|
|
|
|
status: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
defaultValue: 'offline',
|
|
|
|
|
},
|
|
|
|
|
isIdle: {
|
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
|
defaultValue: false,
|
|
|
|
|
},
|
|
|
|
|
idleDate: {
|
|
|
|
|
type: DataTypes.DATE,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
idleReason: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
warehouseId: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
sourceType: {
|
|
|
|
|
type: DataTypes.ENUM('rack', 'warehouse'),
|
|
|
|
|
defaultValue: 'rack',
|
|
|
|
|
},
|
|
|
|
|
purchaseDate: {
|
|
|
|
|
type: DataTypes.DATE,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
warrantyExpiry: {
|
|
|
|
|
type: DataTypes.DATE,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
ipAddress: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
description: {
|
|
|
|
|
type: DataTypes.TEXT,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
|
|
|
|
customFields: {
|
|
|
|
|
type: DataTypes.JSON,
|
|
|
|
|
defaultValue: {},
|
|
|
|
|
allowNull: true,
|
|
|
|
|
},
|
2025-12-18 10:45:13 +08:00
|
|
|
},
|
2026-03-27 19:12:16 +08:00
|
|
|
{
|
|
|
|
|
tableName: 'devices',
|
|
|
|
|
timestamps: true,
|
|
|
|
|
indexes: [
|
|
|
|
|
{ fields: ['status'] },
|
|
|
|
|
{ fields: ['type'] },
|
|
|
|
|
{ fields: ['rackId'] },
|
|
|
|
|
{ fields: ['createdAt'] },
|
|
|
|
|
{ fields: ['status', 'type'] },
|
|
|
|
|
{ fields: ['name'] },
|
2026-03-29 10:40:33 +08:00
|
|
|
// 优化位置冲突检测的复合索引
|
|
|
|
|
{ fields: ['rackId', 'position'] },
|
|
|
|
|
{ fields: ['rackId', 'position', 'isIdle'] },
|
2026-03-27 19:12:16 +08:00
|
|
|
],
|
2026-04-02 10:34:13 +08:00
|
|
|
hooks: {
|
|
|
|
|
beforeCreate: (device) => {
|
|
|
|
|
if (!device.deviceId) {
|
|
|
|
|
device.deviceId = generateId({ prefix: 'DEV' });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-12-18 10:45:13 +08:00
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
);
|
2025-12-18 10:45:13 +08:00
|
|
|
|
2026-03-20 17:15:14 +08:00
|
|
|
Device.belongsTo(Rack, { foreignKey: 'rackId' });
|
|
|
|
|
Device.belongsTo(Warehouse, { foreignKey: 'warehouseId' });
|
|
|
|
|
|
2026-03-06 13:57:37 +08:00
|
|
|
module.exports = Device;
|