feat: 大屏改为纯流式自适应布局,新增KPI带与产线动态拓扑地图

This commit is contained in:
SunYF
2026-10-28 10:10:17 +08:00
parent 1f10c3091e
commit 4d3c4e2c3a
131 changed files with 18118 additions and 1728 deletions
+203 -1
View File
@@ -1,6 +1,7 @@
package handler
import (
"fmt"
"net/http"
"sort"
"strings"
@@ -57,13 +58,72 @@ func internalDisplayHandler(ctx *svc.ServiceContext) http.HandlerFunc {
locTotal = n
}
usedKeys := map[string]bool{}
if invs, e := ctx.EntClient.Inventory.Query().Where(inventory.QuantityGT(0)).All(ctx0()); e == nil {
invs, invErr := ctx.EntClient.Inventory.Query().Where(inventory.QuantityGT(0)).All(ctx0())
if invErr == nil {
for _, iv := range invs {
usedKeys[locKey(iv.ZoneCode, iv.ShelfNo, "", "")] = true
}
}
locUsed := len(usedKeys)
// ---- 货架级占用(大屏热力图):每货架在库数量 + 批次行数 + 占用率(位置级口径)----
// 占用率 = 已占位置数 / 架内位置总数(zones level=4 位置号行计数);
// 已占位置 = 库存 qty>0 按 区域|货架|层|位置 去重。主数据缺 level=4 时 totalPos=0,前端退化为按存量着色。
type shelfRow struct {
Zone string `json:"zone"`
Shelf string `json:"shelf"`
Name string `json:"name"`
Qty int `json:"qty"`
Batches int `json:"batches"`
Used bool `json:"used"`
UsedPos int `json:"usedPos"`
TotalPos int `json:"totalPos"`
}
shelfTotalPos := map[string]int{}
if z4, e := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(4)).All(ctx0()); e == nil {
for _, z := range z4 {
shelfTotalPos[z.ZoneCode+"|"+z.ShelfNo]++
}
}
posUsedCnt := map[string]int{}
seenPos := map[string]bool{}
if invErr == nil {
for _, iv := range invs {
k := iv.ZoneCode + "|" + iv.ShelfNo
pk := k + "|" + iv.LayerNo + "|" + iv.PositionNo
if !seenPos[pk] {
seenPos[pk] = true
posUsedCnt[k]++
}
}
}
shelfMap := map[string]*shelfRow{}
shelvesOut := []*shelfRow{}
if zs, e := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(2)).All(ctx0()); e == nil {
for _, z := range zs {
key := z.ZoneCode + "|" + z.Code
r := &shelfRow{Zone: z.ZoneCode, Shelf: z.Code, Name: z.Name,
UsedPos: posUsedCnt[key], TotalPos: shelfTotalPos[key]}
shelfMap[z.ZoneCode+"/"+z.Code] = r
shelvesOut = append(shelvesOut, r)
}
}
if invErr == nil {
for _, iv := range invs {
if r, ok := shelfMap[iv.ZoneCode+"/"+iv.ShelfNo]; ok {
r.Qty += iv.Quantity
r.Batches++
r.Used = true
}
}
}
sort.Slice(shelvesOut, func(i, j int) bool {
if shelvesOut[i].Zone != shelvesOut[j].Zone {
return shelvesOut[i].Zone < shelvesOut[j].Zone
}
return shelvesOut[i].Shelf < shelvesOut[j].Shelf
})
// ---- 待处理出入库任务 ----
// 入库待处理 = 入库检未闭环(质量状态 空/未检 且未作废);出库待处理 = 已出库待送达。
pendingInbound := 0
@@ -127,6 +187,7 @@ func internalDisplayHandler(ctx *svc.ServiceContext) http.HandlerFunc {
"zoneDetail": zones,
"locationUsed": locUsed,
"locationTotal": locTotal,
"shelves": shelvesOut,
"pendingInbound": pendingInbound,
"pendingOutbound": pendingOutbound,
"movements": movements,
@@ -135,6 +196,147 @@ func internalDisplayHandler(ctx *svc.ServiceContext) http.HandlerFunc {
}
}
// displayNamedQty 大屏聚合通用行(key/name/qty)
type displayNamedQty struct {
Key int `json:"key"`
Name string `json:"name"`
Qty int `json:"qty"`
}
// displayTrendRow 近7日出入库趋势行(按天聚合数量)
type displayTrendRow struct {
Day string `json:"day"`
In int `json:"in"`
Out int `json:"out"`
}
// internalDisplayAnalyticsHandler 库房大屏聚合数据(近7日趋势/物料分类双维度/库龄分档),免登录。
// 库龄口径(2026-10-27 用户拍板):按入库时间(inventories.created_at)计算「入库至今 N 天」;
// 统计范围 = quantity>0 且状态 在库/锁定 的库存行,按数量加权。
func internalDisplayAnalyticsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
// ---- 近 7 天出入库趋势(按天聚合出库/入库数量,含今天共 7 天)----
dayStart := now.Truncate(24 * time.Hour)
since := dayStart.AddDate(0, 0, -6)
days := make([]displayTrendRow, 7)
for i := 0; i < 7; i++ {
days[i].Day = since.AddDate(0, 0, i).Format("01-02")
}
dayIndexOf := func(unix int64) int {
d := time.Unix(unix, 0).Truncate(24 * time.Hour).Unix()
return int((d - since.Unix()) / 86400)
}
if rows, e := ctx.EntClient.InboundOrder.Query().
Where(inboundorder.Voided(false), inboundorder.CreatedAtGTE(since.Unix())).All(ctx0()); e == nil {
for _, o := range rows {
if i := dayIndexOf(o.CreatedAt); i >= 0 && i < 7 {
days[i].In += o.Quantity
}
}
}
if rows, e := ctx.EntClient.OutboundOrder.Query().
Where(outboundorder.CreatedAtGTE(since.Unix())).All(ctx0()); e == nil {
for _, o := range rows {
if i := dayIndexOf(o.CreatedAt); i >= 0 && i < 7 {
days[i].Out += o.Quantity
}
}
}
// ---- 物料分类(双维度)+ 库龄分档:一次库存扫描同时聚合 ----
// 库龄分档阈值来自基础设置 display.age.buckets(默认 30/90/180,基础设置页可改,保存即生效)
ageTh := AgeBucketsFromConfig(ctx)
mm := map[int]int{} // 类型:1结构件/2电气件/3其他
it := map[int]int{} // 物料类别:1原材料/2半成品/3成品
age := [4]int{} // 0-a / a+1-b / b+1-c / c+ 天
if invs, e := ctx.EntClient.Inventory.Query().
Where(inventory.QuantityGT(0), inventory.StatusIn("在库", "锁定")).All(ctx0()); e == nil {
for _, iv := range invs {
mm[iv.ManageMode] += iv.Quantity
it[iv.Category] += iv.Quantity
ageDays := int((now.Unix() - iv.CreatedAt) / 86400)
switch {
case ageDays <= ageTh[0]:
age[0] += iv.Quantity
case ageDays <= ageTh[1]:
age[1] += iv.Quantity
case ageDays <= ageTh[2]:
age[2] += iv.Quantity
default:
age[3] += iv.Quantity
}
}
}
ok(w, map[string]any{
"trend": days,
"manageMode": []displayNamedQty{
{Key: 1, Name: "结构件", Qty: mm[1]},
{Key: 2, Name: "电气件", Qty: mm[2]},
{Key: 3, Name: "其他", Qty: mm[3]},
},
"itemType": []displayNamedQty{
{Key: 1, Name: "原材料", Qty: it[1]},
{Key: 2, Name: "半成品", Qty: it[2]},
{Key: 3, Name: "成品", Qty: it[3]},
},
"ageBuckets": []displayNamedQty{
{Name: fmt.Sprintf("0-%d天", ageTh[0]), Qty: age[0]},
{Name: fmt.Sprintf("%d-%d天", ageTh[0]+1, ageTh[1]), Qty: age[1]},
{Name: fmt.Sprintf("%d-%d天", ageTh[1]+1, ageTh[2]), Qty: age[2]},
{Name: fmt.Sprintf("%d天以上", ageTh[2]), Qty: age[3]},
},
"time": now.Format("2006-01-02 15:04:05"),
})
}
}
// internalDisplayCallsHandler 大屏备料叫料池(免登录):代理 MES 叫料单,只取三个活跃态。
// 七态口径(MES internal/logic/call.go:33):PENDING 待备料 → SHIPPED 已发货 → DONE 已完成;
// CANCELLED/REJECTED/RETURNING/RETURNED 为分支/退库态,大屏不展示。
// MES 未配置或不可达时静默降级为空列表(大屏不报错),mesOk=false 供前端提示。
func internalDisplayCallsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
calls := []map[string]any{}
mesOk := false
if ctx.Mes != nil {
if list, err := ctx.Mes.ListMaterialRequests(r.Context(), ""); err == nil {
mesOk = true
keep := map[string]string{
"PENDING": "待备料",
"SHIPPED": "配送中",
"DONE": "已送达",
}
for _, m := range list { // MES 已按 created_at desc, id desc 排序(logic/call.go:743)
label, ok2 := keep[m.Status]
if !ok2 {
continue
}
t := ""
if m.CreatedAt != "" {
if tt, e := time.Parse(time.RFC3339, m.CreatedAt); e == nil {
t = tt.Format("15:04")
}
}
mat := m.MaterialName
if mat == "" {
mat = m.MaterialCode
}
calls = append(calls, map[string]any{
"time": t, "stationNo": m.StationNo, "material": mat,
"qty": m.ReqQty, "status": label, "statusKey": m.Status,
})
if len(calls) >= 30 {
break
}
}
}
}
ok(w, map[string]any{"calls": calls, "mesOk": mesOk})
}
}
// materialQueryInternalHandler 物料档案 + 库存查询(看板物料追溯入口复用,X-API-TOKEN 内部接口)
//
// 入参:code(物料编码,必填)/ batch(批次号,可选)。