From c58b925ab4a92c9ea300d576921a6371bad46c42 Mon Sep 17 00:00:00 2001 From: Hera Zhao Date: Sat, 11 Apr 2026 10:34:41 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=B0=E9=A1=B9=E7=9B=AE=E5=94=AE?= =?UTF-8?q?=E4=BB=B7299=E5=92=8C=E6=89=B9=E9=87=8F1=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E5=8C=96=E5=88=B0=E5=90=8E=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端存储selling_price/packaging_cost/labor_cost/other_cost/quantity到pricing字段(JSON) - 加载时解析并返回给前端 - 新建时也保存这些字段 Co-Authored-By: Claude Opus 4.6 (1M context) --- backend/main.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/backend/main.py b/backend/main.py index b469c36..275bd84 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1242,17 +1242,32 @@ def list_projects(): conn = get_db() rows = conn.execute("SELECT * FROM profit_projects ORDER BY id DESC").fetchall() conn.close() - return [{ **dict(r), "ingredients": json.loads(r["ingredients"]) } for r in rows] + result = [] + for r in rows: + d = dict(r) + d["ingredients"] = json.loads(r["ingredients"]) + try: + extra = json.loads(r["pricing"]) if r["pricing"] else {} + if isinstance(extra, dict): + d.update(extra) + except (json.JSONDecodeError, TypeError): + pass + result.append(d) + return result @app.post("/api/projects", status_code=201) def create_project(body: dict, user=Depends(require_role("admin", "senior_editor"))): conn = get_db() c = conn.cursor() + extra = {} + for k in ("selling_price", "packaging_cost", "labor_cost", "other_cost", "quantity"): + if k in body: + extra[k] = body[k] c.execute( "INSERT INTO profit_projects (name, ingredients, pricing, note, created_by) VALUES (?, ?, ?, ?, ?)", (body["name"], json.dumps(body.get("ingredients", []), ensure_ascii=False), - body.get("pricing", 0), body.get("note", ""), user["id"]) + json.dumps(extra) if extra else '{}', body.get("note", ""), user["id"]) ) conn.commit() pid = c.lastrowid @@ -1272,6 +1287,14 @@ def update_project(pid: int, body: dict, user=Depends(require_role("admin", "sen conn.execute("UPDATE profit_projects SET pricing = ? WHERE id = ?", (body["pricing"], pid)) if "note" in body: conn.execute("UPDATE profit_projects SET note = ? WHERE id = ?", (body["note"], pid)) + # Store extra cost fields in pricing as JSON + extra = {} + for k in ("selling_price", "packaging_cost", "labor_cost", "other_cost", "quantity"): + if k in body: + extra[k] = body[k] + if extra: + conn.execute("UPDATE profit_projects SET pricing = ? WHERE id = ?", + (json.dumps(extra, ensure_ascii=False), pid)) conn.commit() conn.close() return {"ok": True}