sz-rust-cli 1.4.0

SZ-Rust 命令行工具:项目脚手架、数据库迁移、调度器管理
Documentation
// Generated by sz-rust-cli make:plugin --template {{ template_type }} --name {{ plugin_name }}
// Generation time: {{ generated_at }}
// DO NOT EDIT MANUALLY

//! Controller for workflow `{{ plugin_name }}` with state transitions and approval logic

use crate::models::{{ class_name | snake_case }}::{{ class_name }}State;
use crate::models::{{ class_name | snake_case }}::can_transition;
use sz_rust_core::error::AppError;

pub struct {{ class_name }}Controller;

impl {{ class_name }}Controller {
    /// Transition workflow to a new state
    pub async fn transition(
        model: &mut crate::models::{{ class_name | snake_case }}::{{ class_name }},
        target: {{ class_name }}State,
        operator_id: i64,
    ) -> Result<(), AppError> {
        if !can_transition(model.current_state, target) {
            return Err(AppError::bad_request(format!(
                "不允许从 {} 转换到 {}",
                model.current_state, target
            )));
        }
        tracing::info!(
            from = %model.current_state,
            to = %target,
            operator = operator_id,
            "状态流转"
        );
        model.current_state = target;
        Ok(())
    }

    {%- for transition in transitions %}
    {%- if transition.requires_approval %}
    /// Request approval for {{ transition.from }} -> {{ transition.to }}
    pub async fn request_{{ transition.from }}_to_{{ transition.to }}(
        model: &crate::models::{{ class_name | snake_case }}::{{ class_name }},
        approver_id: i64,
    ) -> Result<bool, AppError> {
        if model.current_state != {{ class_name }}State::{{ transition.from | pascal_case }} {
            return Err(AppError::bad_request("当前状态不匹配"));
        }
        tracing::info!(approver = approver_id, "审批请求: {{ transition.from }} -> {{ transition.to }}");
        Ok(true)
    }
    {%- endif %}
    {%- endfor %}
}