diff --git a/import_codes.py b/import_codes.py index f38de8d..3bd9927 100644 --- a/import_codes.py +++ b/import_codes.py @@ -34,40 +34,80 @@ from sqlor.dbpools import DBPools async def import_data_json(sor, filepath): - """将单个 data.json 中的 appcodes 和 appcodes_kv 导入数据库""" - with open(filepath, 'r', encoding='utf-8') as f: - data = json.load(f) + """将单个 data.json 中的 appcodes 和 appcodes_kv 导入数据库""" + with open(filepath, 'r', encoding='utf-8') as f: + data = json.load(f) - # 导入 appcodes - appcodes = data.get('appcodes', []) - for item in appcodes: - 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': item['id'], - 'name': item['name'], - 'hierarchy_flg': item.get('hierarchy_flg', '0') - }) - print(f" appcodes: {len(appcodes)} 条") + # 导入 appcodes — 支持两种格式: + # 格式1: [{"id":..., "name":..., "hierarchy_flg":...}] + # 格式2: [{"parentid":..., "parentname":..., "items":[...]}] + appcodes = data.get('appcodes', []) + codes_count = 0 + kv_count = 0 - # 导入 appcodes_kv - appcodes_kv = data.get('appcodes_kv', []) - for item in appcodes_kv: - 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': item['id'], - 'parentid': item['parentid'], - 'k': item['k'], - 'v': item['v'] - }) - print(f" appcodes_kv: {len(appcodes_kv)} 条") + 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(""" + 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': item['id'], + 'name': item['name'], + 'hierarchy_flg': item.get('hierarchy_flg', '0') + }) + codes_count += 1 - return len(appcodes), len(appcodes_kv) + print(f" appcodes: {codes_count} 条") + + # 导入独立 appcodes_kv (格式1兼容) + appcodes_kv = data.get('appcodes_kv', []) + for item in appcodes_kv: + 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': item['id'], + 'parentid': item['parentid'], + 'k': item['k'], + 'v': item['v'] + }) + kv_count += 1 + if appcodes_kv: + print(f" appcodes_kv: {len(appcodes_kv)} 条 (独立)") + + print(f" appcodes_kv: {kv_count} 条 (合计)") + return codes_count, kv_count async def main():