feat: add audit logging and lesson statistics for clazz
- Add clazz_audit_log table with diesel migration for CRUD audit trail - Add audit log backend: model, queries, handler, route - Add audit log viewer in clazz detail modal (操作记录) - Add student_lesson_stats API (GET /api/v1/clazz/stats/my-lessons) - Add teacher_detail_stats API (GET /api/v1/clazz/stats/teacher-detail) - Register all new routes in lib.rs Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS clazz_audit_log;
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE clazz_audit_log (
|
||||
id VARCHAR PRIMARY KEY,
|
||||
clazz_id VARCHAR NOT NULL,
|
||||
action VARCHAR NOT NULL,
|
||||
operator_hty_id VARCHAR NOT NULL,
|
||||
operator_name VARCHAR,
|
||||
changes JSONB,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
org_id VARCHAR
|
||||
);
|
||||
|
||||
CREATE INDEX idx_clazz_audit_log_clazz_id ON clazz_audit_log (clazz_id);
|
||||
CREATE INDEX idx_clazz_audit_log_created_at ON clazz_audit_log (created_at);
|
||||
@@ -1255,6 +1255,124 @@ pub struct TeacherHourStatsRow {
|
||||
pub total_hours: f64,
|
||||
}
|
||||
|
||||
#[derive(QueryableByName, Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct StudentLessonRow {
|
||||
#[diesel(sql_type = diesel::sql_types::Varchar)]
|
||||
pub clazz_id: String,
|
||||
#[diesel(sql_type = diesel::sql_types::Varchar)]
|
||||
pub clazz_name: String,
|
||||
#[diesel(sql_type = diesel::sql_types::Timestamp)]
|
||||
pub start_from: NaiveDateTime,
|
||||
#[diesel(sql_type = diesel::sql_types::Timestamp)]
|
||||
pub end_by: NaiveDateTime,
|
||||
#[diesel(sql_type = diesel::sql_types::Nullable<diesel::sql_types::Varchar>)]
|
||||
pub teacher_names: Option<String>,
|
||||
#[diesel(sql_type = diesel::sql_types::Nullable<diesel::sql_types::Varchar>)]
|
||||
pub attendance_status: Option<String>,
|
||||
#[diesel(sql_type = diesel::sql_types::Nullable<diesel::sql_types::Double>)]
|
||||
pub deducted_hours: Option<f64>,
|
||||
#[diesel(sql_type = diesel::sql_types::Nullable<diesel::sql_types::Varchar>)]
|
||||
pub leave_status: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(QueryableByName, Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct TeacherDetailStatsRow {
|
||||
#[diesel(sql_type = diesel::sql_types::Varchar)]
|
||||
pub student_id: String,
|
||||
#[diesel(sql_type = diesel::sql_types::Nullable<diesel::sql_types::Varchar>)]
|
||||
pub student_name: Option<String>,
|
||||
#[diesel(sql_type = diesel::sql_types::BigInt)]
|
||||
pub total_classes: i64,
|
||||
#[diesel(sql_type = diesel::sql_types::BigInt)]
|
||||
pub attended: i64,
|
||||
#[diesel(sql_type = diesel::sql_types::BigInt)]
|
||||
pub absent: i64,
|
||||
#[diesel(sql_type = diesel::sql_types::Double)]
|
||||
pub total_hours: f64,
|
||||
#[diesel(sql_type = diesel::sql_types::BigInt)]
|
||||
pub leave_count: i64,
|
||||
}
|
||||
|
||||
impl Clazz {
|
||||
pub fn student_lesson_stats_by_org_and_date_range(
|
||||
org: &String,
|
||||
start_date: &String,
|
||||
end_date: &String,
|
||||
student_id: &String,
|
||||
conn: &mut PgConnection,
|
||||
) -> anyhow::Result<Vec<StudentLessonRow>> {
|
||||
let query_text = format!(
|
||||
"select c.id as clazz_id,
|
||||
c.clazz_name,
|
||||
c.start_from,
|
||||
c.end_by,
|
||||
(select string_agg(elem->>'real_name', ', ')
|
||||
from jsonb_array_elements(c.teachers#>'{{val,users,vals}}') elem) as teacher_names,
|
||||
a.status as attendance_status,
|
||||
a.deducted_hours,
|
||||
lr.request_status as leave_status
|
||||
from clazz c
|
||||
left join clazz_attendance a on c.id = a.clazz_id and a.student_id = '{sid}' and (a.is_delete is null or a.is_delete = false)
|
||||
left join clazz_leave_request lr on c.id = lr.clazz_id and lr.student_id = '{sid}'
|
||||
where c.org_id = '{org}'
|
||||
and c.start_from >= '{start}'
|
||||
and c.end_by <= '{end}'
|
||||
and (c.is_delete is null or c.is_delete = false)
|
||||
and c.students @> '{{\"val\": {{\"users\": {{\"vals\": [{{\"user_id\": \"{sid}\"}}]}}}}}}'::jsonb
|
||||
order by c.start_from desc",
|
||||
sid = student_id,
|
||||
org = org,
|
||||
start = start_date,
|
||||
end = end_date,
|
||||
);
|
||||
sql_query(query_text)
|
||||
.load::<StudentLessonRow>(conn)
|
||||
.map_err(|e| anyhow!(HtyErr {
|
||||
code: HtyErrCode::DbErr,
|
||||
reason: Some(e.to_string()),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn teacher_detail_stats_by_org_and_date_range(
|
||||
org: &String,
|
||||
start_date: &String,
|
||||
end_date: &String,
|
||||
teacher_id: &String,
|
||||
conn: &mut PgConnection,
|
||||
) -> anyhow::Result<Vec<TeacherDetailStatsRow>> {
|
||||
let query_text = format!(
|
||||
"select s_el->>'user_id' as student_id,
|
||||
s_el->>'real_name' as student_name,
|
||||
count(distinct c.id)::bigint as total_classes,
|
||||
count(distinct a.id) filter (where a.status = 'NORMAL')::bigint as attended,
|
||||
count(distinct a.id) filter (where a.status = 'ABSENT')::bigint as absent,
|
||||
coalesce(sum(a.deducted_hours), 0)::double precision as total_hours,
|
||||
count(distinct lr.id)::bigint as leave_count
|
||||
from clazz c
|
||||
cross join lateral jsonb_array_elements(c.students#>'{{val,users,vals}}') s_el
|
||||
left join clazz_attendance a on c.id = a.clazz_id and a.student_id = s_el->>'user_id' and (a.is_delete is null or a.is_delete = false)
|
||||
left join clazz_leave_request lr on c.id = lr.clazz_id and lr.student_id = s_el->>'user_id'
|
||||
where c.org_id = '{org}'
|
||||
and c.start_from >= '{start}'
|
||||
and c.end_by <= '{end}'
|
||||
and (c.is_delete is null or c.is_delete = false)
|
||||
and c.created_by = '{tid}'
|
||||
group by s_el->>'user_id', s_el->>'real_name'
|
||||
order by student_name",
|
||||
org = org,
|
||||
start = start_date,
|
||||
end = end_date,
|
||||
tid = teacher_id,
|
||||
);
|
||||
sql_query(query_text)
|
||||
.load::<TeacherDetailStatsRow>(conn)
|
||||
.map_err(|e| anyhow!(HtyErr {
|
||||
code: HtyErrCode::DbErr,
|
||||
reason: Some(e.to_string()),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl ClazzLeaveRequest {
|
||||
pub fn create(
|
||||
payload: &ClazzLeaveRequest,
|
||||
@@ -1434,3 +1552,53 @@ impl ClazzAttendance {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Queryable, Insertable, PartialEq)]
|
||||
#[diesel(table_name = clazz_audit_log)]
|
||||
pub struct ClazzAuditLog {
|
||||
pub id: String,
|
||||
pub clazz_id: String,
|
||||
pub action: String,
|
||||
pub operator_hty_id: String,
|
||||
pub operator_name: Option<String>,
|
||||
pub changes: Option<serde_json::Value>,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub org_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = clazz_audit_log)]
|
||||
pub struct NewClazzAuditLog {
|
||||
pub id: String,
|
||||
pub clazz_id: String,
|
||||
pub action: String,
|
||||
pub operator_hty_id: String,
|
||||
pub operator_name: Option<String>,
|
||||
pub changes: Option<serde_json::Value>,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub org_id: Option<String>,
|
||||
}
|
||||
|
||||
impl ClazzAuditLog {
|
||||
pub fn insert(
|
||||
log: &NewClazzAuditLog,
|
||||
conn: &mut PgConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
use crate::schema::clazz_audit_log::dsl::*;
|
||||
insert_into(clazz_audit_log)
|
||||
.values(log)
|
||||
.execute(conn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn list_by_clazz_id(
|
||||
clazz_id_in: &String,
|
||||
conn: &mut PgConnection,
|
||||
) -> anyhow::Result<Vec<ClazzAuditLog>> {
|
||||
use crate::schema::clazz_audit_log::dsl::*;
|
||||
Ok(clazz_audit_log
|
||||
.filter(clazz_id.eq(clazz_id_in))
|
||||
.order(created_at.desc())
|
||||
.load::<ClazzAuditLog>(conn)?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,19 @@ diesel::table! {
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
clazz_audit_log (id) {
|
||||
id -> Varchar,
|
||||
clazz_id -> Varchar,
|
||||
action -> Varchar,
|
||||
operator_hty_id -> Varchar,
|
||||
operator_name -> Nullable<Varchar>,
|
||||
changes -> Nullable<Jsonb>,
|
||||
created_at -> Timestamp,
|
||||
org_id -> Nullable<Varchar>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
clazz_leave_request (id) {
|
||||
id -> Varchar,
|
||||
@@ -155,6 +168,7 @@ diesel::joinable!(hour_transaction -> course_hour_package (package_id));
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
clazz,
|
||||
clazz_attendance,
|
||||
clazz_audit_log,
|
||||
clazz_leave_request,
|
||||
clazz_repeat,
|
||||
course_hour_package,
|
||||
|
||||
Reference in New Issue
Block a user