026_createWorkOrderTables.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus } from "../../client/share/types.ts";
  3. // 工单相关表迁移
  4. const createWorkOrderTables: MigrationLiveDefinition = {
  5. name: 'create_work_order_tables',
  6. up: async (api) => {
  7. await api.schema.createTable('work_orders', (table) => {
  8. table.increments('id').primary();
  9. table.string('title').notNullable().comment('工单标题');
  10. table.text('description').nullable().comment('工单描述');
  11. table.string('status').notNullable().defaultTo('pending').comment('工单状态');
  12. table.string('priority').notNullable().defaultTo('medium').comment('优先级');
  13. table.integer('alert_id').unsigned().nullable().comment('关联的告警ID')
  14. .references('id').inTable('device_alerts').onDelete('SET NULL');
  15. table.integer('creator_id').unsigned().notNullable().comment('创建人ID')
  16. .references('id').inTable('users').onDelete('CASCADE');
  17. table.integer('assignee_id').unsigned().nullable().comment('当前处理人ID')
  18. .references('id').inTable('users').onDelete('SET NULL');
  19. table.integer('previous_assignee_id').unsigned().nullable().comment('上一处理人ID')
  20. .references('id').inTable('users').onDelete('SET NULL');
  21. table.timestamps(true, true);
  22. table.index('status');
  23. table.index('priority');
  24. table.index('alert_id');
  25. table.index('creator_id');
  26. table.index('assignee_id');
  27. });
  28. await api.schema.createTable('work_order_flows', (table) => {
  29. table.increments('id').primary();
  30. table.integer('work_order_id').unsigned().notNullable().comment('关联的工单ID')
  31. .references('id').inTable('work_orders').onDelete('CASCADE');
  32. table.string('from_status').notNullable().comment('原状态');
  33. table.string('to_status').notNullable().comment('新状态');
  34. table.integer('operator_id').unsigned().notNullable().comment('操作人ID')
  35. .references('id').inTable('users').onDelete('CASCADE');
  36. table.text('comment').nullable().comment('操作备注');
  37. table.timestamps(true, true);
  38. table.index('work_order_id');
  39. table.index('operator_id');
  40. table.index(['from_status', 'to_status']);
  41. });
  42. await api.schema.createTable('work_order_settings', (table) => {
  43. table.increments('id').primary();
  44. table.string('name').notNullable().comment('配置名称');
  45. table.jsonb('flow_config').notNullable().comment('流程配置');
  46. table.timestamps(true, true);
  47. table.index('name');
  48. });
  49. },
  50. down: async (api) => {
  51. await api.schema.dropTable('work_order_settings');
  52. await api.schema.dropTable('work_order_flows');
  53. await api.schema.dropTable('work_orders');
  54. }
  55. };
  56. export default createWorkOrderTables