| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
- const addWorkflowFields: MigrationLiveDefinition = {
- name: 'add_workflow_fields',
- up: async (api) => {
- // 工单表新增字段
- await api.schema.alterTable('work_orders', (table) => {
- table.timestamp('deadline').nullable().comment('处理截止时间');
- table.integer('timeout_count').defaultTo(0).comment('超时次数');
- table.integer('escalation_level').defaultTo(0).comment('升级级别');
- });
- // 状态流转规则表
- await api.schema.createTable('work_order_status_rules', (table) => {
- table.increments('id').primary();
- table.string('from_status').notNullable().comment('原状态');
- table.string('to_status').notNullable().comment('目标状态');
- table.string('allowed_roles').notNullable().comment('允许操作的角色');
- table.boolean('need_comment').defaultTo(false).comment('是否需要备注');
- table.timestamps(true, true);
-
- table.index(['from_status', 'to_status']);
- });
- // 超时日志表
- await api.schema.createTable('work_order_timeout_logs', (table) => {
- table.increments('id').primary();
- table.integer('work_order_id').unsigned().notNullable().comment('工单ID')
- .references('id').inTable('work_orders').onDelete('CASCADE');
- table.timestamp('timeout_at').notNullable().comment('超时时间');
- table.timestamp('notified_at').nullable().comment('提醒时间');
- table.timestamp('resolved_at').nullable().comment('解决时间');
- table.timestamps(true, true);
-
- table.index('work_order_id');
- });
- // 初始化状态流转规则
- await api.table('work_order_status_rules').insert([
- { from_status: 'pending', to_status: 'in_progress', allowed_roles: 'creator,admin', need_comment: true },
- { from_status: 'pending', to_status: 'cancelled', allowed_roles: 'creator', need_comment: true },
- { from_status: 'in_progress', to_status: 'resolved', allowed_roles: 'assignee', need_comment: true },
- { from_status: 'in_progress', to_status: 'pending', allowed_roles: 'assignee', need_comment: true },
- { from_status: 'in_progress', to_status: 'escalated', allowed_roles: 'assignee,system', need_comment: true },
- { from_status: 'resolved', to_status: 'closed', allowed_roles: 'creator', need_comment: false },
- { from_status: 'resolved', to_status: 'reopened', allowed_roles: 'creator', need_comment: true },
- { from_status: 'escalated', to_status: 'in_progress', allowed_roles: 'admin', need_comment: true },
- { from_status: 'escalated', to_status: 'closed', allowed_roles: 'admin', need_comment: true },
- { from_status: 'reopened', to_status: 'in_progress', allowed_roles: 'assignee', need_comment: false },
- { from_status: 'reopened', to_status: 'closed', allowed_roles: 'creator', need_comment: false }
- ]);
- },
- down: async (api) => {
- await api.schema.dropTable('work_order_timeout_logs');
- await api.schema.dropTable('work_order_status_rules');
- await api.schema.alterTable('work_orders', (table) => {
- table.dropColumn('deadline');
- table.dropColumn('timeout_count');
- table.dropColumn('escalation_level');
- });
- }
- };
- export default addWorkflowFields
|