fix: 设备验收预警优化

This commit is contained in:
sophie committed 2026-10-13 15:52:09 +08:00
1 parent 7e7bcbd388
commit b8cc0cbe77
25 files changed
+1398 -193

No files matched your search

@@ -0,0 +1,97 @@
"""补齐设备验收预警的企微事件、第 4 轮开关和 Excel 归档。
Revision ID: eah8c9d0e1f2
Revises: b7a2b2155155
Create Date: 2026-10-13 14:30:00.000000
用途:把尚未提交的三步收成一次升级。已有库如果停在本版本,结构已经齐,不会重跑。
依赖:b7a2b2155155、app_settings、equipment_acceptance_settings、users。
逻辑:企微事件名单末尾补上 equipment_acceptance_warning;第 4 轮默认关闭;按收件人留下发送时的 Excel。
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import text
revision = "eah8c9d0e1f2"
down_revision = "b7a2b2155155"
branch_labels = None
depends_on = None
_KEY = "WECOM_NOTIFY_EVENTS"
_EVENT = "equipment_acceptance_warning"
def _split_events(raw: str | None) -> list[str]:
"""按逗号拆开事件名单,去掉空白项。"""
return [part.strip() for part in str(raw or "").split(",") if part.strip()]
def upgrade():
"""补上企微事件、第 4 轮开关,并建立按收件人归档 Excel 的表。"""
_append_wecom_event()
op.add_column(
"equipment_acceptance_settings",
sa.Column("round_4_enabled", sa.Boolean(), nullable=False, server_default=sa.false()),
)
op.create_table(
"equipment_acceptance_send_files",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column("recipient_user_id", sa.Integer(), nullable=True),
sa.Column("recipient_name", sa.String(length=200), nullable=True),
sa.Column("excel_file_name", sa.String(length=255), nullable=True),
sa.Column("excel_file_data", sa.LargeBinary(), nullable=True),
sa.Column("excel_file_size", sa.Integer(), nullable=True),
sa.Column("send_record_ids", sa.JSON(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["recipient_user_id"], ["users.id"], ondelete="SET NULL"),
)
op.create_index(
"ix_equipment_acceptance_send_files_created",
"equipment_acceptance_send_files",
["created_at"],
)
def downgrade():
"""按相反顺序去掉 Excel 归档、第 4 轮开关和企微事件。"""
op.drop_index("ix_equipment_acceptance_send_files_created", table_name="equipment_acceptance_send_files")
op.drop_table("equipment_acceptance_send_files")
op.drop_column("equipment_acceptance_settings", "round_4_enabled")
_remove_wecom_event()
def _append_wecom_event() -> None:
"""已有事件名单时在末尾补上设备验收预警。没有这一行则不插入。"""
bind = op.get_bind()
if "app_settings" not in sa.inspect(bind).get_table_names():
return
row = bind.execute(text("SELECT value FROM app_settings WHERE key = :key"), {"key": _KEY}).fetchone()
if row is None:
return
events = _split_events(row[0])
if _EVENT in events:
return
events.append(_EVENT)
bind.execute(
text("UPDATE app_settings SET value = :value, updated_at = CURRENT_TIMESTAMP WHERE key = :key"),
{"key": _KEY, "value": ",".join(events)},
)
def _remove_wecom_event() -> None:
"""从已保存的企微事件名单里去掉设备验收预警。没有这一行时不处理。"""
bind = op.get_bind()
if "app_settings" not in sa.inspect(bind).get_table_names():
return
row = bind.execute(text("SELECT value FROM app_settings WHERE key = :key"), {"key": _KEY}).fetchone()
if row is None:
return
events = [part for part in _split_events(row[0]) if part != _EVENT]
bind.execute(
text("UPDATE app_settings SET value = :value, updated_at = CURRENT_TIMESTAMP WHERE key = :key"),
{"key": _KEY, "value": ",".join(events)},
)