基于 skill-template 脚手架开发,包含: - payroll_config(费率配置,可编辑)+ payslip_snapshots(解析历史,只读)2 张业务表 - 全国默认 + 北上广 4 套费率(社保/公积金/个税起征点/7 级税率表) - 5 个业务 Action:calculate-payslip-simple / calculate-payslip-full / explain-term / view-config / edit-config - 个税累计预扣法计算,支持多月历史累加 - 20 个薪资名词库(社保/公积金/个税/全勤/加班费等) - 完整市场文档(README/TUTORIAL/DEMO/CHANGELOG) - 296 测试全部通过
51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
"""标准时间字段:Unix 秒级时间戳、默认值与 updated_at 自动维护。
|
||
|
||
业务表若包含 created_at / updated_at,在 CREATE TABLE 后调用
|
||
``init_updated_at_trigger(cursor, table_name)``。模板默认不建业务表。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
STANDARD_TIMESTAMP_COLUMNS = frozenset({"created_at", "updated_at"})
|
||
DATETIME_UNIX_SECONDS = "datetime_unix_seconds"
|
||
|
||
|
||
def updated_at_trigger_name(table_name: str) -> str:
|
||
return f"{table_name}_set_updated_at"
|
||
|
||
|
||
def _quote_identifier(name: str) -> str:
|
||
return '"' + name.replace('"', '""') + '"'
|
||
|
||
|
||
def init_updated_at_trigger(cursor, table_name: str) -> None:
|
||
"""幂等创建 {table}_set_updated_at:业务字段 UPDATE 后刷新 updated_at。"""
|
||
trigger = updated_at_trigger_name(table_name)
|
||
quoted = _quote_identifier(table_name)
|
||
quoted_trigger = _quote_identifier(trigger)
|
||
cursor.execute(
|
||
f"""
|
||
CREATE TRIGGER IF NOT EXISTS {quoted_trigger}
|
||
AFTER UPDATE ON {quoted}
|
||
FOR EACH ROW
|
||
BEGIN
|
||
UPDATE {quoted}
|
||
SET updated_at = unixepoch()
|
||
WHERE id = NEW.id AND updated_at = OLD.updated_at;
|
||
END
|
||
"""
|
||
)
|
||
|
||
|
||
def has_updated_at_trigger(cursor, table_name: str) -> bool:
|
||
trigger_name = updated_at_trigger_name(table_name)
|
||
cursor.execute(
|
||
"""
|
||
SELECT 1 FROM sqlite_master
|
||
WHERE type = 'trigger' AND name = ? AND tbl_name = ?
|
||
LIMIT 1
|
||
""",
|
||
(trigger_name, table_name),
|
||
)
|
||
return cursor.fetchone() is not None
|