feat: 添加操作日志、危险操作确认和业务关联功能

1. 新增操作日志记录功能,记录关键操作
2. 实现危险操作确认对话框,防止误删
3. 添加业务和库房管理模块
4. 支持设备标记为空闲状态
5. 完善API文档和健康检查
6. 优化前端删除操作的确认流程
7. 添加Swagger API文档支持
8. 实现设备与业务的关联功能
9. 改进设备模型,添加空闲相关字段
10. 优化用户、角色管理操作日志
This commit is contained in:
zhang1106
2026-03-20 17:15:14 +08:00
parent c392df9ce3
commit 73cbe4ac1b
52 changed files with 11149 additions and 1756 deletions
+40
View File
@@ -0,0 +1,40 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const Business = sequelize.define('Business', {
businessId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
status: {
type: DataTypes.ENUM('active', 'offline'),
defaultValue: 'active'
},
offlineDate: {
type: DataTypes.DATE,
allowNull: true
},
offlineReason: {
type: DataTypes.STRING,
allowNull: true
}
}, {
tableName: 'businesses',
timestamps: true,
indexes: [
{ fields: ['status'] },
{ fields: ['name'] }
]
});
module.exports = Business;
+25
View File
@@ -1,5 +1,7 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const Rack = require('./Rack');
const Warehouse = require('./Warehouse');
const Device = sequelize.define('Device', {
deviceId: {
@@ -47,6 +49,26 @@ const Device = sequelize.define('Device', {
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
@@ -81,4 +103,7 @@ const Device = sequelize.define('Device', {
]
});
Device.belongsTo(Rack, { foreignKey: 'rackId' });
Device.belongsTo(Warehouse, { foreignKey: 'warehouseId' });
module.exports = Device;
+57
View File
@@ -0,0 +1,57 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const Device = require('./Device');
const Business = require('./Business');
const DeviceBusiness = sequelize.define('DeviceBusiness', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
deviceId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: Device,
key: 'deviceId'
}
},
businessId: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: Business,
key: 'businessId'
}
},
isPrimary: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
}, {
tableName: 'device_business',
timestamps: true,
indexes: [
{ fields: ['deviceId'] },
{ fields: ['businessId'] },
{ unique: true, fields: ['deviceId', 'businessId'] }
]
});
Device.belongsToMany(Business, {
through: DeviceBusiness,
foreignKey: 'deviceId',
otherKey: 'businessId'
});
Business.belongsToMany(Device, {
through: DeviceBusiness,
foreignKey: 'businessId',
otherKey: 'deviceId'
});
DeviceBusiness.belongsTo(Business, { foreignKey: 'businessId' });
DeviceBusiness.belongsTo(Device, { foreignKey: 'deviceId' });
module.exports = DeviceBusiness;
+89
View File
@@ -0,0 +1,89 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const generateRecordId = () => {
return `OPLOG_${Date.now().toString(36)}_${Math.random().toString(36).substr(2, 9)}`;
};
const OperationLog = sequelize.define('OperationLog', {
recordId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true
},
module: {
type: DataTypes.STRING,
allowNull: false,
comment: '模块:device/user/role/consumable/rack/room'
},
operationType: {
type: DataTypes.STRING,
allowNull: false,
comment: '操作类型: create/update/delete/batch_delete/batch_update/status_change/move/permission_change'
},
operationDescription: {
type: DataTypes.TEXT,
comment: '操作描述'
},
targetId: {
type: DataTypes.STRING,
comment: '目标对象ID'
},
targetName: {
type: DataTypes.STRING,
comment: '目标对象名称(冗余便于展示)'
},
operatorId: {
type: DataTypes.STRING,
allowNull: false,
comment: '操作人ID'
},
operatorName: {
type: DataTypes.STRING,
allowNull: false,
comment: '操作人姓名'
},
operatorRole: {
type: DataTypes.STRING,
comment: '操作人角色'
},
beforeState: {
type: DataTypes.JSON,
comment: '操作前状态'
},
afterState: {
type: DataTypes.JSON,
comment: '操作后状态'
},
result: {
type: DataTypes.STRING,
defaultValue: 'success',
comment: '操作结果: success/failed'
},
ipAddress: {
type: DataTypes.STRING,
comment: 'IP地址'
},
userAgent: {
type: DataTypes.STRING,
comment: '用户代理'
},
metadata: {
type: DataTypes.JSON,
defaultValue: {},
comment: '扩展字段'
}
}, {
tableName: 'operation_logs',
timestamps: true,
indexes: [
{ fields: ['module'] },
{ fields: ['operationType'] },
{ fields: ['targetId'] },
{ fields: ['operatorId'] },
{ fields: ['createdAt'] }
]
});
module.exports = OperationLog;
+41
View File
@@ -0,0 +1,41 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const Warehouse = sequelize.define('Warehouse', {
warehouseId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
location: {
type: DataTypes.STRING,
allowNull: true
},
capacity: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 100
},
status: {
type: DataTypes.ENUM('active', 'inactive'),
defaultValue: 'active'
},
description: {
type: DataTypes.TEXT,
allowNull: true
}
}, {
tableName: 'warehouses',
timestamps: true,
indexes: [
{ fields: ['status'] },
{ fields: ['name'] }
]
});
module.exports = Warehouse;