Files
njts-accounting-core/backend/app/routers/month_locks.py
FNOS-WIN11ENT\choshoukaku 87fa1aaa1f 修正
2025-12-18 15:30:39 +09:00

94 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi import APIRouter, HTTPException
from app.core.database import get_connection
router = APIRouter(prefix="/month-locks", tags=["月次锁定"])
# ---------- GET月次锁定一覧 ----------
@router.get("", summary="月次锁定一覧取得")
def get_month_locks():
with get_connection() as conn, conn.cursor() as cur:
cur.execute("""
SELECT
fiscal_year,
month,
is_locked,
locked_at,
locked_by
FROM month_locks
ORDER BY fiscal_year, month
""")
return cur.fetchall()
# ---------- POST锁定月份 ----------
@router.post("/lock", summary="锁定月份")
def lock_month(fiscal_year: int, month: int):
# 参数校验
if not (1 <= month <= 12):
raise HTTPException(status_code=400, detail="month 必须是 112")
with get_connection() as conn, conn.cursor() as cur:
cur.execute("""
INSERT INTO month_locks (
fiscal_year,
month,
is_locked,
locked_at,
locked_by
)
VALUES (%s, %s, true, NOW(), %s)
ON CONFLICT (fiscal_year, month)
DO UPDATE
SET is_locked = true,
locked_at = NOW(),
locked_by = EXCLUDED.locked_by
""", (
fiscal_year,
month,
"system" # 将来可替换为登录用户
))
conn.commit()
return {
"status": "locked",
"fiscal_year": fiscal_year,
"month": month
}
# ---------- POST解锁月份 ----------
@router.post("/unlock", summary="解锁月份")
def unlock_month(fiscal_year: int, month: int):
# 参数校验
if not (1 <= month <= 12):
raise HTTPException(status_code=400, detail="month 必须是 112")
with get_connection() as conn, conn.cursor() as cur:
cur.execute("""
UPDATE month_locks
SET is_locked = false
WHERE fiscal_year = %s
AND month = %s
""", (
fiscal_year,
month
))
if cur.rowcount == 0:
raise HTTPException(
status_code=404,
detail="指定月份不存在"
)
conn.commit()
return {
"status": "unlocked",
"fiscal_year": fiscal_year,
"month": month
}