"""业务种子数据:预警规则阈值。 init-db 幂等写入(存在即跳过,不覆盖管理员修改)。 阈值缺省口径来自《核心建设内容:各部门"Skill"深度需求规格书》财务处 Skill 002: 低于序时进度 10% 记"严重滞后",高于 20% 记"支出异常偏快";暂付款账龄 90 天。 """ from __future__ import annotations import sqlite3 from util.timeutil import now_unix # (rule_key, display_name, threshold_value, description) _SEED_ALERT_RULES: tuple[tuple[str, str, float, str], ...] = ( ( "lag_threshold", "严重滞后阈值", 0.10, "实际支出率低于序时进度该比例(小数,0.10=10个百分点)时标记“严重滞后”。", ), ( "overshoot_threshold", "支出异常偏快阈值", 0.20, "实际支出率高于序时进度该比例(小数,0.20=20个百分点)时标记“支出异常偏快”,预警年底突击花钱。", ), ( "pending_aging_days", "暂付款账龄提醒天数", 90.0, "暂付款自最早发生日起超过该天数(天)时提醒及时冲销。", ), ( "closing_warn_days", "结题临近预警天数", 90.0, "距执行期结束不足该天数(天)且支出率低于 80% 时提醒尽快完成报销。", ), ) def seed_alert_rules(cursor: sqlite3.Cursor) -> int: """幂等写入预警规则种子,返回新增条数。""" inserted = 0 for rule_key, display_name, threshold_value, description in _SEED_ALERT_RULES: cursor.execute("SELECT COUNT(*) FROM alert_rules WHERE rule_key = ?", (rule_key,)) if int(cursor.fetchone()[0]) > 0: continue cursor.execute( """ INSERT INTO alert_rules (rule_key, display_name, threshold_value, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?) """, (rule_key, display_name, threshold_value, description, now_unix(), now_unix()), ) inserted += 1 return inserted def seed_all(cursor: sqlite3.Cursor) -> dict[str, int]: """写入全部业务种子,返回各类新增条数。""" return {"alert_rules": seed_alert_rules(cursor)}