sage: import_codes.py支持parentid/items格式的data.json

This commit is contained in:
Hermes Agent 2026-06-18 11:24:57 +08:00
parent 612b23f540
commit 5583a12e7d

View File

@ -38,9 +38,44 @@ async def import_data_json(sor, filepath):
with open(filepath, 'r', encoding='utf-8') as f: with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f) data = json.load(f)
# 导入 appcodes # 导入 appcodes — 支持两种格式:
# 格式1: [{"id":..., "name":..., "hierarchy_flg":...}]
# 格式2: [{"parentid":..., "parentname":..., "items":[...]}]
appcodes = data.get('appcodes', []) appcodes = data.get('appcodes', [])
codes_count = 0
kv_count = 0
for item in appcodes: for item in appcodes:
# 格式2: parentid/parentname + 内嵌 items
if 'parentid' in item and 'items' in item:
parentid = item['parentid']
parentname = item.get('parentname', parentid)
await sor.sqlExe("""
INSERT INTO appcodes (id, name, hierarchy_flg)
VALUES (${id}$, ${name}$, ${hierarchy_flg}$)
ON DUPLICATE KEY UPDATE name=VALUES(name), hierarchy_flg=VALUES(hierarchy_flg)
""", {
'id': parentid,
'name': parentname,
'hierarchy_flg': item.get('hierarchy_flg', '0')
})
codes_count += 1
# 导入内嵌的 kv 条目
for kv_item in item.get('items', []):
kv_id = f"{parentid}_{kv_item['k']}"
await sor.sqlExe("""
INSERT INTO appcodes_kv (id, parentid, k, v)
VALUES (${id}$, ${parentid}$, ${k}$, ${v}$)
ON DUPLICATE KEY UPDATE id=VALUES(id), v=VALUES(v)
""", {
'id': kv_id,
'parentid': parentid,
'k': kv_item['k'],
'v': kv_item['v']
})
kv_count += 1
# 格式1: 标准 id/name
elif 'id' in item:
await sor.sqlExe(""" await sor.sqlExe("""
INSERT INTO appcodes (id, name, hierarchy_flg) INSERT INTO appcodes (id, name, hierarchy_flg)
VALUES (${id}$, ${name}$, ${hierarchy_flg}$) VALUES (${id}$, ${name}$, ${hierarchy_flg}$)
@ -50,9 +85,11 @@ async def import_data_json(sor, filepath):
'name': item['name'], 'name': item['name'],
'hierarchy_flg': item.get('hierarchy_flg', '0') 'hierarchy_flg': item.get('hierarchy_flg', '0')
}) })
print(f" appcodes: {len(appcodes)}") codes_count += 1
# 导入 appcodes_kv print(f" appcodes: {codes_count}")
# 导入独立 appcodes_kv (格式1兼容)
appcodes_kv = data.get('appcodes_kv', []) appcodes_kv = data.get('appcodes_kv', [])
for item in appcodes_kv: for item in appcodes_kv:
await sor.sqlExe(""" await sor.sqlExe("""
@ -65,9 +102,12 @@ async def import_data_json(sor, filepath):
'k': item['k'], 'k': item['k'],
'v': item['v'] 'v': item['v']
}) })
print(f" appcodes_kv: {len(appcodes_kv)}") kv_count += 1
if appcodes_kv:
print(f" appcodes_kv: {len(appcodes_kv)} 条 (独立)")
return len(appcodes), len(appcodes_kv) print(f" appcodes_kv: {kv_count} 条 (合计)")
return codes_count, kv_count
async def main(): async def main():