import json, io, os, re
BASE = "/data/www/files"
P = os.path.join(BASE, "projects_20260925.json")
d = json.load(io.open(P, encoding="utf-8"))
g = json.load(io.open(os.path.join(BASE, "ggzy_raw_0925.json"), encoding="utf-8"))["records"]

# report category + best-matching alternative record for each SC project
for r in d:
    if "ggzyjy.sc.gov.cn" not in r["url"]:
        continue
    rel = r["url"].replace("https://ggzyjy.sc.gov.cn", "")
    cat = re.search(r"/jyxx/\d+/(\d+)/", rel)
    r["_cat"] = cat.group(1) if cat else "?"
print("SC categories:", [(r["idx"], r["_cat"], r["type"]) for r in d if "_cat" in r])

# fix idx 10 -> use the real 采购公告 record (002002001), not the 002002005 one
target = None
for u, v in g.items():
    if "互联网企业党建工作项目" in (v.get("title") or "") and v.get("cat") == "002002001":
        target = (u, v)
        break
if target:
    u, v = target
    for r in d:
        if r["idx"] == 10:
            r["url"] = "https://ggzyjy.sc.gov.cn" + u
            r["title"] = v["title"]
            r["pub_date"] = v["date"]
            r["type"] = "招标公告"
            r["info_type"] = "招标公告"
            print("fixed #10 ->", r["title"][:70], r["pub_date"])
            print("   url:", r["url"])
for r in d:
    r.pop("_cat", None)
json.dump(d, io.open(P, "w", encoding="utf-8"), ensure_ascii=False, indent=1)
print("saved. #10:", d[9]["title"][:70])
