2026-09-10 16:59:15 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_workstation/internal/svc"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-05 20:15:38 +08:00
|
|
|
|
// ============ 产线点位 / 叫料(代理 MES 内部 API) ============
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// MES 侧对应接口均以 X-API-TOKEN 保护(/api/internal/line/*),本服务用配置 token 转发,
|
|
|
|
|
|
// 前端只与本服务交互(JWT 登录)。MES 响应体原样透传({code,message,data})。
|
|
|
|
|
|
|
|
|
|
|
|
// mesJSON 透传 MES 响应体;网络不可达时返回 502 中文提示。
|
|
|
|
|
|
func mesJSON(w http.ResponseWriter, body []byte, err error) bool {
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
_, _ = w.Write(body)
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// linePointsHandler GET /api/line/points(产线点位台账:当前每个点位上的工件)
|
|
|
|
|
|
func linePointsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
body, _, err := ctx.Mes.Get("/api/internal/line/points", nil)
|
|
|
|
|
|
mesJSON(w, body, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// lineOccupyHandler POST /api/line/occupy(点位人工记账:IN 上料 / OUT 下料)
|
|
|
|
|
|
// 请求体:{pointNo, sn, orderNo, action} —— operator 由本服务补当前登录人。
|
|
|
|
|
|
func lineOccupyHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
payload, err := io.ReadAll(r.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体读取失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var req map[string]any
|
|
|
|
|
|
if err := json.Unmarshal(payload, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req["operator"] == nil || req["operator"] == "" {
|
|
|
|
|
|
if u := currentUsername(r); u != "" {
|
|
|
|
|
|
req["operator"] = u
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
body, _ := json.Marshal(req)
|
|
|
|
|
|
resp, _, err := ctx.Mes.Post("/api/internal/line/occupy", json.RawMessage(body))
|
|
|
|
|
|
mesJSON(w, resp, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// ============ B5 叫料(代理 MES 内部 API,2026-10-13 定稿) ============
|
2026-10-28 10:10:17 +08:00
|
|
|
|
// 接驳台:每行可选(本工位 左L/右R 两个接驳台),未选时用 yaml Station.Dock 配置兜底(本工位固定接驳台)。
|
2026-10-14 17:24:59 +08:00
|
|
|
|
|
|
|
|
|
|
// callPoolHandler GET /api/call/pool 叫料池(本工位物料行四数:需求/调整/已发/占用/可用)
|
|
|
|
|
|
func callPoolHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
q := url.Values{}
|
|
|
|
|
|
if v := strings.TrimSpace(r.URL.Query().Get("stationNo")); v != "" {
|
|
|
|
|
|
q.Set("stationNo", v)
|
|
|
|
|
|
}
|
|
|
|
|
|
body, _, err := ctx.Mes.Get("/api/internal/call/pool", q)
|
|
|
|
|
|
mesJSON(w, body, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callCreateHandler POST /api/call/create 发起叫料(按物料行拆 N 张单,占用池子可用量)
|
|
|
|
|
|
// 请求体:{stationNo, items:[{materialCode, qty, dock?}]};每行缺省接驳台用 yaml Station.Dock 兜底。
|
|
|
|
|
|
func callCreateHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
payload, err := io.ReadAll(r.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体读取失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var req map[string]any
|
|
|
|
|
|
if err := json.Unmarshal(payload, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-09-30 17:33:11 +08:00
|
|
|
|
dock := strings.TrimSpace(ctx.Config.Station.Dock)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
if items, ok := req["items"].([]any); ok {
|
|
|
|
|
|
for _, it := range items {
|
|
|
|
|
|
m, ok := it.(map[string]any)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if d, _ := m["dock"].(string); strings.TrimSpace(d) == "" {
|
|
|
|
|
|
if dock == "" {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "本工位接驳台未配置(配置文件 Station.Dock)且未选择目标接驳台,请联系运维")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
m["dock"] = dock // 兜底:目标接驳台取本工位配置
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-30 17:33:11 +08:00
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
if req["operator"] == nil || req["operator"] == "" {
|
|
|
|
|
|
if u := currentUsername(r); u != "" {
|
|
|
|
|
|
req["operator"] = u
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
body, _ := json.Marshal(req)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
resp, _, err := ctx.Mes.Post("/api/internal/call/create", json.RawMessage(body))
|
2026-09-10 16:59:15 +08:00
|
|
|
|
mesJSON(w, resp, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-19 07:50:21 +08:00
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// callListHandler GET /api/call/list 叫料单列表(本工位;status 可空=全部)
|
|
|
|
|
|
func callListHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
2026-09-19 07:50:21 +08:00
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
q := url.Values{}
|
2026-10-14 17:24:59 +08:00
|
|
|
|
for _, k := range []string{"stationNo", "status", "materialCode", "targetDock", "source", "requestNo"} {
|
2026-09-19 07:50:21 +08:00
|
|
|
|
if v := strings.TrimSpace(r.URL.Query().Get(k)); v != "" {
|
|
|
|
|
|
q.Set(k, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
body, _, err := ctx.Mes.Get("/api/internal/material-requests", q)
|
|
|
|
|
|
mesJSON(w, body, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// callSignHandler POST /api/call/sign 签收(仅已发货;签收=确认收到,系统不再对差异)
|
|
|
|
|
|
func callSignHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return callStateHandler(ctx, "/api/internal/call/sign", "签收")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callCancelHandler POST /api/call/cancel 取消(仅待备料,工位本人)
|
|
|
|
|
|
func callCancelHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return callStateHandler(ctx, "/api/internal/call/cancel", "取消")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callReturnHandler POST /api/call/return 申请退库(仅已发货/签收前;整单退,仓库确认收回后关单)
|
|
|
|
|
|
func callReturnHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return callStateHandler(ctx, "/api/internal/call/return", "退库申请")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// callStateHandler 叫料单状态动作通用代理:补 operator(当前登录人)后转发 MES 内部端点。
|
|
|
|
|
|
func callStateHandler(ctx *svc.ServiceContext, mesPath, action string) http.HandlerFunc {
|
2026-09-19 07:50:21 +08:00
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
payload, err := io.ReadAll(r.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体读取失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var req map[string]any
|
|
|
|
|
|
if err := json.Unmarshal(payload, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "请求体解析失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req["requestNo"] == nil || req["requestNo"] == "" {
|
2026-10-14 17:24:59 +08:00
|
|
|
|
fail(w, http.StatusBadRequest, "缺少叫料单号")
|
2026-09-19 07:50:21 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if req["operator"] == nil || req["operator"] == "" {
|
|
|
|
|
|
if u := currentUsername(r); u != "" {
|
|
|
|
|
|
req["operator"] = u
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
body, _ := json.Marshal(req)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
resp, _, err := ctx.Mes.Post(mesPath, json.RawMessage(body))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadGateway, action+"失败:MES 不可达")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-09-19 07:50:21 +08:00
|
|
|
|
mesJSON(w, resp, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-14 17:24:59 +08:00
|
|
|
|
|
2026-10-28 10:10:17 +08:00
|
|
|
|
// docksHandler GET /api/docks 接驳台列表(代理 MES /api/internal/docks;终端按工位过滤 左L/右R)
|
2026-10-14 17:24:59 +08:00
|
|
|
|
func docksHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
body, _, err := ctx.Mes.Get("/api/internal/docks", url.Values{})
|
|
|
|
|
|
mesJSON(w, body, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|