feat(workflow): add workflow engine module (Phase 4)
Implement complete workflow engine with BPMN subset support: Backend (erp-workflow crate): - Token-driven execution engine with exclusive/parallel gateway support - BPMN parser with flow graph validation - Expression evaluator for conditional branching - Process definition CRUD with draft/publish lifecycle - Process instance management (start, suspend, terminate) - Task service (pending, complete, delegate) - PostgreSQL advisory locks for concurrent safety - 5 database tables: process_definitions, process_instances, tokens, tasks, process_variables - 13 API endpoints with RBAC protection - Timeout checker framework (placeholder) Frontend: - Workflow page with 4 tabs (definitions, pending, completed, monitor) - React Flow visual process designer (@xyflow/react) - Process viewer with active node highlighting - 3 API client modules for workflow endpoints - Sidebar menu integration
This commit is contained in:
269
crates/erp-workflow/src/service/definition_service.rs
Normal file
269
crates/erp-workflow/src/service/definition_service.rs
Normal file
@@ -0,0 +1,269 @@
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{
|
||||
CreateProcessDefinitionReq, ProcessDefinitionResp, UpdateProcessDefinitionReq,
|
||||
};
|
||||
use crate::engine::parser;
|
||||
use crate::entity::process_definition;
|
||||
use crate::error::{WorkflowError, WorkflowResult};
|
||||
use erp_core::events::EventBus;
|
||||
use erp_core::types::Pagination;
|
||||
|
||||
/// 流程定义 CRUD 服务。
|
||||
pub struct DefinitionService;
|
||||
|
||||
impl DefinitionService {
|
||||
/// 分页查询流程定义列表。
|
||||
pub async fn list(
|
||||
tenant_id: Uuid,
|
||||
pagination: &Pagination,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<(Vec<ProcessDefinitionResp>, u64)> {
|
||||
let paginator = process_definition::Entity::find()
|
||||
.filter(process_definition::Column::TenantId.eq(tenant_id))
|
||||
.filter(process_definition::Column::DeletedAt.is_null())
|
||||
.paginate(db, pagination.limit());
|
||||
|
||||
let total = paginator
|
||||
.num_items()
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let page_index = pagination.page.unwrap_or(1).saturating_sub(1) as u64;
|
||||
let models = paginator
|
||||
.fetch_page(page_index)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let resps: Vec<ProcessDefinitionResp> = models.iter().map(Self::model_to_resp).collect();
|
||||
Ok((resps, total))
|
||||
}
|
||||
|
||||
/// 获取单个流程定义。
|
||||
pub async fn get_by_id(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<ProcessDefinitionResp> {
|
||||
let model = process_definition::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|m| m.tenant_id == tenant_id && m.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程定义不存在: {id}")))?;
|
||||
|
||||
Ok(Self::model_to_resp(&model))
|
||||
}
|
||||
|
||||
/// 创建流程定义。
|
||||
pub async fn create(
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
req: &CreateProcessDefinitionReq,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> WorkflowResult<ProcessDefinitionResp> {
|
||||
// 验证流程图合法性
|
||||
parser::parse_and_validate(&req.nodes, &req.edges)?;
|
||||
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
let nodes_json = serde_json::to_value(&req.nodes)
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
let edges_json = serde_json::to_value(&req.edges)
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let model = process_definition::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
name: Set(req.name.clone()),
|
||||
key: Set(req.key.clone()),
|
||||
version: Set(1),
|
||||
category: Set(req.category.clone()),
|
||||
description: Set(req.description.clone()),
|
||||
nodes: Set(nodes_json),
|
||||
edges: Set(edges_json),
|
||||
status: Set("draft".to_string()),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(operator_id),
|
||||
updated_by: Set(operator_id),
|
||||
deleted_at: Set(None),
|
||||
};
|
||||
model
|
||||
.insert(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
event_bus.publish(erp_core::events::DomainEvent::new(
|
||||
"process_definition.created",
|
||||
tenant_id,
|
||||
serde_json::json!({ "definition_id": id, "key": req.key }),
|
||||
));
|
||||
|
||||
Ok(ProcessDefinitionResp {
|
||||
id,
|
||||
name: req.name.clone(),
|
||||
key: req.key.clone(),
|
||||
version: 1,
|
||||
category: req.category.clone(),
|
||||
description: req.description.clone(),
|
||||
nodes: serde_json::to_value(&req.nodes).unwrap_or_default(),
|
||||
edges: serde_json::to_value(&req.edges).unwrap_or_default(),
|
||||
status: "draft".to_string(),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})
|
||||
}
|
||||
|
||||
/// 更新流程定义(仅 draft 状态可编辑)。
|
||||
pub async fn update(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
req: &UpdateProcessDefinitionReq,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<ProcessDefinitionResp> {
|
||||
let model = process_definition::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|m| m.tenant_id == tenant_id && m.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程定义不存在: {id}")))?;
|
||||
|
||||
if model.status != "draft" {
|
||||
return Err(WorkflowError::InvalidState(
|
||||
"只有 draft 状态的流程定义可以编辑".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let mut active: process_definition::ActiveModel = model.into();
|
||||
|
||||
if let Some(name) = &req.name {
|
||||
active.name = Set(name.clone());
|
||||
}
|
||||
if let Some(category) = &req.category {
|
||||
active.category = Set(Some(category.clone()));
|
||||
}
|
||||
if let Some(description) = &req.description {
|
||||
active.description = Set(Some(description.clone()));
|
||||
}
|
||||
if let Some(nodes) = &req.nodes {
|
||||
// 验证新流程图
|
||||
if let Some(edges) = &req.edges {
|
||||
parser::parse_and_validate(nodes, edges)?;
|
||||
}
|
||||
let nodes_json = serde_json::to_value(nodes)
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
active.nodes = Set(nodes_json);
|
||||
}
|
||||
if let Some(edges) = &req.edges {
|
||||
let edges_json = serde_json::to_value(edges)
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
active.edges = Set(edges_json);
|
||||
}
|
||||
|
||||
active.updated_at = Set(Utc::now());
|
||||
active.updated_by = Set(operator_id);
|
||||
|
||||
let updated = active
|
||||
.update(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(Self::model_to_resp(&updated))
|
||||
}
|
||||
|
||||
/// 发布流程定义(draft → published)。
|
||||
pub async fn publish(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> WorkflowResult<ProcessDefinitionResp> {
|
||||
let model = process_definition::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|m| m.tenant_id == tenant_id && m.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程定义不存在: {id}")))?;
|
||||
|
||||
if model.status != "draft" {
|
||||
return Err(WorkflowError::InvalidState(
|
||||
"只有 draft 状态的流程定义可以发布".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
// 验证流程图
|
||||
let nodes: Vec<crate::dto::NodeDef> = serde_json::from_value(model.nodes.clone())
|
||||
.map_err(|e| WorkflowError::InvalidDiagram(format!("节点数据无效: {e}")))?;
|
||||
let edges: Vec<crate::dto::EdgeDef> = serde_json::from_value(model.edges.clone())
|
||||
.map_err(|e| WorkflowError::InvalidDiagram(format!("连线数据无效: {e}")))?;
|
||||
parser::parse_and_validate(&nodes, &edges)?;
|
||||
|
||||
let mut active: process_definition::ActiveModel = model.into();
|
||||
active.status = Set("published".to_string());
|
||||
active.updated_at = Set(Utc::now());
|
||||
active.updated_by = Set(operator_id);
|
||||
|
||||
let updated = active
|
||||
.update(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
event_bus.publish(erp_core::events::DomainEvent::new(
|
||||
"process_definition.published",
|
||||
tenant_id,
|
||||
serde_json::json!({ "definition_id": id }),
|
||||
));
|
||||
|
||||
Ok(Self::model_to_resp(&updated))
|
||||
}
|
||||
|
||||
/// 软删除流程定义。
|
||||
pub async fn delete(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<()> {
|
||||
let model = process_definition::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|m| m.tenant_id == tenant_id && m.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程定义不存在: {id}")))?;
|
||||
|
||||
let mut active: process_definition::ActiveModel = model.into();
|
||||
active.deleted_at = Set(Some(Utc::now()));
|
||||
active.updated_at = Set(Utc::now());
|
||||
active.updated_by = Set(operator_id);
|
||||
active
|
||||
.update(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn model_to_resp(m: &process_definition::Model) -> ProcessDefinitionResp {
|
||||
ProcessDefinitionResp {
|
||||
id: m.id,
|
||||
name: m.name.clone(),
|
||||
key: m.key.clone(),
|
||||
version: m.version,
|
||||
category: m.category.clone(),
|
||||
description: m.description.clone(),
|
||||
nodes: m.nodes.clone(),
|
||||
edges: m.edges.clone(),
|
||||
status: m.status.clone(),
|
||||
created_at: m.created_at,
|
||||
updated_at: m.updated_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
353
crates/erp-workflow/src/service/instance_service.rs
Normal file
353
crates/erp-workflow/src/service/instance_service.rs
Normal file
@@ -0,0 +1,353 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, Set,
|
||||
TransactionTrait, ConnectionTrait,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{ProcessInstanceResp, StartInstanceReq, TokenResp};
|
||||
use crate::engine::executor::FlowExecutor;
|
||||
use crate::engine::parser;
|
||||
use crate::entity::{process_definition, process_instance, process_variable, token};
|
||||
use crate::error::{WorkflowError, WorkflowResult};
|
||||
use erp_core::events::EventBus;
|
||||
use erp_core::types::Pagination;
|
||||
|
||||
/// 流程实例服务。
|
||||
pub struct InstanceService;
|
||||
|
||||
impl InstanceService {
|
||||
/// 启动流程实例。
|
||||
pub async fn start(
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
req: &StartInstanceReq,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> WorkflowResult<ProcessInstanceResp> {
|
||||
// 查找流程定义
|
||||
let definition = process_definition::Entity::find_by_id(req.definition_id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|d| d.tenant_id == tenant_id && d.deleted_at.is_none())
|
||||
.ok_or_else(|| {
|
||||
WorkflowError::NotFound(format!("流程定义不存在: {}", req.definition_id))
|
||||
})?;
|
||||
|
||||
if definition.status != "published" {
|
||||
return Err(WorkflowError::InvalidState(
|
||||
"只能启动已发布的流程定义".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
// 解析流程图
|
||||
let nodes: Vec<crate::dto::NodeDef> = serde_json::from_value(definition.nodes.clone())
|
||||
.map_err(|e| WorkflowError::InvalidDiagram(format!("节点数据无效: {e}")))?;
|
||||
let edges: Vec<crate::dto::EdgeDef> = serde_json::from_value(definition.edges.clone())
|
||||
.map_err(|e| WorkflowError::InvalidDiagram(format!("连线数据无效: {e}")))?;
|
||||
let graph = parser::parse_and_validate(&nodes, &edges)?;
|
||||
|
||||
// 准备流程变量
|
||||
let mut variables = HashMap::new();
|
||||
if let Some(vars) = &req.variables {
|
||||
for v in vars {
|
||||
let var_type = v.var_type.as_deref().unwrap_or("string");
|
||||
variables.insert(v.name.clone(), v.value.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let instance_id = Uuid::now_v7();
|
||||
let now = Utc::now();
|
||||
|
||||
// 在事务中创建实例、变量和 token
|
||||
let instance_id_clone = instance_id;
|
||||
let tenant_id_clone = tenant_id;
|
||||
let operator_id_clone = operator_id;
|
||||
let business_key = req.business_key.clone();
|
||||
let definition_id = definition.id;
|
||||
let definition_name = definition.name.clone();
|
||||
let vars_to_save = req.variables.clone();
|
||||
|
||||
db.transaction::<_, (), WorkflowError>(|txn| {
|
||||
let graph = graph.clone();
|
||||
let variables = variables.clone();
|
||||
Box::pin(async move {
|
||||
// 创建流程实例
|
||||
let instance = process_instance::ActiveModel {
|
||||
id: Set(instance_id_clone),
|
||||
tenant_id: Set(tenant_id_clone),
|
||||
definition_id: Set(definition_id),
|
||||
business_key: Set(business_key),
|
||||
status: Set("running".to_string()),
|
||||
started_by: Set(operator_id_clone),
|
||||
started_at: Set(now),
|
||||
completed_at: Set(None),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(operator_id_clone),
|
||||
updated_by: Set(operator_id_clone),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
instance.insert(txn).await.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
// 保存初始变量
|
||||
if let Some(vars) = vars_to_save {
|
||||
for v in vars {
|
||||
Self::save_variable(
|
||||
instance_id_clone,
|
||||
tenant_id_clone,
|
||||
&v.name,
|
||||
v.var_type.as_deref().unwrap_or("string"),
|
||||
&v.value,
|
||||
txn,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
// 启动执行引擎
|
||||
FlowExecutor::start(
|
||||
instance_id_clone,
|
||||
tenant_id_clone,
|
||||
&graph,
|
||||
&variables,
|
||||
txn,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
event_bus.publish(erp_core::events::DomainEvent::new(
|
||||
"process_instance.started",
|
||||
tenant_id,
|
||||
serde_json::json!({ "instance_id": instance_id, "definition_id": definition.id }),
|
||||
));
|
||||
|
||||
// 查询创建后的实例(包含 token)
|
||||
let instance = process_instance::Entity::find_by_id(instance_id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程实例不存在: {instance_id}")))?;
|
||||
|
||||
let active_tokens = Self::get_active_tokens(instance_id, db).await?;
|
||||
|
||||
Ok(ProcessInstanceResp {
|
||||
id: instance.id,
|
||||
definition_id: instance.definition_id,
|
||||
definition_name: Some(definition_name),
|
||||
business_key: instance.business_key,
|
||||
status: instance.status,
|
||||
started_by: instance.started_by,
|
||||
started_at: instance.started_at,
|
||||
completed_at: instance.completed_at,
|
||||
created_at: instance.created_at,
|
||||
active_tokens,
|
||||
})
|
||||
}
|
||||
|
||||
/// 分页查询流程实例。
|
||||
pub async fn list(
|
||||
tenant_id: Uuid,
|
||||
pagination: &Pagination,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<(Vec<ProcessInstanceResp>, u64)> {
|
||||
let paginator = process_instance::Entity::find()
|
||||
.filter(process_instance::Column::TenantId.eq(tenant_id))
|
||||
.filter(process_instance::Column::DeletedAt.is_null())
|
||||
.paginate(db, pagination.limit());
|
||||
|
||||
let total = paginator
|
||||
.num_items()
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let page_index = pagination.page.unwrap_or(1).saturating_sub(1) as u64;
|
||||
let models = paginator
|
||||
.fetch_page(page_index)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let mut resps = Vec::new();
|
||||
for m in &models {
|
||||
let active_tokens = Self::get_active_tokens(m.id, db).await.unwrap_or_default();
|
||||
let def_name = process_definition::Entity::find_by_id(m.definition_id)
|
||||
.one(db)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|d| d.name);
|
||||
resps.push(ProcessInstanceResp {
|
||||
id: m.id,
|
||||
definition_id: m.definition_id,
|
||||
definition_name: def_name,
|
||||
business_key: m.business_key.clone(),
|
||||
status: m.status.clone(),
|
||||
started_by: m.started_by,
|
||||
started_at: m.started_at,
|
||||
completed_at: m.completed_at,
|
||||
created_at: m.created_at,
|
||||
active_tokens,
|
||||
});
|
||||
}
|
||||
|
||||
Ok((resps, total))
|
||||
}
|
||||
|
||||
/// 获取单个流程实例详情。
|
||||
pub async fn get_by_id(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<ProcessInstanceResp> {
|
||||
let instance = process_instance::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|i| i.tenant_id == tenant_id && i.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程实例不存在: {id}")))?;
|
||||
|
||||
let def_name = process_definition::Entity::find_by_id(instance.definition_id)
|
||||
.one(db)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|d| d.name);
|
||||
|
||||
let active_tokens = Self::get_active_tokens(id, db).await?;
|
||||
|
||||
Ok(ProcessInstanceResp {
|
||||
id: instance.id,
|
||||
definition_id: instance.definition_id,
|
||||
definition_name: def_name,
|
||||
business_key: instance.business_key,
|
||||
status: instance.status,
|
||||
started_by: instance.started_by,
|
||||
started_at: instance.started_at,
|
||||
completed_at: instance.completed_at,
|
||||
created_at: instance.created_at,
|
||||
active_tokens,
|
||||
})
|
||||
}
|
||||
|
||||
/// 挂起流程实例。
|
||||
pub async fn suspend(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<()> {
|
||||
Self::change_status(id, tenant_id, operator_id, "running", "suspended", db).await
|
||||
}
|
||||
|
||||
/// 终止流程实例。
|
||||
pub async fn terminate(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<()> {
|
||||
Self::change_status(id, tenant_id, operator_id, "running", "terminated", db).await
|
||||
}
|
||||
|
||||
async fn change_status(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
from_status: &str,
|
||||
to_status: &str,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<()> {
|
||||
let instance = process_instance::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|i| i.tenant_id == tenant_id && i.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程实例不存在: {id}")))?;
|
||||
|
||||
if instance.status != from_status {
|
||||
return Err(WorkflowError::InvalidState(format!(
|
||||
"流程实例状态不是 {},无法变更为 {}",
|
||||
from_status, to_status
|
||||
)));
|
||||
}
|
||||
|
||||
let mut active: process_instance::ActiveModel = instance.into();
|
||||
active.status = Set(to_status.to_string());
|
||||
active.updated_at = Set(Utc::now());
|
||||
active.updated_by = Set(operator_id);
|
||||
active
|
||||
.update(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取实例的活跃 token 列表。
|
||||
pub async fn get_active_tokens(
|
||||
instance_id: Uuid,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<Vec<TokenResp>> {
|
||||
let tokens = token::Entity::find()
|
||||
.filter(token::Column::InstanceId.eq(instance_id))
|
||||
.filter(token::Column::Status.eq("active"))
|
||||
.all(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(tokens
|
||||
.iter()
|
||||
.map(|t| TokenResp {
|
||||
id: t.id,
|
||||
node_id: t.node_id.clone(),
|
||||
status: t.status.clone(),
|
||||
created_at: t.created_at,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// 保存流程变量。
|
||||
pub async fn save_variable(
|
||||
instance_id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
name: &str,
|
||||
var_type: &str,
|
||||
value: &serde_json::Value,
|
||||
txn: &impl ConnectionTrait,
|
||||
) -> WorkflowResult<()> {
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
let (value_string, value_number, value_boolean, value_date): (Option<String>, Option<f64>, Option<bool>, Option<chrono::DateTime<Utc>>) = match var_type {
|
||||
"string" => (value.as_str().map(|s| s.to_string()), None, None, None),
|
||||
"number" => (None, value.as_f64(), None, None),
|
||||
"boolean" => (None, None, value.as_bool(), None),
|
||||
_ => (Some(value.to_string()), None, None, None),
|
||||
};
|
||||
|
||||
let model = process_variable::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
instance_id: Set(instance_id),
|
||||
name: Set(name.to_string()),
|
||||
var_type: Set(var_type.to_string()),
|
||||
value_string: Set(value_string),
|
||||
value_number: Set(value_number),
|
||||
value_boolean: Set(value_boolean),
|
||||
value_date: Set(None),
|
||||
};
|
||||
model
|
||||
.insert(txn)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
3
crates/erp-workflow/src/service/mod.rs
Normal file
3
crates/erp-workflow/src/service/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod definition_service;
|
||||
pub mod instance_service;
|
||||
pub mod task_service;
|
||||
336
crates/erp-workflow/src/service/task_service.rs
Normal file
336
crates/erp-workflow/src/service/task_service.rs
Normal file
@@ -0,0 +1,336 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, Set,
|
||||
TransactionTrait,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::{CompleteTaskReq, DelegateTaskReq, TaskResp};
|
||||
use crate::engine::executor::FlowExecutor;
|
||||
use crate::engine::parser;
|
||||
use crate::entity::{process_definition, process_instance, task};
|
||||
use crate::error::{WorkflowError, WorkflowResult};
|
||||
use erp_core::events::EventBus;
|
||||
use erp_core::types::Pagination;
|
||||
|
||||
/// 任务服务。
|
||||
pub struct TaskService;
|
||||
|
||||
impl TaskService {
|
||||
/// 查询当前用户的待办任务。
|
||||
pub async fn list_pending(
|
||||
tenant_id: Uuid,
|
||||
assignee_id: Uuid,
|
||||
pagination: &Pagination,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<(Vec<TaskResp>, u64)> {
|
||||
let paginator = task::Entity::find()
|
||||
.filter(task::Column::TenantId.eq(tenant_id))
|
||||
.filter(task::Column::AssigneeId.eq(assignee_id))
|
||||
.filter(task::Column::Status.eq("pending"))
|
||||
.filter(task::Column::DeletedAt.is_null())
|
||||
.paginate(db, pagination.limit());
|
||||
|
||||
let total = paginator
|
||||
.num_items()
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let page_index = pagination.page.unwrap_or(1).saturating_sub(1) as u64;
|
||||
let models = paginator
|
||||
.fetch_page(page_index)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let mut resps = Vec::new();
|
||||
for m in &models {
|
||||
let mut resp = Self::model_to_resp(m);
|
||||
// 附加实例信息
|
||||
if let Some(inst) = process_instance::Entity::find_by_id(m.instance_id)
|
||||
.one(db)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
{
|
||||
resp.business_key = inst.business_key;
|
||||
if let Some(def) = process_definition::Entity::find_by_id(inst.definition_id)
|
||||
.one(db)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
{
|
||||
resp.definition_name = Some(def.name);
|
||||
}
|
||||
}
|
||||
resps.push(resp);
|
||||
}
|
||||
|
||||
Ok((resps, total))
|
||||
}
|
||||
|
||||
/// 查询当前用户的已办任务。
|
||||
pub async fn list_completed(
|
||||
tenant_id: Uuid,
|
||||
assignee_id: Uuid,
|
||||
pagination: &Pagination,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<(Vec<TaskResp>, u64)> {
|
||||
let paginator = task::Entity::find()
|
||||
.filter(task::Column::TenantId.eq(tenant_id))
|
||||
.filter(task::Column::AssigneeId.eq(assignee_id))
|
||||
.filter(task::Column::Status.is_in(["approved", "rejected", "delegated"]))
|
||||
.filter(task::Column::DeletedAt.is_null())
|
||||
.paginate(db, pagination.limit());
|
||||
|
||||
let total = paginator
|
||||
.num_items()
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let page_index = pagination.page.unwrap_or(1).saturating_sub(1) as u64;
|
||||
let models = paginator
|
||||
.fetch_page(page_index)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
let mut resps = Vec::new();
|
||||
for m in &models {
|
||||
let mut resp = Self::model_to_resp(m);
|
||||
if let Some(inst) = process_instance::Entity::find_by_id(m.instance_id)
|
||||
.one(db)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
{
|
||||
resp.business_key = inst.business_key;
|
||||
if let Some(def) = process_definition::Entity::find_by_id(inst.definition_id)
|
||||
.one(db)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
{
|
||||
resp.definition_name = Some(def.name);
|
||||
}
|
||||
}
|
||||
resps.push(resp);
|
||||
}
|
||||
|
||||
Ok((resps, total))
|
||||
}
|
||||
|
||||
/// 完成任务:更新任务状态 + 推进 token。
|
||||
pub async fn complete(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
req: &CompleteTaskReq,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> WorkflowResult<TaskResp> {
|
||||
let task_model = task::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|t| t.tenant_id == tenant_id && t.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("任务不存在: {id}")))?;
|
||||
|
||||
if task_model.status != "pending" {
|
||||
return Err(WorkflowError::InvalidState(
|
||||
"任务状态不是 pending,无法完成".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let instance_id = task_model.instance_id;
|
||||
let token_id = task_model.token_id;
|
||||
|
||||
// 获取流程定义和流程图
|
||||
let instance = process_instance::Entity::find_by_id(instance_id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|i| i.tenant_id == tenant_id && i.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("流程实例不存在: {instance_id}")))?;
|
||||
|
||||
let definition = process_definition::Entity::find_by_id(instance.definition_id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|d| d.tenant_id == tenant_id && d.deleted_at.is_none())
|
||||
.ok_or_else(|| {
|
||||
WorkflowError::NotFound(format!("流程定义不存在: {}", instance.definition_id))
|
||||
})?;
|
||||
|
||||
let nodes: Vec<crate::dto::NodeDef> =
|
||||
serde_json::from_value(definition.nodes.clone()).map_err(|e| {
|
||||
WorkflowError::InvalidDiagram(format!("节点数据无效: {e}"))
|
||||
})?;
|
||||
let edges: Vec<crate::dto::EdgeDef> =
|
||||
serde_json::from_value(definition.edges.clone()).map_err(|e| {
|
||||
WorkflowError::InvalidDiagram(format!("连线数据无效: {e}"))
|
||||
})?;
|
||||
let graph = parser::parse_and_validate(&nodes, &edges)?;
|
||||
|
||||
// 准备变量(从 req.form_data 中提取)
|
||||
let mut variables = HashMap::new();
|
||||
if let Some(form) = &req.form_data {
|
||||
if let Some(obj) = form.as_object() {
|
||||
for (k, v) in obj {
|
||||
variables.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 在事务中更新任务 + 推进 token
|
||||
let now = Utc::now();
|
||||
let outcome = req.outcome.clone();
|
||||
let form_data = req.form_data.clone();
|
||||
db.transaction::<_, (), WorkflowError>(|txn| {
|
||||
let graph = graph.clone();
|
||||
let variables = variables.clone();
|
||||
let task_model = task_model.clone();
|
||||
Box::pin(async move {
|
||||
// 更新任务状态
|
||||
let mut active: task::ActiveModel = task_model.clone().into();
|
||||
active.status = Set("completed".to_string());
|
||||
active.outcome = Set(Some(outcome));
|
||||
active.form_data = Set(form_data);
|
||||
active.completed_at = Set(Some(now));
|
||||
active.updated_at = Set(now);
|
||||
active.updated_by = Set(operator_id);
|
||||
active
|
||||
.update(txn)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
// 推进 token
|
||||
FlowExecutor::advance(
|
||||
token_id,
|
||||
instance_id,
|
||||
tenant_id,
|
||||
&graph,
|
||||
&variables,
|
||||
txn,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
event_bus.publish(erp_core::events::DomainEvent::new(
|
||||
"task.completed",
|
||||
tenant_id,
|
||||
serde_json::json!({ "task_id": id, "outcome": req.outcome }),
|
||||
));
|
||||
|
||||
// 重新查询任务
|
||||
let updated = task::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("任务不存在: {id}")))?;
|
||||
|
||||
Ok(Self::model_to_resp(&updated))
|
||||
}
|
||||
|
||||
/// 委派任务给其他人。
|
||||
pub async fn delegate(
|
||||
id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Uuid,
|
||||
req: &DelegateTaskReq,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<TaskResp> {
|
||||
let task_model = task::Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?
|
||||
.filter(|t| t.tenant_id == tenant_id && t.deleted_at.is_none())
|
||||
.ok_or_else(|| WorkflowError::NotFound(format!("任务不存在: {id}")))?;
|
||||
|
||||
if task_model.status != "pending" {
|
||||
return Err(WorkflowError::InvalidState(
|
||||
"任务状态不是 pending,无法委派".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let mut active: task::ActiveModel = task_model.into();
|
||||
active.assignee_id = Set(Some(req.delegate_to));
|
||||
active.updated_at = Set(Utc::now());
|
||||
active.updated_by = Set(operator_id);
|
||||
|
||||
let updated = active
|
||||
.update(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(Self::model_to_resp(&updated))
|
||||
}
|
||||
|
||||
/// 创建任务记录(由执行引擎调用)。
|
||||
pub async fn create_task(
|
||||
instance_id: Uuid,
|
||||
tenant_id: Uuid,
|
||||
token_id: Uuid,
|
||||
node_id: &str,
|
||||
node_name: Option<&str>,
|
||||
assignee_id: Option<Uuid>,
|
||||
candidate_groups: Option<Vec<String>>,
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
) -> WorkflowResult<Uuid> {
|
||||
let id = Uuid::now_v7();
|
||||
let now = Utc::now();
|
||||
let system_user = Uuid::nil();
|
||||
|
||||
let model = task::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
instance_id: Set(instance_id),
|
||||
token_id: Set(token_id),
|
||||
node_id: Set(node_id.to_string()),
|
||||
node_name: Set(node_name.map(|s| s.to_string())),
|
||||
assignee_id: Set(assignee_id),
|
||||
candidate_groups: Set(candidate_groups.map(|g| serde_json::to_value(g).unwrap_or_default())),
|
||||
status: Set("pending".to_string()),
|
||||
outcome: Set(None),
|
||||
form_data: Set(None),
|
||||
due_date: Set(None),
|
||||
completed_at: Set(None),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(system_user),
|
||||
updated_by: Set(system_user),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
model
|
||||
.insert(db)
|
||||
.await
|
||||
.map_err(|e| WorkflowError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn model_to_resp(m: &task::Model) -> TaskResp {
|
||||
TaskResp {
|
||||
id: m.id,
|
||||
instance_id: m.instance_id,
|
||||
token_id: m.token_id,
|
||||
node_id: m.node_id.clone(),
|
||||
node_name: m.node_name.clone(),
|
||||
assignee_id: m.assignee_id,
|
||||
candidate_groups: m.candidate_groups.clone(),
|
||||
status: m.status.clone(),
|
||||
outcome: m.outcome.clone(),
|
||||
form_data: m.form_data.clone(),
|
||||
due_date: m.due_date,
|
||||
completed_at: m.completed_at,
|
||||
created_at: m.created_at,
|
||||
definition_name: None,
|
||||
business_key: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user