"""Keep output units aligned to native workorder quantities; unit products have their own routes.""" import sys,json from pathlib import Path P=Path(__file__).parent sys.path.insert(0,str(P.parent/'iqc-import-20261012/deps')) import pymysql p=json.loads((P/'binding-plan-with-ids.json').read_text(encoding='utf8')) targets=[(r,st) for r in p['routes'] for st in r['steps'] if st['output_item_id'] is not None] c=pymysql.connect(host='36.213.150.237',port=10000,user='root',password=sys.stdin.readline().strip(),database='mom-hm',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor,connect_timeout=10,read_timeout=60) log=[];before=[] try: with c.cursor() as q: q.execute('SELECT DATABASE() db');assert q.fetchone()['db']=='mom-hm' q.execute("SELECT GET_LOCK('stp-routes-20261012',10) ok");assert q.fetchone()['ok']==1 c.begin() for r,st in targets: q.execute('SELECT * FROM mes_pro_route_process WHERE route_id=%s AND process_id=%s AND deleted=0 FOR UPDATE',(r['id'],st['process_id']));row=q.fetchone() assert row['output_item_id']==st['output_item_id'];before.append(row) sql="UPDATE mes_pro_route_process SET output_item_id=NULL,updater='1' WHERE id=%s" log.append(q.mogrify(sql,(row['id'],))+';');q.execute(sql,(row['id'],));st['output_item_id']=None ids=[r['id'] for r in p['routes']] q.execute('SELECT * FROM mes_pro_route_process WHERE deleted=0 AND route_id IN ('+','.join(['%s']*len(ids))+')',ids) actual=list(q.fetchall());assert all(row['output_item_id'] is None and row['backflush_flag']==b'\x00' for row in actual) q.execute('SELECT COUNT(*) n FROM mes_pro_route_product WHERE deleted=0');assert q.fetchone()['n']==98 q.execute('SELECT COUNT(*) n FROM mes_pro_route_product_bom WHERE deleted=0');assert q.fetchone()['n']==470 q.execute('SELECT COUNT(*) n FROM mes_pro_route WHERE deleted=0 AND status<>1 AND id IN ('+','.join(['%s']*len(ids))+')',ids);assert q.fetchone()['n']==0 c.commit() p['outputs']=[o for o in p['outputs'] if o['product_id'] is not None] result=json.loads((P/'binding-import-result.json').read_text(encoding='utf8')) result['configured_output_rows']=len(p['outputs']);result['output_matches_workorder_product_and_unit']=True;result['verified_after_commit']=True (P/'binding-plan-with-ids.json').write_text(json.dumps(p,ensure_ascii=False,indent=2),encoding='utf8') (P/'binding-import-result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf8') (P/'before-output-finalization.json').write_text(json.dumps(before,ensure_ascii=False,indent=2,default=str),encoding='utf8') with (P/'binding-import-executed.sql').open('a',encoding='utf8') as f:f.write('\nSTART TRANSACTION;\n'+'\n'.join(log)+'\nCOMMIT;\n') print(json.dumps(result,ensure_ascii=False)) q.execute("SELECT RELEASE_LOCK('stp-routes-20261012')") except:c.rollback();raise finally:c.close()