fix: 新项目售价299和批量1持久化到后端
All checks were successful
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 4s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 10s
Test / e2e-test (push) Successful in 53s

- 后端存储selling_price/packaging_cost/labor_cost/other_cost/quantity到pricing字段(JSON)
- 加载时解析并返回给前端
- 新建时也保存这些字段

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-11 10:34:41 +00:00
parent 4f3f39967c
commit c58b925ab4

View File

@@ -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}