#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Approve an approval task""" import json, time, uuid result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}} try: task_id = params_kw.get('id', '') decision = params_kw.get('decision', '') if not task_id: result['options'] = {'title': 'Error', 'message': '任务ID不能为空', 'type': 'error'} else: dbname = get_module_dbname('workflow_approval') now = time.strftime('%Y-%m-%d %H:%M:%S') org_id = '0' async with DBPools().sqlorContext(dbname) as sor: tasks = await sor.sqlExe("SELECT * FROM approval_task WHERE id=${id}$ AND status='pending'", {'id': task_id}) if not tasks: result['options'] = {'title': 'Error', 'message': '任务不存在或已处理', 'type': 'error'} return json.dumps(result, ensure_ascii=False) task = tasks[0] instance_id = task['instance_id'] step_id = task['step_id'] await sor.sqlExe(""" UPDATE approval_task SET status='approved', comment=${comment}$, completed_at=${completed_at}$ WHERE id=${id}$ """, {'id': task_id, 'comment': decision, 'completed_at': now}) pending = await sor.sqlExe("SELECT id FROM approval_task WHERE instance_id=${instance_id}$ AND step_id=${step_id}$ AND status='pending'", {'instance_id': instance_id, 'step_id': step_id}) if not pending: current_step_order = await sor.sqlExe(""" SELECT step_number FROM approval_step WHERE id=${step_id}$ """, {'step_id': step_id}) if current_step_order: order = current_step_order[0]['step_number'] next_step = await sor.sqlExe(""" SELECT id, approver_id FROM approval_step WHERE workflow_id=(SELECT workflow_id FROM approval_step WHERE id=${step_id}$) AND step_number > ${order}$ ORDER BY step_number ASC LIMIT 1 """, {'step_id': step_id, 'order': order}) if next_step: ns = next_step[0] new_task_id = str(uuid.uuid4()).replace('-', '')[:32] await sor.sqlExe(""" UPDATE approval_instance SET current_step=${step_id}$ WHERE id=${instance_id}$ """, {'step_id': ns['id'], 'instance_id': instance_id}) await sor.sqlExe(""" INSERT INTO approval_task (id, instance_id, step_id, assignee_id, status, org_id, assigned_at) VALUES (${id}$, ${instance_id}$, ${step_id}$, ${assignee_id}$, 'pending', ${org_id}$, ${assigned_at}$) """, { 'id': new_task_id, 'instance_id': instance_id, 'step_id': ns['id'], 'assignee_id': ns.get('approver_id', '0'), 'org_id': org_id, 'assigned_at': now }) else: await sor.sqlExe(""" UPDATE approval_instance SET status='approved', completed_at=${completed_at}$ WHERE id=${instance_id}$ """, {'completed_at': now, 'instance_id': instance_id}) else: await sor.sqlExe(""" UPDATE approval_instance SET status='approved', completed_at=${completed_at}$ WHERE id=${instance_id}$ """, {'completed_at': now, 'instance_id': instance_id}) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '审批通过', 'type': 'success'}} except Exception as e: result['options'] = {'title': 'Error', 'message': f'审批失败: {str(e)}', 'type': 'error'} return json.dumps(result, ensure_ascii=False)