| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import type { VariablesContext } from "../middlewares.ts";
- export class WorkflowEngine {
- private c: VariablesContext;
- constructor(context: VariablesContext) {
- this.c = context;
- }
- /**
- * 检查状态流转是否允许
- * @param fromStatus 原状态
- * @param toStatus 目标状态
- * @param userRoles 用户角色
- */
- async canTransition(fromStatus: string, toStatus: string, userRoles: string[]): Promise<boolean> {
- const apiClient = this.c.get('apiClient');
- const rule = await apiClient.database.table('work_order_status_rules')
- .where('from_status', fromStatus)
- .where('to_status', toStatus)
- .first();
- if (!rule) return false;
- const allowedRoles = rule.allowed_roles.split(',');
- return userRoles.some(role => allowedRoles.includes(role));
- }
- /**
- * 获取状态流转规则
- * @param fromStatus 原状态
- */
- async getAvailableTransitions(fromStatus: string): Promise<Array<{
- to_status: string;
- allowed_roles: string[];
- need_comment: boolean;
- }>> {
- const apiClient = this.c.get('apiClient');
- return apiClient.database.table('work_order_status_rules')
- .where('from_status', fromStatus)
- .select('to_status', 'allowed_roles', 'need_comment');
- }
- /**
- * 记录状态变更历史
- * @param workOrderId 工单ID
- * @param fromStatus 原状态
- * @param toStatus 目标状态
- * @param userId 操作用户ID
- * @param comment 备注
- */
- async logStatusChange(
- workOrderId: number,
- fromStatus: string,
- toStatus: string,
- userId: number,
- comment?: string
- ): Promise<void> {
- const apiClient = this.c.get('apiClient');
- await apiClient.database.table('work_order_status_history').insert({
- work_order_id: workOrderId,
- from_status: fromStatus,
- to_status: toStatus,
- changed_by: userId,
- comment,
- created_at: apiClient.database.fn.now()
- });
- }
- /**
- * 检查并处理超时状态
- * @param workOrderId 工单ID
- */
- async checkTimeout(workOrderId: number): Promise<void> {
- const apiClient = this.c.get('apiClient');
- const [order] = await apiClient.database.table('work_orders')
- .where('id', workOrderId)
- .select();
- if (!order.deadline || order.status !== 'in_progress') return;
- const now = new Date();
- if (new Date(order.deadline) < now) {
- // 记录超时日志
- await apiClient.database.table('work_order_timeout_logs').insert({
- work_order_id: workOrderId,
- timeout_at: now,
- created_at: now
- });
- // 更新超时计数
- await apiClient.database.table('work_orders')
- .where('id', workOrderId)
- .update({
- timeout_count: apiClient.database.raw('timeout_count + 1'),
- updated_at: now
- });
- // 如果超时超过3次,自动升级
- if (order.timeout_count >= 2) { // 从0开始计数
- await apiClient.database.table('work_orders')
- .where('id', workOrderId)
- .update({
- status: 'escalated',
- escalation_level: apiClient.database.raw('escalation_level + 1'),
- updated_at: now
- });
- await this.logStatusChange(
- workOrderId,
- order.status,
- 'escalated',
- 0, // system user
- '自动升级:工单多次超时'
- );
- }
- }
- }
- }
|