Files
bj_power/bj_power_mes/_fix_station.py
T

96 lines
2.7 KiB
Python
Raw Normal View History

p='internal/logic/processflow.go'
lines=open(p,encoding='utf-8').read().split('\n')
# find function start
start=None
for i,l in enumerate(lines):
if l.startswith('func (s *Service) StationTask('):
start=i
break
assert start is not None, 'func not found'
# find matching close brace
depth=0
end=None
for j in range(start,len(lines)):
depth+=lines[j].count('{')
depth-=lines[j].count('}')
if depth==0 and j>start:
end=j
break
assert end is not None, 'no end'
func='''func (s *Service) StationTask(ctx context.Context, stationNo int) (map[string]any, error) {
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(stationNo)).First(ctx)
if err != nil {
return nil, errors.New("工位不存在或未配置")
}
var flow *ent.ProcessFlow
if st.FlowId > 0 {
flow, _ = s.ctx.EntClient.ProcessFlow.Get(ctx, st.FlowId)
}
steps := []*ProcessStepTemplate{}
if flow != nil && flow.Status == "ACTIVE" {
steps = s.flowSteps(ctx, flow.ID)
}
orderNos := []string{}
wos, _ := s.ctx.EntClient.WorkOrder.Query().
Where(workorder.StatusIn("CREATED", "RELEASED", "IN_PROGRESS")).
Order(ent.Asc(workorder.FieldID)).Limit(10).All(ctx)
for _, w := range wos {
orderNos = append(orderNos, w.WorkOrderNo)
}
// M4:工位终端按"工艺路线段"驱动(段=并行工位组,谁空停谁)。
// 解析活跃工单的 routeSnapshot,找出本工位所属的路线段,供终端按段执行流程。
routeSegments := []map[string]any{}
for _, w := range wos {
if len(w.RouteSnapshot) == 0 {
continue
}
for _, seg := range w.RouteSnapshot {
stations, _ := seg["stations"].([]any)
inSeg := false
for _, sv := range stations {
switch n := sv.(type) {
case float64:
if int(n) == stationNo {
inSeg = true
}
case int:
if n == stationNo {
inSeg = true
}
}
if inSeg {
break
}
}
if !inSeg {
continue
}
flowId := 0
if fv, ok := seg["flowId"].(float64); ok {
flowId = int(fv)
} else if fv, ok := seg["flowId"].(int); ok {
flowId = fv
}
routeSegments = append(routeSegments, map[string]any{
"orderNo": w.WorkOrderNo,
"seq": seg["seq"],
"segmentType": seg["segmentType"],
"flowId": flowId,
"stations": stations,
})
}
}
return map[string]any{
"stationNo": st.StationNo,
"stationName": st.Name,
"flow": flow,
"flowActive": flow != nil && flow.Status == "ACTIVE",
"steps": steps,
"orderNos": orderNos,
"routeSegments": routeSegments,
}, nil
}'''
out=lines[:start]+func.split('\n')+lines[end+1:]
open(p,'w',encoding='utf-8').write('\n'.join(out))
print("OK StationTask rewritten")