from pathlib import Path import sys,re,json sys.stdout.reconfigure(encoding='utf-8') sys.path.insert(0,str(Path('scripts/iqc-import-20261012/deps').resolve())) import pymysql s=Path('yudao-server/src/main/resources/application-local.yaml').read_text(encoding='utf-8') x=s[s.index('jdbc:mysql://localhost:3306/'):] pw=re.search(r'password:\s*([^\r\n#]+)',x).group(1).strip().strip('\"\'') c=pymysql.connect(host='127.0.0.1',port=3306,user='root',password=pw,database='mom-hm',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor,autocommit=False) try: with c.cursor() as q: if '--seed' in sys.argv: from decimal import Decimal out=Path('scripts/local-production-20261015');out.mkdir(exist_ok=True) assert not (out/'result.json').exists(), 'Already seeded; inspect result.json instead of duplicating' q.execute('select count(*) n from mes_pro_task where deleted=0 and (status is null or status not in (4,5))') assert q.fetchone()['n']==0, 'Active tasks require resource review' q.execute('select distinct m.* from mes_dv_machinery m left join mes_md_workstation_machine wm on wm.machinery_id=m.id and wm.deleted=0 where m.deleted=0 and (m.workstation_id in (11,12,13) or wm.workstation_id in (11,12,13)) for update') machines=q.fetchall() assert machines and all(m['status'] in (1,2) and m['production_efficiency'] and m['production_efficiency']>0 for m in machines) (out/'equipment-before.json').write_text(json.dumps(machines,ensure_ascii=False,indent=2,default=str),encoding='utf-8') ids=[]; summary=[] for product,route,qty in [(497,36,200),(507,34,300),(497,36,500)]: q.execute('select * from mes_md_item where id=%s and deleted=0 and status=0',(product,));item=q.fetchone();assert item q.execute('select * from mes_pro_route_process where route_id=%s and deleted=0 order by sort',(route,));processes=q.fetchall();assert len(processes)==3 for p in processes: q.execute('select count(*) n from mes_pro_process where id=%s and deleted=0 and status=0',(p['process_id'],));assert q.fetchone()['n']==1 q.execute('select count(*) n from mes_pd_process_param_template_detail where template_id=%s and deleted=0',(p['param_template_id'],));assert q.fetchone()['n']>0 q.execute("select result from mes_md_auto_code_record where rule_id=30 and result like 'WO_20261015%' union select code result from mes_pro_work_order where code like 'WO_20261015%'") serial=max([int(r['result'][-3:]) for r in q.fetchall()] or [0])+1 code=f'WO_20261015{serial:03d}' q.execute('insert into mes_pro_work_order(code,name,type,order_source_type,product_id,quantity,quantity_produced,quantity_changed,quantity_scheduled,route_id,auto_finish_flag,request_date,status,creator,updater,is_feedback_approve,parent_id) values(%s,%s,1,2,%s,%s,0,0,0,%s,1,%s,1,%s,%s,0,0)',(code,item['name']+'生产工单',product,qty,route,'2026-10-23 18:00:00','244','244')) wid=q.lastrowid;ids.append(wid) q.execute('insert into mes_md_auto_code_record(rule_id,result,serial_no,creator,updater) values(30,%s,%s,%s,%s)',(code,serial,'244','244')) cols=['process_id','param_template_id','yield_rate','sort','next_process_id','link_type','prepare_time','wait_time','color_code','key_flag','check_flag','backflush_flag','output_item_id','remark'] for p in processes: q.execute('insert into mes_pro_work_order_process(work_order_id,'+','.join(cols)+',creator,updater) values('+','.join(['%s']*(len(cols)+3))+')',[wid]+[p[k] for k in cols]+['244','244']) q.execute('select bom_item_id,quantity from mes_md_product_bom where item_id=%s and deleted=0',(product,));bom=q.fetchall();assert bom for b in bom: q.execute('select id from mes_md_item where id=%s and deleted=0',(b['bom_item_id'],));assert q.fetchone() q.execute('insert into mes_pro_work_order_bom(work_order_id,item_id,quantity,creator,updater) values(%s,%s,%s,%s,%s)',(wid,b['bom_item_id'],b['quantity']*Decimal(qty),'244','244')) summary.append(dict(id=wid,code=code,product=item['name'],quantity=qty,route_id=route,process_count=len(processes),bom_count=len(bom))) for m in machines: if m['status']==2:q.execute('update mes_dv_machinery set status=1 where id=%s and status=2',(m['id'],));assert q.rowcount==1 q.execute('select count(*) n from mes_pro_task where deleted=0 and work_order_id in ('+','.join(['%s']*len(ids))+')',ids);assert q.fetchone()['n']==0 c.commit() (out/'result.json').write_text(json.dumps({'database':'localhost:3306/mom-hm','orders':summary,'equipment_ids':[m['id'] for m in machines],'tasks_created':0},ensure_ascii=False,indent=2),encoding='utf-8') print(json.dumps(summary,ensure_ascii=False));sys.exit(0) for sql in sys.argv[1:]: q.execute(sql);print(json.dumps(q.fetchall(),ensure_ascii=False,default=str)) finally: c.rollback();c.close()