包含前后端完整代码: - 前端:Vue3+Vite+ElementPlus - 后端:FastAPI+SQLAlchemy - 模块:驾驶舱/KPI/战略地图/预警/预算/成本/预测/改善行动 - 当前版本:v1.0.0
87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
"""战略地图版本管理 API"""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.database import get_db
|
|
from app.auth_middleware import require_role
|
|
from app.models import StrategicMap, StrategicMapVersion
|
|
|
|
router = APIRouter(prefix="/api/cma/maps", tags=["战略地图版本"],
|
|
dependencies=[Depends(require_role("ceo", "finance"))],
|
|
)
|
|
|
|
|
|
@router.get("/{map_id}/versions")
|
|
def list_versions(map_id: int, db: Session = Depends(get_db)):
|
|
"""查看版本历史"""
|
|
versions = db.query(StrategicMapVersion).filter(
|
|
StrategicMapVersion.map_id == map_id
|
|
).order_by(StrategicMapVersion.id.desc()).all()
|
|
return {"data": [v_to_dict(v) for v in versions]}
|
|
|
|
|
|
@router.post("/{map_id}/versions/snapshot")
|
|
def create_snapshot(map_id: int, data: dict, db: Session = Depends(get_db)):
|
|
"""手动创建快照"""
|
|
m = db.query(StrategicMap).filter(StrategicMap.id == map_id).first()
|
|
if not m:
|
|
raise HTTPException(404, "战略地图不存在")
|
|
|
|
import json
|
|
dims = m.dimensions
|
|
canvas = m.canvas_data
|
|
if isinstance(dims, str):
|
|
dims = json.loads(dims)
|
|
if isinstance(canvas, str):
|
|
canvas = json.loads(canvas)
|
|
|
|
# 自动版本号
|
|
existing = db.query(StrategicMapVersion).filter(
|
|
StrategicMapVersion.map_id == map_id
|
|
).order_by(StrategicMapVersion.id.desc()).first()
|
|
if existing:
|
|
import re
|
|
match = re.search(r"v(\d+)\.(\d+)", existing.version)
|
|
major = int(match.group(1)) if match else 1
|
|
minor = int(match.group(2)) + 1 if match else 0
|
|
new_ver = f"v{major}.{minor}"
|
|
else:
|
|
new_ver = "v1.0"
|
|
|
|
snapshot = StrategicMapVersion(
|
|
map_id=map_id,
|
|
version=new_ver,
|
|
dimensions=dims,
|
|
canvas_data=canvas,
|
|
comment=data.get("comment", f"手动快照 {new_ver}"),
|
|
)
|
|
db.add(snapshot)
|
|
db.commit()
|
|
db.refresh(snapshot)
|
|
return v_to_dict(snapshot)
|
|
|
|
|
|
@router.post("/{map_id}/versions/{ver_id}/rollback")
|
|
def rollback_version(map_id: int, ver_id: int, db: Session = Depends(get_db)):
|
|
"""回滚到指定版本"""
|
|
m = db.query(StrategicMap).filter(StrategicMap.id == map_id).first()
|
|
if not m:
|
|
raise HTTPException(404, "战略地图不存在")
|
|
|
|
v = db.query(StrategicMapVersion).filter(
|
|
StrategicMapVersion.id == ver_id,
|
|
StrategicMapVersion.map_id == map_id,
|
|
).first()
|
|
if not v:
|
|
raise HTTPException(404, "版本不存在")
|
|
|
|
m.dimensions = v.dimensions
|
|
m.canvas_data = v.canvas_data
|
|
m.version = f"rollback-{v.version}"
|
|
m.status = "draft"
|
|
db.commit()
|
|
return {"message": f"已回滚到 {v.version}", "version": m.version}
|
|
|
|
|
|
def v_to_dict(v):
|
|
return {c.name: getattr(v, c.name) for c in v.__table__.columns}
|