|
|
|
|
# controller/GraphStyleController.py
|
|
|
|
|
import json
|
|
|
|
|
from robyn import jsonify, Response
|
|
|
|
|
from app import app
|
|
|
|
|
from service.GraphStyleService import GraphStyleService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- 核心工具函数:解决乱码 ---
|
|
|
|
|
def create_response(status_code, data_dict):
|
|
|
|
|
"""
|
|
|
|
|
统一响应格式封装,强制使用 UTF-8 防止中文乱码。
|
|
|
|
|
"""
|
|
|
|
|
return Response(
|
|
|
|
|
status_code=status_code,
|
|
|
|
|
description=json.dumps(data_dict, ensure_ascii=False),
|
|
|
|
|
headers={"Content-Type": "application/json; charset=utf-8"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/graph/style/save")
|
|
|
|
|
async def save_style_config(request):
|
|
|
|
|
"""保存配置接口 - 修复版:支持移动与更新逻辑"""
|
|
|
|
|
try:
|
|
|
|
|
body = request.json()
|
|
|
|
|
|
|
|
|
|
# 1. 核心修改:接收前端传来的 id
|
|
|
|
|
# 当执行“移动”操作时,前端会传 config.id;当执行“保存当前配置”时,id 为空
|
|
|
|
|
config_id = body.get('id')
|
|
|
|
|
|
|
|
|
|
canvas_name = body.get('canvas_name')
|
|
|
|
|
current_label = body.get('current_label')
|
|
|
|
|
styles = body.get('styles')
|
|
|
|
|
group_name = body.get('group_name')
|
|
|
|
|
|
|
|
|
|
if not all([canvas_name, current_label, styles]):
|
|
|
|
|
return create_response(200, {"code": 400, "msg": "参数不完整"})
|
|
|
|
|
|
|
|
|
|
# 2. 核心修改:将 config_id 传给 Service 层
|
|
|
|
|
# 这样 Service 就能根据是否有 id 来判断是执行 UPDATE 还是 INSERT
|
|
|
|
|
success = GraphStyleService.save_config(
|
|
|
|
|
canvas_name=canvas_name,
|
|
|
|
|
current_label=current_label,
|
|
|
|
|
styles_dict=styles,
|
|
|
|
|
group_name=group_name,
|
|
|
|
|
config_id=config_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if success:
|
|
|
|
|
return create_response(200, {"code": 200, "msg": "操作成功"})
|
|
|
|
|
else:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": "操作失败"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"系统异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/graph/style/list/grouped")
|
|
|
|
|
async def get_grouped_style_list(request):
|
|
|
|
|
"""获取【分组嵌套】格式的配置列表(用于右侧折叠面板)"""
|
|
|
|
|
try:
|
|
|
|
|
# 调用 Service 的嵌套聚合方法,现在内部已包含 is_active/is_default 逻辑
|
|
|
|
|
data = GraphStyleService.get_grouped_configs()
|
|
|
|
|
return create_response(200, {"code": 200, "data": data, "msg": "查询成功"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"查询异常: {str(e)}"})
|
|
|
|
|
@app.get("/api/graph/style/active")
|
|
|
|
|
async def get_active_style(request):
|
|
|
|
|
"""获取【分组嵌套】格式的配置列表(用于右侧折叠面板)"""
|
|
|
|
|
try:
|
|
|
|
|
# 调用 Service 的嵌套聚合方法,现在内部已包含 is_active/is_default 逻辑
|
|
|
|
|
data = GraphStyleService.get_active_configs()
|
|
|
|
|
return create_response(200, {"code": 200, "data": data, "msg": "查询成功"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"查询异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/graph/style/group/apply")
|
|
|
|
|
async def apply_style_group(request):
|
|
|
|
|
"""应用全案:将某个方案组设为当前激活状态"""
|
|
|
|
|
try:
|
|
|
|
|
body = request.json()
|
|
|
|
|
group_id = body.get('group_id')
|
|
|
|
|
|
|
|
|
|
if not group_id:
|
|
|
|
|
return create_response(200, {"code": 400, "msg": "缺少分组ID"})
|
|
|
|
|
|
|
|
|
|
success = GraphStyleService.apply_group_all(group_id)
|
|
|
|
|
if success:
|
|
|
|
|
return create_response(200, {"code": 200, "msg": "方案已成功应用全案"})
|
|
|
|
|
else:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": "应用全案失败"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"操作异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/graph/style/group/set_default")
|
|
|
|
|
async def set_default_style_group(request):
|
|
|
|
|
"""设为默认:将某个方案组设为页面初始化的默认配置"""
|
|
|
|
|
try:
|
|
|
|
|
body = request.json()
|
|
|
|
|
group_id = body.get('group_id')
|
|
|
|
|
|
|
|
|
|
if not group_id:
|
|
|
|
|
return create_response(200, {"code": 400, "msg": "缺少分组ID"})
|
|
|
|
|
|
|
|
|
|
success = GraphStyleService.set_default_group(group_id)
|
|
|
|
|
if success:
|
|
|
|
|
return create_response(200, {"code": 200, "msg": "已成功设为系统默认方案"})
|
|
|
|
|
else:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": "设置默认方案失败"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"操作异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/graph/style/groups")
|
|
|
|
|
async def get_group_names(request):
|
|
|
|
|
"""获取所有已存在的方案组列表(用于保存弹窗的下拉选择)"""
|
|
|
|
|
try:
|
|
|
|
|
data = GraphStyleService.get_group_list()
|
|
|
|
|
return create_response(200, {"code": 200, "data": data, "msg": "查询成功"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"查询异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/api/graph/style/list")
|
|
|
|
|
async def get_style_list(request):
|
|
|
|
|
"""获取原始扁平配置列表(保留兼容性)"""
|
|
|
|
|
try:
|
|
|
|
|
data = GraphStyleService.get_all_configs()
|
|
|
|
|
return create_response(200, {"code": 200, "data": data, "msg": "查询成功"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"查询异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/graph/style/delete")
|
|
|
|
|
async def delete_style_config(request):
|
|
|
|
|
"""删除单条画布配置"""
|
|
|
|
|
try:
|
|
|
|
|
body = request.json()
|
|
|
|
|
config_id = body.get('id')
|
|
|
|
|
|
|
|
|
|
if not config_id:
|
|
|
|
|
return create_response(200, {"code": 400, "msg": "缺少ID"})
|
|
|
|
|
|
|
|
|
|
success = GraphStyleService.delete_config(config_id)
|
|
|
|
|
if success:
|
|
|
|
|
return create_response(200, {"code": 200, "msg": "删除成功"})
|
|
|
|
|
else:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": "删除失败"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"操作异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/graph/style/group/delete")
|
|
|
|
|
async def delete_style_group(request):
|
|
|
|
|
"""删除整个方案组及其下属所有配置"""
|
|
|
|
|
try:
|
|
|
|
|
body = request.json()
|
|
|
|
|
group_id = body.get('group_id')
|
|
|
|
|
|
|
|
|
|
if not group_id:
|
|
|
|
|
return create_response(200, {"code": 400, "msg": "缺少分组ID"})
|
|
|
|
|
|
|
|
|
|
success = GraphStyleService.delete_group(group_id)
|
|
|
|
|
if success:
|
|
|
|
|
return create_response(200, {"code": 200, "msg": "方案组已彻底删除"})
|
|
|
|
|
else:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": "方案组删除失败"})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"操作异常: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/graph/style/batch_delete")
|
|
|
|
|
async def batch_delete_style(request):
|
|
|
|
|
"""批量删除配置接口"""
|
|
|
|
|
try:
|
|
|
|
|
body = request.json()
|
|
|
|
|
config_ids = body.get('ids')
|
|
|
|
|
|
|
|
|
|
if isinstance(config_ids, str):
|
|
|
|
|
try:
|
|
|
|
|
config_ids = json.loads(config_ids)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if not config_ids or not isinstance(config_ids, list):
|
|
|
|
|
return create_response(200, {"code": 400, "msg": "参数格式错误"})
|
|
|
|
|
|
|
|
|
|
count = GraphStyleService.batch_delete_configs(config_ids)
|
|
|
|
|
return create_response(200, {"code": 200, "msg": f"成功删除 {count} 条配置", "count": count})
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return create_response(200, {"code": 500, "msg": f"批量删除异常: {str(e)}"})
|