feat(health): 积分触发扩展 + 随访模板关联 — Phase 3

- 新增 follow_up.completed 事件积分消费者,随访完成触发 30 积分
- follow_up_task 新增 template_id FK 关联随访模板
- follow_up_record 新增 form_data JSONB 存储结构化表单数据
- 新增 POST /health/follow-up-tasks/from-template 基于模板创建随访任务端点
- 数据库迁移 160:follow_up_task.template_id + follow_up_record.form_data + 积分规则种子
This commit is contained in:
iven
2026-05-21 00:50:29 +08:00
parent 5877342a4d
commit ec7f76127d
11 changed files with 341 additions and 0 deletions

View File

@@ -159,6 +159,9 @@ mod m20260519_000154_seed_ai_knowledge_permissions;
mod m20260519_000155_fix_ai_menus_and_add_chat;
mod m20260519_000156_fix_ai_menus_round2;
mod m20260520_000157_follow_up_source_and_points_rules;
mod m20260521_000158_alerts_add_source_columns;
mod m20260521_000159_patient_phone_and_consent_seed;
mod m20260521_000160_follow_up_task_template_id_and_record_form_data;
pub struct Migrator;
@@ -325,6 +328,9 @@ impl MigratorTrait for Migrator {
Box::new(m20260519_000155_fix_ai_menus_and_add_chat::Migration),
Box::new(m20260519_000156_fix_ai_menus_round2::Migration),
Box::new(m20260520_000157_follow_up_source_and_points_rules::Migration),
Box::new(m20260521_000158_alerts_add_source_columns::Migration),
Box::new(m20260521_000159_patient_phone_and_consent_seed::Migration),
Box::new(m20260521_000160_follow_up_task_template_id_and_record_form_data::Migration),
]
}
}

View File

@@ -0,0 +1,85 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 1. follow_up_task 新增 template_id 字段,关联随访模板
manager
.alter_table(
Table::alter()
.table(Alias::new("follow_up_task"))
.add_column(ColumnDef::new(Alias::new("template_id")).uuid().null())
.to_owned(),
)
.await?;
// 2. follow_up_record 新增 form_data JSONB 字段,存储模板结构化表单数据
manager
.alter_table(
Table::alter()
.table(Alias::new("follow_up_record"))
.add_column(ColumnDef::new(Alias::new("form_data")).json_binary().null())
.to_owned(),
)
.await?;
// 3. 新增积分规则种子:随访完成
let insert_sql = r#"
INSERT INTO points_rule (id, tenant_id, name, event_type, points_value, daily_cap, is_active, created_at, updated_at, version)
SELECT
gen_random_uuid(),
t.id,
'随访完成',
'follow_up_completion',
30,
60,
true,
NOW(),
NOW(),
1
FROM tenant t
WHERE NOT EXISTS (
SELECT 1 FROM points_rule pr
WHERE pr.event_type = 'follow_up_completion' AND pr.tenant_id = t.id
);
"#;
manager
.get_connection()
.execute_unprepared(insert_sql)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Alias::new("follow_up_task"))
.drop_column(Alias::new("template_id"))
.to_owned(),
)
.await?;
manager
.alter_table(
Table::alter()
.table(Alias::new("follow_up_record"))
.drop_column(Alias::new("form_data"))
.to_owned(),
)
.await?;
manager
.get_connection()
.execute_unprepared(
"DELETE FROM points_rule WHERE event_type = 'follow_up_completion';",
)
.await?;
Ok(())
}
}