2026-08-28 15:06:01 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-11-30 15:25:29 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
"strings"
|
2026-08-28 15:06:01 +08:00
|
|
|
"time"
|
|
|
|
|
|
2026-11-30 15:25:29 +08:00
|
|
|
"bj_power_mes/ent/dock"
|
2026-08-28 15:06:01 +08:00
|
|
|
"bj_power_mes/internal/svc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Service 业务逻辑服务(handler 薄层调用)
|
|
|
|
|
type Service struct {
|
|
|
|
|
ctx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(s *svc.ServiceContext) *Service {
|
|
|
|
|
return &Service{ctx: s}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 17:31:18 +08:00
|
|
|
// notifyDashboard 生产业务数据落库成功后广播看板刷新事件。
|
|
|
|
|
// SSE 事件只做"数据变了"的通知(空载荷),大屏收到后自行拉取最新快照,
|
|
|
|
|
// 避免把整份快照塞进事件流。无订阅客户端时 Publish 为空操作,零开销。
|
|
|
|
|
func (s *Service) notifyDashboard() {
|
|
|
|
|
if s.ctx.SSE != nil {
|
|
|
|
|
s.ctx.SSE.Publish("dashboard.updated", "{}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-30 15:25:29 +08:00
|
|
|
// stationMaterialEvent 工位物料流水刷新事件载荷:只带"哪个工位、哪类变化、哪张单",
|
|
|
|
|
// 不带整份明细,终端收到后自行补拉叫料池/叫料单/物料明细/退料单四接口。
|
|
|
|
|
type stationMaterialEvent struct {
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
|
RefNo string `json:"refNo,omitempty"`
|
|
|
|
|
Dock string `json:"dock,omitempty"`
|
|
|
|
|
DockSide string `json:"dockSide,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// notifyStationMaterial 物料/叫料/退料/AGV 事件统一推送口(Q2/Q3 两件套)。
|
|
|
|
|
// 事件 kind 全集:call.created / call.borrow / call.cancelled / call.delivered /
|
|
|
|
|
// call.signed / call.rejected / call.return_requested / call.prepared /
|
|
|
|
|
// return.created / return.cancelled / return.received / return.adjusted /
|
|
|
|
|
// agv.dispatched / agv.arrived。
|
|
|
|
|
func (s *Service) notifyStationMaterial(stationNo int, kind, refNo string) {
|
|
|
|
|
if s.ctx.SSE == nil || stationNo <= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(stationMaterialEvent{StationNo: stationNo, Kind: kind, RefNo: refNo})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.ctx.SSE.Publish("station.material", string(b))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// notifyStationArrival 送料 AGV 到位专用事件:带上目标接驳台的左/右方位,
|
|
|
|
|
// 工位终端据此按用户原话播报「工位X左/右接驳台到货」;退料回库到位不走这里。
|
|
|
|
|
func (s *Service) notifyStationArrival(ctx context.Context, dockCode, refNo string) {
|
|
|
|
|
stationNo := s.stationNoByDock(ctx, dockCode)
|
|
|
|
|
if s.ctx.SSE == nil || stationNo <= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
side := ""
|
|
|
|
|
if s.ctx.EntClient != nil && strings.TrimSpace(dockCode) != "" {
|
|
|
|
|
if d, err := s.ctx.EntClient.Dock.Query().Where(dock.DockCodeEQ(dockCode)).First(ctx); err == nil && d != nil {
|
|
|
|
|
switch strings.ToLower(d.Side) {
|
|
|
|
|
case "left":
|
|
|
|
|
side = "左"
|
|
|
|
|
case "right":
|
|
|
|
|
side = "右"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(stationMaterialEvent{StationNo: stationNo, Kind: "agv.arrived", RefNo: refNo, Dock: dockCode, DockSide: side})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.ctx.SSE.Publish("station.material", string(b))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stationNoByDock 从接驳台反查工位(AGV 到位/派车事件用;查不到返回 0=不推)。
|
|
|
|
|
func (s *Service) stationNoByDock(ctx context.Context, dockCode string) int {
|
|
|
|
|
if s.ctx.EntClient == nil || strings.TrimSpace(dockCode) == "" {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
d, err := s.ctx.EntClient.Dock.Query().Where(dock.DockCodeEQ(dockCode)).First(ctx)
|
|
|
|
|
if err != nil || d == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return d.StationNo
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-09 18:58:47 +08:00
|
|
|
// cachedTyped 看板进程内缓存:
|
2026-11-07 15:06:00 +08:00
|
|
|
// TTL + singleflight 防击穿,返回强类型,调用方不必再做类型断言(看板快照等结构化数据用这个)。
|
2026-11-09 18:58:47 +08:00
|
|
|
// 零调用的旧 cached(any) 死代码已一并删除。
|
2026-09-04 14:18:53 +08:00
|
|
|
func cachedTyped[T any](ctx context.Context, s *Service, key string, ttl int, build func() (T, error)) (T, error) {
|
2026-11-07 15:06:00 +08:00
|
|
|
v, err := dashboardCache.getOrBuild(key, time.Duration(ttl)*time.Second, func() (any, error) {
|
2026-09-04 14:18:53 +08:00
|
|
|
return build()
|
2026-11-07 15:06:00 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
var zero T
|
|
|
|
|
return zero, err
|
2026-09-04 14:18:53 +08:00
|
|
|
}
|
2026-11-07 15:06:00 +08:00
|
|
|
return v.(T), nil
|
2026-08-28 15:06:01 +08:00
|
|
|
}
|