fix(import_init): 支持嵌套格式data.json + 加pipeline_core/pipeline-sdlc appcodes导入
This commit is contained in:
parent
1b10c9c8c5
commit
91eac9e119
@ -25,9 +25,39 @@ from ahserver.globalEnv import initEnv
|
||||
INIT_MODULES = [
|
||||
('product_management', 'pkgs/product_management/init/data.json'),
|
||||
('discount', 'pkgs/discount/init/data.json'),
|
||||
('pipeline_core', 'pkgs/pipeline_core/init/data.json'),
|
||||
('pipeline-sdlc', 'pkgs/pipeline-sdlc/init/data.json'),
|
||||
]
|
||||
|
||||
|
||||
def normalize(data):
|
||||
"""兼容两种 data.json 格式,返回 (appcodes, appcodes_kv, organization)。
|
||||
|
||||
扁平格式(product_management/discount):
|
||||
{"appcodes": [{"id","name","hierarchy_flg"}],
|
||||
"appcodes_kv": [{"id","parentid","k","v"}]}
|
||||
嵌套格式(pipeline_core/pipeline-sdlc):
|
||||
{"appcodes": [{"parentid","parentname","items":[{"k","v"}]}]}
|
||||
嵌套格式展开为扁平:appcodes.id=parentid、appcodes_kv.id=f"{parentid}_{k}"(稳定组合键,幂等)。
|
||||
"""
|
||||
raw_ac = data.get('appcodes', [])
|
||||
if raw_ac and isinstance(raw_ac[0], dict) and 'parentid' in raw_ac[0]:
|
||||
appcodes = []
|
||||
appcodes_kv = []
|
||||
for ac in raw_ac:
|
||||
pid = ac['parentid']
|
||||
appcodes.append({'id': pid, 'name': ac.get('parentname', pid), 'hierarchy_flg': '0'})
|
||||
for item in ac.get('items', []):
|
||||
appcodes_kv.append({
|
||||
'id': f"{pid}_{item['k']}",
|
||||
'parentid': pid,
|
||||
'k': item['k'],
|
||||
'v': item['v'],
|
||||
})
|
||||
return appcodes, appcodes_kv, data.get('organization', [])
|
||||
return raw_ac, data.get('appcodes_kv', []), data.get('organization', [])
|
||||
|
||||
|
||||
async def main():
|
||||
config = getConfig(ROOT_DIR, NS={'workdir': ROOT_DIR, 'ProgramPath': ProgramPath()})
|
||||
DBPools(config.databases)
|
||||
@ -43,7 +73,8 @@ async def main():
|
||||
print(f' skip {mod}: {rel} not found')
|
||||
continue
|
||||
data = json.load(open(f, encoding='utf-8'))
|
||||
for ac in data.get('appcodes', []):
|
||||
appcodes, appcodes_kv, organization = normalize(data)
|
||||
for ac in appcodes:
|
||||
recs = await sor.R('appcodes', {'id': ac['id']})
|
||||
if not recs:
|
||||
await sor.C('appcodes', {
|
||||
@ -52,7 +83,7 @@ async def main():
|
||||
'hierarchy_flg': ac.get('hierarchy_flg', '0'),
|
||||
})
|
||||
total['appcodes'] += 1
|
||||
for kv in data.get('appcodes_kv', []):
|
||||
for kv in appcodes_kv:
|
||||
recs = await sor.R('appcodes_kv', {'id': kv['id']})
|
||||
if not recs:
|
||||
await sor.C('appcodes_kv', {
|
||||
@ -62,7 +93,7 @@ async def main():
|
||||
'v': kv.get('v', ''),
|
||||
})
|
||||
total['appcodes_kv'] += 1
|
||||
for org in data.get('organization', []):
|
||||
for org in organization:
|
||||
recs = await sor.R('organization', {'id': org['id']})
|
||||
if not recs:
|
||||
await sor.C('organization', {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user