//! 任务相关命令 use serde::Deserialize; use tauri::State; use df_core::types::new_id; use df_storage::models::TaskRecord; use crate::state::AppState; use super::now_millis; /// 创建任务入参 #[derive(Debug, Deserialize)] pub struct CreateTaskInput { pub project_id: String, pub title: String, #[serde(default)] pub description: String, #[serde(default = "default_priority")] pub priority: i32, pub branch_name: Option, pub assignee: Option, } fn default_priority() -> i32 { 2 // medium — 新任务默认中优先级(非 high),符合常识 } /// 列出任务,可按 project_id 过滤 #[tauri::command] pub async fn list_tasks( state: State<'_, AppState>, project_id: Option, ) -> Result, String> { let result = match project_id { Some(pid) => state.tasks.query("project_id", &pid).await, None => state.tasks.list_all().await, }; result.map_err(|e| e.to_string()) } /// 创建任务,返回完整记录 #[tauri::command] pub async fn create_task( state: State<'_, AppState>, input: CreateTaskInput, ) -> Result { let now = now_millis(); let record = TaskRecord { id: new_id(), project_id: input.project_id, title: input.title, description: input.description, status: "todo".to_string(), priority: input.priority, branch_name: input.branch_name, assignee: input.assignee, workflow_def_id: None, base_branch: None, created_at: now.clone(), updated_at: now, }; state .tasks .insert(record.clone()) .await .map_err(|e| e.to_string())?; Ok(record) } /// 更新任务单个字段(字段名走 df-storage 白名单校验) #[tauri::command] pub async fn update_task( state: State<'_, AppState>, id: String, field: String, value: String, ) -> Result { state .tasks .update_field(&id, &field, &value) .await .map_err(|e| e.to_string()) } /// 删除任务 #[tauri::command] pub async fn delete_task(state: State<'_, AppState>, id: String) -> Result { state.tasks.delete(&id).await.map_err(|e| e.to_string()) }