feat(工单系统): 实现完整的工单管理功能

添加工单系统核心功能,包括:
- 工单模型及相关API接口
- 故障分类管理
- 工单操作记录
- 前端工单管理页面
- 统计报表功能
- 测试框架支持

后端新增工单相关路由和模型,前端添加工单管理界面和统计报表
This commit is contained in:
zhang1106
2025-12-25 16:49:00 +08:00
parent a911d02999
commit bfb789dfd9
17 changed files with 9230 additions and 23 deletions
+65
View File
@@ -0,0 +1,65 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const FaultCategory = sequelize.define('FaultCategory', {
categoryId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
comment: '分类名称'
},
description: {
type: DataTypes.TEXT,
comment: '分类说明 - 说明此类故障代表什么问题'
},
priority: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: '排序优先级'
},
defaultPriority: {
type: DataTypes.STRING,
defaultValue: 'medium',
comment: '默认优先级: critical/high/medium/low'
},
expectedDuration: {
type: DataTypes.INTEGER,
comment: '预计处理时长(小时)'
},
solutions: {
type: DataTypes.JSON,
defaultValue: [],
comment: '常见解决方案'
},
isSystem: {
type: DataTypes.BOOLEAN,
defaultValue: false,
comment: '是否系统内置分类'
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
comment: '是否启用'
},
metadata: {
type: DataTypes.JSON,
defaultValue: {},
comment: '扩展字段'
}
}, {
tableName: 'fault_categories',
timestamps: true,
indexes: [
{ fields: ['name'] },
{ fields: ['isActive'] },
{ fields: ['priority'] }
]
});
module.exports = FaultCategory;
+124
View File
@@ -0,0 +1,124 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const User = require('./User');
const Device = require('./Device');
const Ticket = sequelize.define('Ticket', {
ticketId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true
},
title: {
type: DataTypes.STRING,
allowNull: false,
comment: '工单标题'
},
deviceId: {
type: DataTypes.STRING,
allowNull: false,
comment: '关联设备ID'
},
deviceName: {
type: DataTypes.STRING,
allowNull: false,
comment: '设备名称'
},
deviceModel: {
type: DataTypes.STRING,
comment: '设备型号'
},
serialNumber: {
type: DataTypes.STRING,
comment: '设备序列号'
},
faultCategory: {
type: DataTypes.STRING,
allowNull: false,
comment: '故障分类'
},
faultSubCategory: {
type: DataTypes.STRING,
comment: '故障子分类'
},
priority: {
type: DataTypes.STRING,
defaultValue: 'medium',
comment: '优先级: critical/high/medium/low'
},
status: {
type: DataTypes.STRING,
defaultValue: 'pending',
comment: '工单状态: pending/in_progress/completed/closed/cancelled'
},
description: {
type: DataTypes.TEXT,
comment: '故障描述'
},
expectedCompletionDate: {
type: DataTypes.DATE,
comment: '期望完成时间'
},
reporterId: {
type: DataTypes.STRING,
allowNull: false,
comment: '报修人ID'
},
reporterName: {
type: DataTypes.STRING,
allowNull: false,
comment: '报修人姓名'
},
location: {
type: DataTypes.STRING,
comment: '设备位置'
},
resolution: {
type: DataTypes.TEXT,
comment: '解决方案'
},
completionDate: {
type: DataTypes.DATE,
comment: '实际完成时间'
},
evaluation: {
type: DataTypes.TEXT,
comment: '用户评价'
},
evaluationRating: {
type: DataTypes.INTEGER,
comment: '评价星级(1-5)'
},
attachments: {
type: DataTypes.JSON,
defaultValue: [],
comment: '附件列表'
},
tags: {
type: DataTypes.JSON,
defaultValue: [],
comment: '标签'
},
metadata: {
type: DataTypes.JSON,
defaultValue: {},
comment: '扩展字段'
}
}, {
tableName: 'tickets',
timestamps: true,
indexes: [
{ fields: ['deviceId'] },
{ fields: ['status'] },
{ fields: ['faultCategory'] },
{ fields: ['priority'] },
{ fields: ['reporterId'] },
{ fields: ['createdAt'] }
]
});
Ticket.belongsTo(User, { foreignKey: 'reporterId', as: 'reporter', constraints: false });
Ticket.belongsTo(Device, { foreignKey: 'deviceId', constraints: false });
module.exports = Ticket;
+90
View File
@@ -0,0 +1,90 @@
const { DataTypes } = require('sequelize');
const { sequelize } = require('../db');
const TicketOperationRecord = sequelize.define('TicketOperationRecord', {
recordId: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true
},
ticketId: {
type: DataTypes.STRING,
allowNull: false,
comment: '关联工单ID'
},
operationType: {
type: DataTypes.STRING,
allowNull: false,
comment: '操作类型: create/update/status_change/assignment/comment/attachment'
},
operationDescription: {
type: DataTypes.TEXT,
comment: '操作描述'
},
operatorId: {
type: DataTypes.STRING,
allowNull: false,
comment: '操作人ID'
},
operatorName: {
type: DataTypes.STRING,
allowNull: false,
comment: '操作人姓名'
},
operatorRole: {
type: DataTypes.STRING,
comment: '操作人角色'
},
operationSteps: {
type: DataTypes.JSON,
defaultValue: [],
comment: '操作步骤详情'
},
spareParts: {
type: DataTypes.JSON,
defaultValue: [],
comment: '使用的备件列表'
},
beforeState: {
type: DataTypes.JSON,
comment: '操作前状态'
},
afterState: {
type: DataTypes.JSON,
comment: '操作后状态'
},
duration: {
type: DataTypes.INTEGER,
comment: '操作耗时(分钟)'
},
result: {
type: DataTypes.STRING,
comment: '操作结果: success/failed/partial'
},
notes: {
type: DataTypes.TEXT,
comment: '备注信息'
},
attachments: {
type: DataTypes.JSON,
defaultValue: [],
comment: '附件'
},
metadata: {
type: DataTypes.JSON,
defaultValue: {},
comment: '扩展字段'
}
}, {
tableName: 'ticket_operation_records',
timestamps: true,
indexes: [
{ fields: ['ticketId'] },
{ fields: ['operatorId'] },
{ fields: ['operationType'] },
{ fields: ['createdAt'] }
]
});
module.exports = TicketOperationRecord;
+188
View File
@@ -0,0 +1,188 @@
const { sequelize } = require('../db');
const FaultCategory = require('./FaultCategory');
const Ticket = require('./Ticket');
const TicketOperationRecord = require('./TicketOperationRecord');
const User = require('./User');
const Device = require('./Device');
const initDefaultFaultCategories = async () => {
try {
const count = await FaultCategory.count();
if (count > 0) {
console.log('故障分类已存在,跳过初始化');
return;
}
const defaultCategories = [
{
categoryId: 'CAT001',
name: '系统故障',
code: 'system_fault',
description: '操作系统、软件系统相关的故障',
icon: 'DesktopOutlined',
color: '#ff4d4f',
priority: 1,
defaultPriority: 'high',
expectedDuration: 4,
solutions: ['重启服务', '回滚版本', '修复配置', '重装系统'],
isSystem: true
},
{
categoryId: 'CAT002',
name: '硬件故障',
code: 'hardware_fault',
description: '服务器、存储、网络设备等硬件故障',
icon: 'CpuOutlined',
color: '#fa8c16',
priority: 2,
defaultPriority: 'critical',
expectedDuration: 8,
solutions: ['更换部件', '联系厂商', '现场维修'],
isSystem: true
},
{
categoryId: 'CAT003',
name: '网络故障',
code: 'network_fault',
description: '网络连接、交换机、路由器等问题',
icon: 'WifiOutlined',
color: '#1890ff',
priority: 3,
defaultPriority: 'high',
expectedDuration: 2,
solutions: ['检查网线', '重启交换机', '修复配置', '联系运营商'],
isSystem: true
},
{
categoryId: 'CAT004',
name: '软件故障',
code: 'software_fault',
description: '应用程序、Bug、性能问题等',
icon: 'AppstoreOutlined',
color: '#722ed1',
priority: 4,
defaultPriority: 'medium',
expectedDuration: 6,
solutions: ['修复Bug', '优化性能', '更新版本', '配置调整'],
isSystem: true
},
{
categoryId: 'CAT005',
name: '安全事件',
code: 'security_incident',
description: '安全漏洞、入侵检测、权限问题等',
icon: 'SafetyOutlined',
color: '#eb2f96',
priority: 0,
defaultPriority: 'critical',
expectedDuration: 1,
solutions: ['隔离系统', '调查取证', '修复漏洞', '更新安全策略'],
isSystem: true
},
{
categoryId: 'CAT006',
name: '性能问题',
code: 'performance_issue',
description: '响应慢、卡顿、资源耗尽等性能问题',
icon: 'RocketOutlined',
color: '#52c41a',
priority: 5,
defaultPriority: 'medium',
expectedDuration: 4,
solutions: ['资源扩容', '优化SQL', '清理缓存', '负载均衡'],
isSystem: true
},
{
categoryId: 'CAT007',
name: '配置变更',
code: 'config_change',
description: '配置修改、系统调优等需求',
icon: 'SettingOutlined',
color: '#13c2c2',
priority: 6,
defaultPriority: 'low',
expectedDuration: 2,
solutions: ['调整配置', '参数优化', '功能启用'],
isSystem: true
},
{
categoryId: 'CAT008',
name: '例行维护',
code: 'routine_maintenance',
description: '定期维护、巡检、预防性保养',
icon: 'ToolOutlined',
color: '#faad14',
priority: 7,
defaultPriority: 'low',
expectedDuration: 4,
solutions: ['系统更新', '安全检查', '日志清理', '硬件检测'],
isSystem: true
},
{
categoryId: 'CAT009',
name: '数据问题',
code: 'data_issue',
description: '数据丢失、数据错误、数据同步问题',
icon: 'DatabaseOutlined',
color: '#f5222d',
priority: 1,
defaultPriority: 'high',
expectedDuration: 6,
solutions: ['数据恢复', '数据修复', '重新同步', '备份还原'],
isSystem: true
},
{
categoryId: 'CAT010',
name: '电源问题',
code: 'power_issue',
description: '电源故障、UPS、空调等电力系统问题',
icon: 'ThunderboltOutlined',
color: '#fa541c',
priority: 0,
defaultPriority: 'critical',
expectedDuration: 2,
solutions: ['切换电源', '更换UPS', '联系供电', '检查线路'],
isSystem: true
}
];
for (const category of defaultCategories) {
await FaultCategory.create(category);
}
console.log('默认故障分类初始化完成');
} catch (error) {
console.error('初始化故障分类失败:', error);
}
};
const initAssociations = () => {
Ticket.belongsTo(User, { foreignKey: 'assigneeId', as: 'assignee' });
Ticket.hasMany(TicketOperationRecord, { foreignKey: 'ticketId', as: 'operationRecords' });
TicketOperationRecord.belongsTo(Ticket, { foreignKey: 'ticketId' });
};
// 立即初始化关联(在模型导出前)
initAssociations();
const initializeModels = async () => {
try {
await sequelize.sync({ alter: true });
console.log('工单相关数据表同步完成');
initAssociations();
await initDefaultFaultCategories();
console.log('工单系统数据初始化完成');
} catch (error) {
console.error('初始化工单系统数据失败:', error);
}
};
module.exports = {
initializeModels,
Ticket,
TicketOperationRecord,
FaultCategory
};