2026-08-28 15:06:01 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-28 10:10:17 +08:00
|
|
|
|
"fmt"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"net/http"
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"sort"
|
|
|
|
|
|
"strings"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"bj_power_wms/ent"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"bj_power_wms/ent/inboundorder"
|
2026-09-02 08:21:11 +08:00
|
|
|
|
"bj_power_wms/ent/inventory"
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"bj_power_wms/ent/material"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"bj_power_wms/ent/outboundorder"
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"bj_power_wms/ent/zone"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"bj_power_wms/internal/svc"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// internalDisplayHandler 库存总览(55寸大屏免登录用)
|
|
|
|
|
|
func internalDisplayHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2026-10-10 15:58:59 +08:00
|
|
|
|
// 批次库存(序列号=否)总量(按质量状态)
|
2026-09-30 17:33:11 +08:00
|
|
|
|
batches, _ := ctx.EntClient.Inventory.Query().Where(inventory.SnModeEQ(snModeBatch)).All(ctx0())
|
2026-08-28 15:06:01 +08:00
|
|
|
|
totalQty := 0
|
|
|
|
|
|
lockedQty := 0
|
|
|
|
|
|
type zoneRow struct {
|
|
|
|
|
|
Zone, Quality string
|
|
|
|
|
|
Qty int
|
|
|
|
|
|
}
|
|
|
|
|
|
zones := map[string]int{}
|
|
|
|
|
|
batchCount := len(batches)
|
|
|
|
|
|
for _, b := range batches {
|
|
|
|
|
|
totalQty += b.Quantity
|
|
|
|
|
|
lockedQty += b.LockedQty
|
2026-09-02 08:21:11 +08:00
|
|
|
|
zc := b.ZoneCode
|
|
|
|
|
|
if zc == "" {
|
|
|
|
|
|
zc = "未分区"
|
|
|
|
|
|
}
|
|
|
|
|
|
zones[zc+"/"+b.QualityStatus] += b.Quantity
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
2026-09-30 17:33:11 +08:00
|
|
|
|
snInStock, _ := ctx.EntClient.Inventory.Query().Where(inventory.SnModeEQ(snModeSN), inventory.StatusIn("在库", "锁定")).Count(ctx0())
|
|
|
|
|
|
snOut, _ := ctx.EntClient.Inventory.Query().Where(inventory.SnModeEQ(snModeSN), inventory.StatusEQ("出库")).Count(ctx0())
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// 半成品统计:统一库存主表 category=2(semi_finisheds 已收编废弃)
|
|
|
|
|
|
semiInStock, _ := ctx.EntClient.Inventory.Query().Where(inventory.CategoryEQ(invCatSemi), inventory.StatusEQ("在库")).Count(ctx0())
|
|
|
|
|
|
// 成品在库统计:category=3
|
|
|
|
|
|
finishedInStock, _ := ctx.EntClient.Inventory.Query().Where(inventory.CategoryEQ(invCatFinished), inventory.StatusIn("在库", "锁定")).Count(ctx0())
|
2026-09-03 14:20:37 +08:00
|
|
|
|
pkgCount, _ := ctx.EntClient.OutboundOrder.Query().Where(outboundorder.BoxNoNEQ("")).Count(ctx0())
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
dayStart := time.Now().Truncate(24 * time.Hour).Unix()
|
|
|
|
|
|
inToday, _ := ctx.EntClient.InboundOrder.Query().
|
|
|
|
|
|
Where(inboundorder.CreatedAtGTE(dayStart)).Count(ctx0())
|
|
|
|
|
|
outToday, _ := ctx.EntClient.OutboundOrder.Query().
|
|
|
|
|
|
Where(outboundorder.CreatedAtGTE(dayStart)).Count(ctx0())
|
|
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// ---- 库位利用率:库位 = 货架(区域→货架→层→位置 中的「货架」物理存储单元)----
|
|
|
|
|
|
locTotal := 0
|
|
|
|
|
|
if n, e := ctx.EntClient.Zone.Query().Where(zone.LevelEQ(2)).Count(ctx0()); e == nil {
|
|
|
|
|
|
locTotal = n
|
|
|
|
|
|
}
|
|
|
|
|
|
usedKeys := map[string]bool{}
|
2026-10-28 10:10:17 +08:00
|
|
|
|
invs, invErr := ctx.EntClient.Inventory.Query().Where(inventory.QuantityGT(0)).All(ctx0())
|
|
|
|
|
|
if invErr == nil {
|
2026-10-20 16:36:26 +08:00
|
|
|
|
for _, iv := range invs {
|
|
|
|
|
|
usedKeys[locKey(iv.ZoneCode, iv.ShelfNo, "", "")] = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
locUsed := len(usedKeys)
|
|
|
|
|
|
|
2026-10-28 10:10:17 +08:00
|
|
|
|
// ---- 货架级占用(大屏热力图):每货架在库数量 + 批次行数 + 占用率(位置级口径)----
|
|
|
|
|
|
// 占用率 = 已占位置数 / 架内位置总数(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
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// ---- 待处理出入库任务 ----
|
|
|
|
|
|
// 入库待处理 = 入库检未闭环(质量状态 空/未检 且未作废);出库待处理 = 已出库待送达。
|
|
|
|
|
|
pendingInbound := 0
|
|
|
|
|
|
if n, e := ctx.EntClient.InboundOrder.Query().
|
|
|
|
|
|
Where(inboundorder.Voided(false), inboundorder.QualityStatusIn("未检", "")).Count(ctx0()); e == nil {
|
|
|
|
|
|
pendingInbound = n
|
|
|
|
|
|
}
|
|
|
|
|
|
pendingOutbound := 0
|
|
|
|
|
|
if n, e := ctx.EntClient.OutboundOrder.Query().
|
|
|
|
|
|
Where(outboundorder.StatusEQ("已出库")).Count(ctx0()); e == nil {
|
|
|
|
|
|
pendingOutbound = n
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---- 出入库动态(带批次号,最近各 15 单合并取前 20)----
|
|
|
|
|
|
movements := []map[string]any{}
|
|
|
|
|
|
inb, _ := ctx.EntClient.InboundOrder.Query().Where(inboundorder.Voided(false)).
|
|
|
|
|
|
Order(ent.Desc(inboundorder.FieldCreatedAt)).Limit(15).All(ctx0())
|
|
|
|
|
|
outb, _ := ctx.EntClient.OutboundOrder.Query().
|
|
|
|
|
|
Order(ent.Desc(outboundorder.FieldCreatedAt)).Limit(15).All(ctx0())
|
|
|
|
|
|
type mv struct{ t int64; m map[string]any }
|
|
|
|
|
|
all := make([]mv, 0, len(inb)+len(outb))
|
|
|
|
|
|
for _, o := range inb {
|
|
|
|
|
|
mat := o.MaterialName
|
|
|
|
|
|
if mat == "" {
|
|
|
|
|
|
mat = o.MaterialCode
|
|
|
|
|
|
}
|
|
|
|
|
|
all = append(all, mv{t: o.CreatedAt, m: map[string]any{
|
|
|
|
|
|
"id": o.InboundNo, "time": time.Unix(o.CreatedAt, 0).Format("2006-01-02 15:04:05"),
|
|
|
|
|
|
"type": "in", "material": mat, "qty": o.Quantity, "operator": o.Operator, "batch": o.BatchNo,
|
|
|
|
|
|
}})
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, o := range outb {
|
|
|
|
|
|
mat := o.MaterialName
|
|
|
|
|
|
if mat == "" {
|
|
|
|
|
|
mat = o.MaterialCode
|
|
|
|
|
|
}
|
|
|
|
|
|
all = append(all, mv{t: o.CreatedAt, m: map[string]any{
|
|
|
|
|
|
"id": o.OutboundNo, "time": time.Unix(o.CreatedAt, 0).Format("2006-01-02 15:04:05"),
|
|
|
|
|
|
"type": "out", "material": mat, "qty": o.Quantity, "operator": o.Operator, "batch": o.BatchNo,
|
|
|
|
|
|
}})
|
|
|
|
|
|
}
|
|
|
|
|
|
sort.Slice(all, func(i, j int) bool { return all[i].t > all[j].t })
|
|
|
|
|
|
if len(all) > 20 {
|
|
|
|
|
|
all = all[:20]
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, x := range all {
|
|
|
|
|
|
movements = append(movements, x.m)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
ok(w, map[string]any{
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"batchCount": batchCount,
|
|
|
|
|
|
"totalQty": totalQty,
|
|
|
|
|
|
"lockedQty": lockedQty,
|
|
|
|
|
|
"snInStock": snInStock,
|
|
|
|
|
|
"snOut": snOut,
|
|
|
|
|
|
"semiInStock": semiInStock,
|
2026-09-08 11:44:04 +08:00
|
|
|
|
"finishedInStock": finishedInStock,
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"packageCount": pkgCount,
|
|
|
|
|
|
"inToday": inToday,
|
|
|
|
|
|
"outToday": outToday,
|
|
|
|
|
|
"zoneDetail": zones,
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"locationUsed": locUsed,
|
|
|
|
|
|
"locationTotal": locTotal,
|
2026-10-28 10:10:17 +08:00
|
|
|
|
"shelves": shelvesOut,
|
2026-10-20 16:36:26 +08:00
|
|
|
|
"pendingInbound": pendingInbound,
|
|
|
|
|
|
"pendingOutbound": pendingOutbound,
|
|
|
|
|
|
"movements": movements,
|
2026-09-30 17:33:11 +08:00
|
|
|
|
"time": time.Now().Format("2006-01-02 15:04:05"),
|
2026-08-28 15:06:01 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-08 11:44:04 +08:00
|
|
|
|
|
2026-10-28 10:10:17 +08:00
|
|
|
|
// 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})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-20 16:36:26 +08:00
|
|
|
|
// materialQueryInternalHandler 物料档案 + 库存查询(看板物料追溯入口复用,X-API-TOKEN 内部接口)
|
|
|
|
|
|
//
|
|
|
|
|
|
// 入参:code(物料编码,必填)/ batch(批次号,可选)。
|
|
|
|
|
|
// 返回:found(档案是否存在)+ material(档案字段)+ inventory(在库库存行,含批次/库位/质量状态)。
|
|
|
|
|
|
func materialQueryInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
code := strings.TrimSpace(r.URL.Query().Get("code"))
|
|
|
|
|
|
batch := strings.TrimSpace(r.URL.Query().Get("batch"))
|
|
|
|
|
|
resp := map[string]any{"code": code, "found": false, "material": nil, "inventory": []map[string]any{}}
|
|
|
|
|
|
if code == "" {
|
|
|
|
|
|
ok(w, resp)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if m, e := ctx.EntClient.Material.Query().Where(material.CodeEQ(code)).First(ctx0()); e == nil && m != nil {
|
|
|
|
|
|
resp["found"] = true
|
|
|
|
|
|
resp["material"] = map[string]any{
|
|
|
|
|
|
"code": m.Code, "name": m.Name, "spec": m.Spec, "itemType": m.ItemType,
|
|
|
|
|
|
"unit": m.Unit, "manageMode": m.ManageMode, "snMode": m.SnMode,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
iq := ctx.EntClient.Inventory.Query().Where(inventory.MaterialCodeEQ(code))
|
|
|
|
|
|
if batch != "" {
|
|
|
|
|
|
iq = iq.Where(inventory.BatchNoEQ(batch))
|
|
|
|
|
|
}
|
|
|
|
|
|
if invs, e := iq.Where(inventory.QuantityGT(0)).
|
|
|
|
|
|
Order(ent.Desc(inventory.FieldCreatedAt)).Limit(50).All(ctx0()); e == nil {
|
|
|
|
|
|
rows := make([]map[string]any, 0, len(invs))
|
|
|
|
|
|
for _, iv := range invs {
|
|
|
|
|
|
rows = append(rows, map[string]any{
|
|
|
|
|
|
"zone": iv.ZoneCode, "shelf": iv.ShelfNo, "layer": iv.LayerNo, "position": iv.PositionNo,
|
|
|
|
|
|
"quality": iv.QualityStatus, "qty": iv.Quantity, "batch": iv.BatchNo,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
resp["inventory"] = rows
|
|
|
|
|
|
}
|
|
|
|
|
|
ok(w, resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-05 20:15:38 +08:00
|
|
|
|
// finishedInboundRequest 成品入库请求(MES 完工回流内部接口 与 WMS 手工成品入库页 共用契约)
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// 定稿(2026-09-07):成品 SN 由 MES 系统生成(系统必发号,实物可不贴码,允许手填覆盖),
|
2026-10-10 15:58:59 +08:00
|
|
|
|
// SN 与实物码解耦;完工品落 WMS 统一库存主表(sn_mode=1 序列号=是SN单件, category=3 成品)。
|
2026-10-05 20:15:38 +08:00
|
|
|
|
// 方案A(2026-10-04):完工检合格才入成品库,入库解耦为双通道——
|
|
|
|
|
|
// ① MES 完工检合格调 /api/internal/finished/inbound 自动回流;② 库房在「成品入库」页手工录入(/api/finished/inbound)。
|
|
|
|
|
|
// 归属三字段与产品状态/相关标准/试验记录单号/记录员/记录日期均可空,落库写法照 semiInboundHandler
|
|
|
|
|
|
// (归属类型经 ownershipTypeOrNone 归一化,其余 Nillable 空=不落)。
|
2026-09-08 11:44:04 +08:00
|
|
|
|
type finishedInboundRequest struct {
|
|
|
|
|
|
ProductCode string `json:"productCode"` // 产品编码(成品物料编码)
|
|
|
|
|
|
ProductName string `json:"productName"` // 产品名称(档案缺失时兜底展示)
|
|
|
|
|
|
Sn string `json:"sn"` // 成品 SN(系统生成或手填,全局唯一)
|
|
|
|
|
|
OrderNo string `json:"orderNo"` // 关联工单号
|
|
|
|
|
|
ZoneCode string `json:"zoneCode"` // 入库区域(可空=未分区)
|
2026-10-05 20:15:38 +08:00
|
|
|
|
QualityStatus string `json:"qualityStatus"` // 质量状态(默认 合格;MES 完工检按产品状态传入:已通过→合格/部分通过→合格/返修→不合格,后端尊重传入值不重复映射)
|
2026-09-08 11:44:04 +08:00
|
|
|
|
SnSource string `json:"snSource"` // AUTO=系统生成 / MANUAL=手填,仅记录
|
|
|
|
|
|
Operator string `json:"operator"`
|
2026-10-05 20:15:38 +08:00
|
|
|
|
// 归属三字段(照 semiInboundHandler 口径)
|
|
|
|
|
|
OwnershipType string `json:"ownershipType"` // workorder=合同号 / project=科技项目号 / other=其他
|
|
|
|
|
|
OwnershipNo string `json:"ownershipNo"` // 归属编号
|
|
|
|
|
|
ProductWorkNo string `json:"productWorkNo"` // 产品工号
|
|
|
|
|
|
// 成品质量档案(客户问题记录 L218 九字段)
|
|
|
|
|
|
ProductStatus string `json:"productStatus"` // 已通过/部分通过/返修
|
|
|
|
|
|
RelatedStandard string `json:"relatedStandard"` // 相关标准(试验方案号)
|
|
|
|
|
|
TestRecordNo string `json:"testRecordNo"` // 试验记录单号
|
|
|
|
|
|
RecordedBy string `json:"recordedBy"` // 记录员(手工入库必填;MES 回流可空)
|
|
|
|
|
|
RecordDate string `json:"recordDate"` // 记录日期 YYYY-MM-DD(可空;手工入库默认今天)
|
|
|
|
|
|
Remark string `json:"remark"` // 备注(手工入库用;MES 回流留空走默认文案)
|
2026-09-08 11:44:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-05 20:15:38 +08:00
|
|
|
|
// finishedInboundHandler 成品完工回流入库(MES 完工检合格后调用,X-API-TOKEN 内部接口)
|
|
|
|
|
|
// 创建逻辑抽至 createFinishedInventory(finished_inbound.go),与手工成品入库共享。
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// 物料档案缺失时不阻塞回流(库存行 material_code 仍落产品编码,名称用请求传入兜底),
|
|
|
|
|
|
// 记 eventlog 提示补建档——编码主数据仍以 WMS 为单一来源,产线不被档案缺失卡死。
|
|
|
|
|
|
func finishedInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req finishedInboundRequest
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-10-05 20:15:38 +08:00
|
|
|
|
rec, code, err := createFinishedInventory(ctx, &req, sourceMES)
|
2026-09-08 11:44:04 +08:00
|
|
|
|
if err != nil {
|
2026-10-05 20:15:38 +08:00
|
|
|
|
fail(w, code, err.Error())
|
2026-09-08 11:44:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
ok(w, rec)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func snSourceLabel(s string) string {
|
|
|
|
|
|
if s == "MANUAL" {
|
|
|
|
|
|
return "手填"
|
|
|
|
|
|
}
|
|
|
|
|
|
return "系统生成"
|
|
|
|
|
|
}
|