028_add_workflow_fields.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. const addWorkflowFields: MigrationLiveDefinition = {
  3. name: 'add_workflow_fields',
  4. up: async (api) => {
  5. // 工单表新增字段
  6. await api.schema.alterTable('work_orders', (table) => {
  7. table.timestamp('deadline').nullable().comment('处理截止时间');
  8. table.integer('timeout_count').defaultTo(0).comment('超时次数');
  9. table.integer('escalation_level').defaultTo(0).comment('升级级别');
  10. });
  11. // 状态流转规则表
  12. await api.schema.createTable('work_order_status_rules', (table) => {
  13. table.increments('id').primary();
  14. table.string('from_status').notNullable().comment('原状态');
  15. table.string('to_status').notNullable().comment('目标状态');
  16. table.string('allowed_roles').notNullable().comment('允许操作的角色');
  17. table.boolean('need_comment').defaultTo(false).comment('是否需要备注');
  18. table.timestamps(true, true);
  19. table.index(['from_status', 'to_status']);
  20. });
  21. // 超时日志表
  22. await api.schema.createTable('work_order_timeout_logs', (table) => {
  23. table.increments('id').primary();
  24. table.integer('work_order_id').unsigned().notNullable().comment('工单ID')
  25. .references('id').inTable('work_orders').onDelete('CASCADE');
  26. table.timestamp('timeout_at').notNullable().comment('超时时间');
  27. table.timestamp('notified_at').nullable().comment('提醒时间');
  28. table.timestamp('resolved_at').nullable().comment('解决时间');
  29. table.timestamps(true, true);
  30. table.index('work_order_id');
  31. });
  32. // 初始化状态流转规则
  33. await api.table('work_order_status_rules').insert([
  34. { from_status: 'pending', to_status: 'in_progress', allowed_roles: 'creator,admin', need_comment: true },
  35. { from_status: 'pending', to_status: 'cancelled', allowed_roles: 'creator', need_comment: true },
  36. { from_status: 'in_progress', to_status: 'resolved', allowed_roles: 'assignee', need_comment: true },
  37. { from_status: 'in_progress', to_status: 'pending', allowed_roles: 'assignee', need_comment: true },
  38. { from_status: 'in_progress', to_status: 'escalated', allowed_roles: 'assignee,system', need_comment: true },
  39. { from_status: 'resolved', to_status: 'closed', allowed_roles: 'creator', need_comment: false },
  40. { from_status: 'resolved', to_status: 'reopened', allowed_roles: 'creator', need_comment: true },
  41. { from_status: 'escalated', to_status: 'in_progress', allowed_roles: 'admin', need_comment: true },
  42. { from_status: 'escalated', to_status: 'closed', allowed_roles: 'admin', need_comment: true },
  43. { from_status: 'reopened', to_status: 'in_progress', allowed_roles: 'assignee', need_comment: false },
  44. { from_status: 'reopened', to_status: 'closed', allowed_roles: 'creator', need_comment: false }
  45. ]);
  46. },
  47. down: async (api) => {
  48. await api.schema.dropTable('work_order_timeout_logs');
  49. await api.schema.dropTable('work_order_status_rules');
  50. await api.schema.alterTable('work_orders', (table) => {
  51. table.dropColumn('deadline');
  52. table.dropColumn('timeout_count');
  53. table.dropColumn('escalation_level');
  54. });
  55. }
  56. };
  57. export default addWorkflowFields