workflow_approval/wwwroot/api/instance_create.dspy
yumoqing 5472211972 sync: local modifications to workflow_approval
- Updated all model JSON files: approval_instance, approval_step, approval_task, approval_workflow
- Updated init.py, mysql.ddl.sql, mobile_base.ui
- Added __init__.py
- Added API files: instance CRUD, step CRUD, task approve/reject/list, workflow CRUD
- Added UI files: base.ui, approval_instance.ui, approval_task.ui, approval_workflow.ui
2026-04-28 18:55:29 +08:00

61 lines
3.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Create approval instance"""
import json, time, uuid
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}}
try:
workflow_id = params_kw.get('workflow_id', '')
module_type = params_kw.get('module_type', '')
module_record_id = params_kw.get('module_record_id', '')
title = params_kw.get('title', '')
description = params_kw.get('description', '')
if not workflow_id or not module_type or not module_record_id or not title:
result['options'] = {'title': 'Error', 'message': '工作流ID、模块类型、记录ID和标题不能为空', 'type': 'error'}
else:
dbname = get_module_dbname('workflow_approval')
new_id = str(uuid.uuid4()).replace('-', '')[:32]
now = time.strftime('%Y-%m-%d %H:%M:%S')
org_id = '0'
initiator_id = '0'
async with DBPools().sqlorContext(dbname) as sor:
workflows = await sor.sqlExe("SELECT id FROM approval_workflow WHERE id=${id}$ AND status='Y'", {'id': workflow_id})
if not workflows:
result['options'] = {'title': 'Error', 'message': '工作流不存在或未激活', 'type': 'error'}
return json.dumps(result, ensure_ascii=False)
first_step = await sor.sqlExe("SELECT id, step_number FROM approval_step WHERE workflow_id=${workflow_id}$ ORDER BY step_number ASC LIMIT 1", {'workflow_id': workflow_id})
current_step_num = first_step[0]['step_number'] if first_step else 0
first_step_id = first_step[0]['id'] if first_step else ''
await sor.sqlExe("""
INSERT INTO approval_instance (id, workflow_id, business_type, business_id, current_step, status, initiator_id, org_id, initiated_at)
VALUES (${id}$, ${workflow_id}$, ${business_type}$, ${business_id}$, ${current_step}$, 'pending', ${initiator_id}$, ${org_id}$, ${initiated_at}$)
""", {
'id': new_id, 'workflow_id': workflow_id, 'business_type': module_type,
'business_id': module_record_id, 'current_step': current_step_num,
'initiator_id': initiator_id, 'org_id': org_id, 'initiated_at': now
})
if first_step_id:
task_id = str(uuid.uuid4()).replace('-', '')[:32]
async with DBPools().sqlorContext(dbname) as sor2:
await sor2.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': task_id, 'instance_id': new_id, 'step_id': first_step_id,
'assignee_id': initiator_id,
'org_id': org_id, 'assigned_at': now
})
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)