gaoluyang
21 小时以前 e40a608ef9bf51db294d8eefada6a8e2e80cebca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys,json
from pathlib import Path
from datetime import datetime
from collections import defaultdict
sys.stdout.reconfigure(encoding='utf-8')
sys.path.insert(0,str(Path(__file__).parent/'iqc-import-20261012/deps'))
import pymysql
apply='--apply' in sys.argv
tables=['mes_pro_work_order','mes_pro_work_order_process','mes_pro_route_product','mes_pro_route_process','mes_md_workstation','mes_md_workstation_machine','mes_dv_machinery','mes_pro_task']
with pymysql.connect(host='36.213.150.237',port=10000,user='root',password=sys.stdin.readline().strip(),database='mom-hm',charset='utf8mb4',connect_timeout=15,cursorclass=pymysql.cursors.DictCursor) as c:
 with c.cursor() as q:
  q.execute('SELECT DATABASE() db');assert q.fetchone()['db']=='mom-hm'
  data={}
  for t in tables:
   q.execute('SELECT * FROM '+t+' WHERE deleted=0 ORDER BY id'+(' FOR UPDATE' if apply else ''))
   data[t]=q.fetchall()
  links=defaultdict(set)
  for r in data['mes_md_workstation_machine']:links[r['workstation_id']].add(r['machinery_id'])
  for m in data['mes_dv_machinery']:
   if m['workstation_id']:links[m['workstation_id']].add(m['id'])
  needed_processes=set()
  orders=[o for o in data['mes_pro_work_order'] if o['status']==1 and o['type']==1]
  for o in orders:
   processes=[p for p in data['mes_pro_work_order_process'] if p['work_order_id']==o['id']]
   if not processes:
    route=o['route_id']
    if not route:
     relations=[r for r in data['mes_pro_route_product'] if r.get('item_id')==o['product_id']]
     route=relations[0]['route_id'] if relations else None
    processes=[p for p in data['mes_pro_route_process'] if p['route_id']==route]
   needed_processes.update(p['process_id'] for p in processes)
  needed=set().union(*(links[s['id']] for s in data['mes_md_workstation'] if s['status']==0 and s['process_id'] in needed_processes))
  active=[t for t in data['mes_pro_task'] if t['status'] not in (4,5)]
  occupied=set().union(*(links[t['workstation_id']] for t in active)) if active else set()
  changes=[m for m in data['mes_dv_machinery'] if m['id'] in needed-occupied and m['status']==2]
  print(json.dumps({'confirmedOrders':len(orders),'neededMachines':len(needed),'preservedOccupied':sorted(needed&occupied),'changeToStopped':[{'id':m['id'],'name':m['name']} for m in changes]},ensure_ascii=False))
  if apply:
   out=Path(__file__).parent/'schedule-diagnosis-20261015';out.mkdir(exist_ok=True)
   stamp=datetime.now().strftime('%Y%m%d%H%M%S')
   (out/f'machines-before-{stamp}.json').write_text(json.dumps(changes,ensure_ascii=False,default=str,indent=2),encoding='utf-8')
   for m in changes:
    q.execute('UPDATE mes_dv_machinery SET status=1,update_time=NOW() WHERE id=%s AND deleted=0 AND status=2',(m['id'],));assert q.rowcount==1
   q.execute('SELECT * FROM mes_dv_machinery WHERE deleted=0 ORDER BY id')
   after={m['id']:m for m in q.fetchall()}
   changed={m['id'] for m in changes}
   for old in data['mes_dv_machinery']:
    new=after[old['id']]
    if old['id'] in changed:
     assert new['status']==1
     assert {k:v for k,v in old.items() if k not in ('status','update_time')}=={k:v for k,v in new.items() if k not in ('status','update_time')}
    else:assert new==old
   c.commit()
   print(json.dumps({'updated':len(changes),'preservedOccupied':len(needed&occupied),'verified':True}))