feat: 退料闭环定稿(损耗台账/备料清单/退料AGV自动派车/剩余量参考)+ 工位托盘守卫

- MES 新增损耗台账 loss_ledger,差异确认损耗接口 ConfirmReceiveLoss(按工单归集)
- 备料清单 preparation:MES 按 日计划×工单×工位×物料 实时计算,WMS 只读展示 PreparationList
- 退料闭环:N5 工位托盘守卫(未完成禁止退料)、退料 AGV 自动派车(JBT21 空位判定 + 托盘回收链路)、退料参考剩余量接口(应剩余=实备-已耗-已退-已损)
- 新增 AGV 车辆状态表 agv_vehicle_state 及查询
- 整合接驳台/叫料池/库存锁既有闭环,清理 sql 目录注释生成脚本与 catalog,更新文档
This commit is contained in:
SunYF
2026-11-04 19:06:03 +08:00
parent 42bcf395d2
commit 46fcb59079
123 changed files with 10666 additions and 3227 deletions
+2
View File
@@ -25,6 +25,8 @@ var pathPermMap = map[string]string{
"POST:/api/v1/daily-plans": "produce.workorder:dailyplan",
"POST:/api/v1/material-requests/generate": "produce.material:generate",
"POST:/api/v1/material-requests/auto-outbound": "produce.material:outbound",
// T2 收货差异确认损耗(2026-11-04):人工核实后落损耗台账,归数量不符处理权限(同链路操作者)
"POST:/api/v1/material-requests/confirm-loss": "produce.qty:handle",
// 数量不符处理(produce.qty 菜单下:补发/退库/调整关闭)
"POST:/api/v1/qty-reports/handle": "produce.qty:handle",
"POST:/api/v1/plc/send-process": "produce.plc:send",
@@ -1,6 +1,7 @@
package production
import (
"encoding/json"
"net/http"
"strconv"
@@ -113,6 +114,27 @@ func AgvModeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
}
// DispatchCallInternalHandler POST /api/internal/agv/dispatch-call body {requestNo, operator}
// 库管人工派车(2026-11-03 定论:废除备料完成自动派车)——WMS 叫料队列点「送货」→ 下发空车去程段。
func DispatchCallInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
RequestNo string `json:"requestNo"`
Operator string `json:"operator"`
}
_ = json.NewDecoder(r.Body).Decode(&req)
if req.RequestNo == "" {
httpx.Fail(w, 3309, "requestNo 必填")
return
}
if err := logic.New(svcCtx).DispatchCallAgv(r.Context(), req.RequestNo, req.Operator); err != nil {
httpx.Fail(w, 3309, err.Error())
return
}
httpx.Ok(w, map[string]any{"ok": true})
}
}
// ListAgvPointsHandler GET /agv/points?enabledOnly=1 列出 AGV 点位(启停用管理 / 派车下拉)。
func ListAgvPointsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -204,14 +226,15 @@ func DispatchAgvInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func AgvCallbackHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var body struct {
TaskCode string `json:"taskCode"`
Method string `json:"method"`
TaskCode string `json:"taskCode"`
Method string `json:"method"`
RobotCode string `json:"robotCode"` // 文档 2.2.1 必填 AGV 编号,用于绑定任务与载货台账
}
if err := httpx.ParseJSON(r, &body); err != nil {
httpx.BadRequest(w, "回调报文解析失败")
return
}
if err := logic.New(svcCtx).ApplyAgvCallback(r.Context(), body.TaskCode, body.Method); err != nil {
if err := logic.New(svcCtx).ApplyAgvCallback(r.Context(), body.TaskCode, body.Method, body.RobotCode); err != nil {
httpx.Fail(w, 3307, err.Error())
return
}
@@ -219,6 +242,46 @@ func AgvCallbackHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
}
// ListAgvVehicleStatesHandler GET /agv/vehicle-states —— AGV 载货台账列表(按车维护,防撞校验消费方)。
func ListAgvVehicleStatesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
list, err := logic.New(svcCtx).ListAgvVehicleStates(r.Context())
if err != nil {
httpx.Fail(w, 3311, err.Error())
return
}
httpx.Ok(w, map[string]any{"list": list})
}
}
// SetAgvVehicleStateHandler POST /agv/vehicle-states/set —— 人工维护载货状态(有托盘/空车),留痕操作人与时间。
func SetAgvVehicleStateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
AgvCode string `json:"agvCode"`
HasPallet bool `json:"hasPallet"`
Remark string `json:"remark"`
}
if err := httpx.ParseJSON(r, &req); err != nil || req.AgvCode == "" {
httpx.BadRequest(w, "缺少车辆编号(agvCode)")
return
}
operator := r.Header.Get("X-Username")
if operator == "" {
operator = "manual"
}
if err := logic.New(svcCtx).SetAgvVehicleStateManual(r.Context(), req.AgvCode, req.HasPallet, operator, req.Remark); err != nil {
httpx.Fail(w, 3312, err.Error())
return
}
state := "空车"
if req.HasPallet {
state = "有托盘"
}
httpx.OkMessage(w, "载货状态已更新为「"+state+"」", nil)
}
}
// atoiSafe 安全解析分页参数(非法/缺省回退 def)。
func atoiSafe(s string, def int) int {
if s == "" {
@@ -165,12 +165,15 @@ func RejectCallHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
// ConfirmReturnHandler POST /api/internal/call/return-confirm 仓库确认收回(退库闭环终点):
// RETURNING → RETURNED,池子已发额度回补。
// RETURNING → RETURNED,按实收量回补池子已发额度;实收<应退的差异落「待核」,不自动进损耗台账(R18 2026-11-03)。
func ConfirmReturnHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
RequestNo string `json:"requestNo"`
Operator string `json:"operator"`
RequestNo string `json:"requestNo"`
Operator string `json:"operator"`
ReceiveQty float64 `json:"receiveQty"` // 库管实收量(0=整单全收)
ReceiveDiff float64 `json:"receiveDiff"` // 收货差异=应退-实收
ReceiveNote string `json:"receiveNote"` // 差异备注
}
if err := httpx.ParseJSON(r, &req); err != nil || req.RequestNo == "" {
httpx.BadRequest(w, "缺少叫料单号")
@@ -180,7 +183,7 @@ func ConfirmReturnHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
if op == "" {
op = "warehouse"
}
if err := logic.New(svcCtx).ConfirmReturn(r.Context(), req.RequestNo, op); err != nil {
if err := logic.New(svcCtx).ConfirmReturn(r.Context(), req.RequestNo, op, req.ReceiveQty, req.ReceiveDiff, req.ReceiveNote); err != nil {
httpx.Fail(w, 3208, err.Error())
return
}
@@ -0,0 +1,33 @@
package production
import (
"encoding/json"
"net/http"
"strings"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// ConfirmReceiveLossHandler POST /api/v1/material-requests/confirm-loss
// T2(2026-11-04 拍板):收货差异「确认损耗」——人工点击才写损耗台账并挂工单,不自动转。
// body {"requestNo":"...", "reason":"..."};权限码 produce.qty:handle(数量不符处理同链路)。
func ConfirmReceiveLossHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
RequestNo string `json:"requestNo"`
Reason string `json:"reason"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httpx.Fail(w, 3406, "参数错误: "+err.Error())
return
}
op := strings.TrimSpace(operator(r, ""))
if err := logic.New(svcCtx).ConfirmReceiveLoss(r.Context(), req.RequestNo, strings.TrimSpace(req.Reason), op); err != nil {
httpx.Fail(w, 3407, err.Error())
return
}
httpx.Ok(w, map[string]any{"requestNo": req.RequestNo, "lossQty": "written", "status": "PENDING"})
}
}
@@ -0,0 +1,42 @@
package production
import (
"net/http"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// ---------- P1 备料清单(2026-11-04 用户拍板) ----------
// PreparationListInternalHandler GET /api/internal/preparation-list
// 备料清单:今天及以后 × 未完成/已启用/执行中 日排产 → 日期×工单×工位×物料×数量。
// 只读实时算(工艺改动后刷新即联动),不触发出库/领料,不管库存。供 WMS 备料清单页调用。
func PreparationListInternalHandler(serverCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
list, err := logic.New(serverCtx).ComputePreparationList(r.Context())
if err != nil {
httpx.Fail(w, 3404, err.Error())
return
}
httpx.Ok(w, list)
}
}
// ---------- E4 托盘余料明细(2026-11-04 拍板) ----------
// PalletResidualInternalHandler GET /api/internal/pallet/residual?dock=XXX
// 指定接驳台托盘余料明细:最近一次 AGV 送达该接驳台的运输段 materials_json(只读)。
// 供 WMS 托盘工作台「代发起退料」弹窗自动带出余料,库管核对后提交。
func PalletResidualInternalHandler(serverCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
dock := r.URL.Query().Get("dock")
rows, err := logic.New(serverCtx).PalletResidual(r.Context(), dock)
if err != nil {
httpx.Fail(w, 3405, err.Error())
return
}
httpx.Ok(w, rows)
}
}
@@ -267,6 +267,8 @@ func InProcessInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
// OnlineWorkpieceInternalHandler 工件进线建档(工位终端上线面板提交)。
// POST OnlineReq{sn,orderNo,workOrderId,processSeq};operator 从 query 兜底(内部接口无 JWT)。
// E2(2026-11-03 拍板):建档标记位置+工单——工位号优先取工位登录态注入的 X-Station-No
// (终端链路 body 不带 stationNo,历史落 0 导致建档工位缺失、预生成 SN 被首道工位校验拒绝)。
func OnlineWorkpieceInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.OnlineReq
@@ -274,6 +276,7 @@ func OnlineWorkpieceInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
httpx.BadRequest(w, "请求体解析失败")
return
}
req.StationNo = StationNo(r, req.StationNo)
LogImpersonation(svcCtx, r, r.URL.Query().Get("operator"), "/api/internal/workpiece/online")
if msg, mm, err := logic.New(svcCtx).OnlineWorkpiece(r.Context(), req, StationOp(r, r.URL.Query().Get("operator"))); err != nil {
httpx.Fail(w, 2007, err.Error())
@@ -0,0 +1,104 @@
package production
import (
"net/http"
"strconv"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// ============ 退料 AGV / 损耗 / 余量参考值 internal 接口(2026-11-04 口径定稿) ============
// Jbt21FreeInternalHandler GET /api/internal/agv/jbt21-free
// 库房接驳台 JBT21 空位查询(WMS 托盘工作台按钮禁用判定 / 自动派车循环复用)。
// 口径:JBT21 无空位不派车——传感器有托盘或有在途任务即视为占用。
func Jbt21FreeInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
free, reason := logic.New(svcCtx).Jbt21Free(r.Context())
httpx.Ok(w, map[string]any{"free": free, "reason": reason})
}
}
// CancelAgvByDockInternalHandler POST /api/internal/agv/cancel-by-dock {dockCode, reason}
// 按接驳台取消未终态 PALLET 回收任务(退料单取消联动:作废未完成 AGV 任务,留痕)。
// 已装车运输中(TRANSPORT 段执行中)的段不在此列——按 D2 口径由调用方先判定可取消窗口。
func CancelAgvByDockInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
DockCode string `json:"dockCode"`
Reason string `json:"reason"`
}
if err := httpx.ParseJSON(r, &req); err != nil || req.DockCode == "" {
httpx.BadRequest(w, "dockCode 必填")
return
}
n, inTransit, err := logic.New(svcCtx).CancelPalletTasksByDock(r.Context(), req.DockCode, req.Reason)
if err != nil {
httpx.Fail(w, 3306, err.Error())
return
}
httpx.Ok(w, map[string]any{"cancelled": n, "hasInTransit": inTransit})
}
}
// LossConfirmInternalHandler POST /api/internal/loss/confirm
// WMS 确认收货时实收<应退就地填损耗原因 → 落 MES loss_ledgers(损耗确认移 WMS,MES 只读展示)。
func LossConfirmInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
ReturnNo string `json:"returnNo"`
OrderNo string `json:"orderNo"`
StationNo int `json:"stationNo"`
MaterialCode string `json:"materialCode"`
MaterialName string `json:"materialName"`
TheoryQty float64 `json:"theoryQty"`
ActualQty float64 `json:"actualQty"`
LossQty float64 `json:"lossQty"`
Reason string `json:"reason"`
Operator string `json:"operator"`
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
op := r.Header.Get("X-Username")
if op == "" {
op = req.Operator
}
if err := logic.New(svcCtx).ConfirmLossFromWMS(r.Context(), req.ReturnNo, req.OrderNo, req.StationNo,
req.MaterialCode, req.MaterialName, req.TheoryQty, req.ActualQty, req.LossQty, req.Reason, op); err != nil {
httpx.Fail(w, 3408, err.Error())
return
}
httpx.OkMessage(w, "损耗已落账", nil)
}
}
// ReturnReferenceHandler GET /api/v1/return-reference?stationNo= (JWT,MES 前端转产向导/下班退料展示)
func ReturnReferenceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo, _ := strconv.Atoi(r.URL.Query().Get("stationNo"))
rows, err := logic.New(svcCtx).ReturnReference(r.Context(), stationNo)
if err != nil {
httpx.Fail(w, 3409, err.Error())
return
}
httpx.Ok(w, map[string]any{"rows": rows})
}
}
// ReturnReferenceInternalHandler GET /api/internal/return-reference?stationNo=
// 余量参考值(应剩余量−已退余量−已损耗):转产向导/下班退料/巡检退料/WMS 代退料共用数据源。
func ReturnReferenceInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo, _ := strconv.Atoi(r.URL.Query().Get("stationNo"))
rows, err := logic.New(svcCtx).ReturnReference(r.Context(), stationNo)
if err != nil {
httpx.Fail(w, 3409, err.Error())
return
}
httpx.Ok(w, map[string]any{"rows": rows})
}
}
@@ -2,6 +2,7 @@ package production
import (
"encoding/json"
"errors"
"net/http"
"strconv"
@@ -150,6 +151,11 @@ func OffDutyInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
op = r.Header.Get("X-Username")
}
if err := logic.New(svcCtx).OffDuty(r.Context(), req.StationNo, op, req.HasLeftover, req.Items); err != nil {
// 拍板2(2026-11-04):PLC 离线需人工强行确认 → 业务码 2409,前端弹风险提示二次确认
if errors.Is(err, logic.ErrReturnForceConfirm) {
httpx.Fail(w, 2409, err.Error())
return
}
httpx.Fail(w, 500, err.Error())
return
}
@@ -0,0 +1,105 @@
package production
import (
"net/http"
"strconv"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// ============ 工位托盘位传感器 / 退料前置守卫(N5 2026-11-04 拍板) ============
//
// N5 口径:工位传感器表示无托盘(工件已完工离开工位)才允许退料;有托盘(工件在工位、
// 未完工、消耗未定)时所有退料入口一律拒绝,提示「工件未完工,不允许退料」。
// 本文件三个内部端点:读传感器 / mock 联调置位 / WMS 代退料前置校验。
// StationPalletInternalHandler GET /api/internal/station/pallet?stationNo=
// 读工位托盘位传感器(G{stationNo} 位)。hasPallet=true = 工件在工位未完工。
func StationPalletInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo, _ := strconv.Atoi(r.URL.Query().Get("stationNo"))
if stationNo <= 0 {
httpx.BadRequest(w, "缺少 stationNo")
return
}
if svcCtx.PLC == nil || svcCtx.PLC.Get() == nil {
httpx.Fail(w, 3202, "PLC 未接入")
return
}
has, err := svcCtx.PLC.QueryStationPallet(r.Context(), stationNo)
if err != nil {
httpx.Fail(w, 3202, err.Error())
return
}
httpx.Ok(w, map[string]any{"stationNo": stationNo, "hasPallet": has})
}
}
// SetStationPalletInternalHandler POST /api/internal/station/pallet/set {stationNo, hasPallet}
// mock 联调用:手动置位某工位托盘位是否有件(仅 mock 模式生效,真实 PLC 忽略)。
func SetStationPalletInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
StationNo int `json:"stationNo"`
HasPallet bool `json:"hasPallet"`
}
if err := httpx.ParseJSON(r, &req); err != nil || req.StationNo <= 0 {
httpx.BadRequest(w, "请求体缺失或 stationNo 非法")
return
}
svcCtx.PLC.SetMockStationPallet(req.StationNo, req.HasPallet)
if !svcCtx.PLC.IsMock() {
httpx.OkMessage(w, "真实 PLC 模式忽略手动置位", nil)
return
}
httpx.Ok(w, nil)
}
}
// StationReturnGuardInternalHandler GET /api/internal/station/return-guard?stationNo=
// WMS 代退料前置校验(N5 + 拍板2):三态——
// allowed=true 正常放行(传感器无托盘);
// allowed=false, needForce=true PLC 离线:前端弹风险提示,人工确认后带 forceConfirm 重提;
// allowed=false, needForce=false 硬拒(传感器有托盘/工位号非法),reason 为拒绝文案。
func StationReturnGuardInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo, _ := strconv.Atoi(r.URL.Query().Get("stationNo"))
if stationNo <= 0 {
httpx.BadRequest(w, "缺少 stationNo")
return
}
needForce, err := logic.New(svcCtx).CheckReturnStationClear(r.Context(), stationNo)
if err != nil {
httpx.Ok(w, map[string]any{"stationNo": stationNo, "allowed": false, "needForce": false, "reason": err.Error()})
return
}
if needForce {
httpx.Ok(w, map[string]any{"stationNo": stationNo, "allowed": false, "needForce": true, "reason": logic.ErrReturnForceConfirm.Error()})
return
}
httpx.Ok(w, map[string]any{"stationNo": stationNo, "allowed": true, "needForce": false, "reason": ""})
}
}
// PlcMockOfflineInternalHandler POST /api/internal/plc/mock-offline {offline}
// mock 联调用:模拟 PLC 离线(工位托盘位读失败),验证拍板2「PLC 离线提示+人工强行确认」链路。
// 仅 mock 模式生效;真实 PLC 模式忽略(读真实传感器,离线与否由现场决定)。
func PlcMockOfflineInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Offline bool `json:"offline"`
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
svcCtx.PLC.SetMockOffline(req.Offline)
if !svcCtx.PLC.IsMock() {
httpx.OkMessage(w, "真实 PLC 模式忽略离线模拟", nil)
return
}
httpx.Ok(w, map[string]any{"offline": req.Offline})
}
}
@@ -2,6 +2,7 @@ package production
import (
"encoding/json"
"errors"
"net/http"
"strconv"
@@ -51,6 +52,11 @@ func CreateTransferReturnsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
op = "system"
}
if err := logic.New(svcCtx).CreateTransferReturns(r.Context(), req.StationNo, op, req.Items); err != nil {
// 拍板2(2026-11-04):PLC 离线需人工强行确认 → 业务码 2409,前端弹风险提示二次确认
if errors.Is(err, logic.ErrReturnForceConfirm) {
httpx.Fail(w, 2409, err.Error())
return
}
httpx.Fail(w, 500, err.Error())
return
}
@@ -86,6 +86,31 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/station/return-material", Handler: internal(production.StationSessionAuth(serverCtx)(StationReturnMaterialInternalHandler(serverCtx))),
})
// 退料单列表/取消/余量参考值(2026-11-04 口径定稿:工位可取消自己 PENDING 退料单;参考值=应剩−已退−损耗)
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/station/return-list", Handler: internal(production.StationSessionAuth(serverCtx)(StationReturnListInternalHandler(serverCtx))),
})
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/station/return-cancel", Handler: internal(production.StationSessionAuth(serverCtx)(StationReturnCancelInternalHandler(serverCtx))),
})
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/station/return-reference", Handler: internal(production.StationSessionAuth(serverCtx)(StationReturnReferenceInternalHandler(serverCtx))),
})
// JBT21 空位查询 / 按接驳台取消 AGV 任务(WMS 退料单取消联动、托盘工作台按钮判定)
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/agv/jbt21-free", Handler: internal(production.Jbt21FreeInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/agv/cancel-by-dock", Handler: internal(production.CancelAgvByDockInternalHandler(serverCtx)),
})
// WMS 收货确认损耗落账(2026-11-04 F:损耗确认移 WMS,MES 只读展示)
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/loss/confirm", Handler: internal(production.LossConfirmInternalHandler(serverCtx)),
})
// 余量参考值(WMS 代退料拉取:应剩余量−已退余量−已损耗)
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/return-reference", Handler: internal(production.ReturnReferenceInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/station/inspection", Handler: internal(production.StationSessionAuth(serverCtx)(StationInspectionInternalHandler(serverCtx))),
})
@@ -130,6 +155,10 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/agv/pallet-dispatch", Handler: internal(production.PalletDispatchInternalHandler(serverCtx)),
})
// 库管人工派车(2026-11-03 定论:废除备料完成自动派车,WMS 点「送货」才发车)
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/agv/dispatch-call", Handler: internal(production.DispatchCallInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/dock/list", Handler: internal(production.DockListInternalHandler(serverCtx)),
})
@@ -164,10 +193,33 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/dock/pallet/set", Handler: internal(production.SetDockPalletInternalHandler(serverCtx)),
})
// 工位托盘位传感器读取 / mock 置位 / 退料前置守卫(N5 2026-11-04 拍板:工位传感器无托盘才允许退料;
// WMS 代退料经 return-guard 前置校验,有托盘拒绝并提示「工件未完工,不允许退料」)
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/station/pallet", Handler: internal(production.StationPalletInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/station/pallet/set", Handler: internal(production.SetStationPalletInternalHandler(serverCtx)),
})
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/station/return-guard", Handler: internal(production.StationReturnGuardInternalHandler(serverCtx)),
})
// mock 联调:模拟 PLC 离线(拍板2 2026-11-04:离线时退料拦截降级为提示+人工强行确认)
server.AddRoute(rest.Route{
Method: http.MethodPost, Path: "/api/internal/plc/mock-offline", Handler: internal(production.PlcMockOfflineInternalHandler(serverCtx)),
})
// 排产物料需求量:WMS「缺料补货」视图实时拉取(?days=N 覆盖窗口,默认5天)
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/material-demand", Handler: internal(production.MaterialDemandInternalHandler(serverCtx)),
})
// P1 备料清单(2026-11-04 用户拍板):今天及以后×工单×工位×物料×数量,只读实时算,供 WMS 备料清单页
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/preparation-list", Handler: internal(production.PreparationListInternalHandler(serverCtx)),
})
// E4 托盘余料明细(2026-11-04 拍板):最近送达该接驳台的 AGV 运输段 materials_json,供 WMS 代退料弹窗带出
server.AddRoute(rest.Route{
Method: http.MethodGet, Path: "/api/internal/pallet/residual", Handler: internal(production.PalletResidualInternalHandler(serverCtx)),
})
// ---------- 产线点位(供工位终端调用,X-API-TOKEN 保护)----------
server.AddRoute(rest.Route{
@@ -268,6 +320,8 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
// 转产按工单精确退料(TransferReturn 向导):列出故障工位涉及工单+理论应剩 / 按工单生成退库单
{Method: http.MethodGet, Path: "/transfer/orders", Handler: production.ListTransferOrdersHandler(serverCtx)},
{Method: http.MethodPost, Path: "/transfer/returns", Handler: production.CreateTransferReturnsHandler(serverCtx)},
// 余量参考值(2026-11-04 定稿:剩余余量=应剩余量−已退余量−已损耗;转产向导/前端展示用)
{Method: http.MethodGet, Path: "/return-reference", Handler: production.ReturnReferenceHandler(serverCtx)},
// AGV 三段链确认(到位待令)与自由调度(2026-11-03 三层安全机制)
{Method: http.MethodPost, Path: "/agv/confirm", Handler: production.ConfirmAgvHandler(serverCtx)},
{Method: http.MethodPost, Path: "/agv/free-dispatch", Handler: production.FreeDispatchHandler(serverCtx)},
@@ -295,6 +349,8 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
// 叫料单列表(B5:MES 后台只读视图;发起/签收/退库/取消在工位终端,完成/驳回/收回在仓库端)
{Method: http.MethodGet, Path: "/material-requests", Handler: production.ListCallsHandler(serverCtx)},
// T2(2026-11-04 拍板):收货差异「确认损耗」——人工点击才写损耗台账并挂工单,不自动转
{Method: http.MethodPost, Path: "/material-requests/confirm-loss", Handler: production.ConfirmReceiveLossHandler(serverCtx)},
// 物料档案(代理 WMS /api/internal/materials,WMS 是物料主数据源;WMS 不可达返回空列表)
{Method: http.MethodGet, Path: "/materials", Handler: production.ListMaterialsHandler(serverCtx)},
// 工单单台用料(物料清单已删除:工单详情页只读汇总 = 工单工艺组合×工艺物料清单)
@@ -352,6 +408,10 @@ func RegisterProductionRoutes(server *rest.Server, serverCtx *svc.ServiceContext
{Method: http.MethodPut, Path: "/agv/points/:id", Handler: production.UpdateAgvPointHandler(serverCtx)},
// 车辆状态只读展示(海康 3.1.7,2026-11-03 拍板:纯只读,不干预调度)
{Method: http.MethodGet, Path: "/agv/vehicles", Handler: production.ListAgvVehiclesHandler(serverCtx)},
// AGV 载货状态台账(2026-11-04 防撞校验补做):取货/卸货确认自动维护 + 人工维护留痕;
// 派车前防撞硬拒消费本台账(AGV载货=true 且目标接驳台有托盘 → 拒派)
{Method: http.MethodGet, Path: "/agv/vehicle-states", Handler: production.ListAgvVehicleStatesHandler(serverCtx)},
{Method: http.MethodPost, Path: "/agv/vehicle-states/set", Handler: production.SetAgvVehicleStateHandler(serverCtx)},
{Method: http.MethodPost, Path: "/torque/report", Handler: production.TorqueReportHandler(serverCtx)},
{Method: http.MethodGet, Path: "/torque/records", Handler: production.TorqueRecordsHandler(serverCtx)},
+83 -12
View File
@@ -2,9 +2,11 @@ package handler
import (
"encoding/base64"
"errors"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@@ -63,17 +65,20 @@ func StationQtyReportInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFun
func StationReturnMaterialInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
OrderNo string `json:"orderNo"`
StationNo int `json:"stationNo"`
Sn string `json:"sn"`
MaterialCode string `json:"materialCode"`
MaterialName string `json:"materialName"`
Spec string `json:"spec"`
Qty int `json:"qty"`
Unit string `json:"unit"`
ReturnDock string `json:"returnDock"`
Reason string `json:"reason"`
Operator string `json:"operator"`
OrderNo string `json:"orderNo"`
StationNo int `json:"stationNo"`
Sn string `json:"sn"`
MaterialCode string `json:"materialCode"`
MaterialName string `json:"materialName"`
Spec string `json:"spec"`
Qty int `json:"qty"`
Unit string `json:"unit"`
ReturnDock string `json:"returnDock"`
Reason string `json:"reason"`
LossReason string `json:"lossReason"` // 2026-11-04 拍板:工位退料损耗原因(巡检退料一律必填,前端校验)
TransportMode string `json:"transportMode"` // 2026-11-04 拍板:运输方式 agv/manual(必填;agv=传感器检测到托盘且库房有空位自动派车)
ForceConfirm bool `json:"forceConfirm"` // 拍板2 2026-11-04:PLC 离线时前端弹风险提示后人工强行确认
Operator string `json:"operator"`
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
@@ -81,7 +86,12 @@ func StationReturnMaterialInternalHandler(svcCtx *svc.ServiceContext) http.Handl
}
production.LogImpersonation(svcCtx, r, req.Operator, "/api/internal/station/return-material")
if err := logic.New(svcCtx).StationReturnMaterial(r.Context(), req.OrderNo, req.StationNo, req.Sn,
req.MaterialCode, req.MaterialName, req.Spec, req.Reason, production.StationOp(r, req.Operator), req.Unit, req.Qty, req.ReturnDock); err != nil {
req.MaterialCode, req.MaterialName, req.Spec, req.Reason, production.StationOp(r, req.Operator), req.Unit, req.Qty, req.ReturnDock, req.LossReason, req.TransportMode, req.ForceConfirm); err != nil {
// 拍板2(2026-11-04):PLC 离线需人工强行确认 → 业务码 2409,前端弹风险提示二次确认
if errors.Is(err, logic.ErrReturnForceConfirm) {
httpx.Fail(w, 2409, err.Error())
return
}
httpx.Fail(w, 2403, err.Error())
return
}
@@ -89,6 +99,67 @@ func StationReturnMaterialInternalHandler(svcCtx *svc.ServiceContext) http.Handl
}
}
// StationReturnListInternalHandler GET /api/internal/station/return-list?stationNo=
// 工位终端「我的退料单」列表(PENDING 可取消 / 全部可查),WMS 数据经 MES 转发。
func StationReturnListInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo, _ := strconv.Atoi(r.URL.Query().Get("stationNo"))
if stationNo <= 0 {
httpx.Fail(w, 2407, "stationNo 必填")
return
}
list, err := logic.New(svcCtx).ListStationReturnOrders(r.Context(), stationNo)
if err != nil {
httpx.Fail(w, 2407, err.Error())
return
}
httpx.Ok(w, map[string]any{"list": list})
}
}
// StationReturnCancelInternalHandler POST /api/internal/station/return-cancel {returnNo, reason}
// 工位取消自己发起的 PENDING 退料单(库房也可取消,见 WMS 退库单页取消入口)。
// 已收货(RECEIVED)不可取消;取消后作废未完成 AGV 任务、不产生库存变动。
func StationReturnCancelInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
StationNo int `json:"stationNo"`
ReturnNo string `json:"returnNo"`
Reason string `json:"reason"`
Operator string `json:"operator"`
}
if err := httpx.ParseJSON(r, &req); err != nil || req.ReturnNo == "" {
httpx.BadRequest(w, "returnNo 必填")
return
}
production.LogImpersonation(svcCtx, r, req.Operator, "/api/internal/station/return-cancel")
if err := logic.New(svcCtx).CancelStationReturn(r.Context(), req.ReturnNo, req.StationNo,
production.StationOp(r, req.Operator), req.Reason); err != nil {
httpx.Fail(w, 2408, err.Error())
return
}
httpx.OkMessage(w, "退料单已取消", nil)
}
}
// StationReturnReferenceInternalHandler GET /api/internal/station/return-reference?stationNo=
// 工位终端余量参考值(剩余余量=应剩余量−已退余量−已损耗;仅展示不阻断提交)。
func StationReturnReferenceInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stationNo, _ := strconv.Atoi(r.URL.Query().Get("stationNo"))
if stationNo <= 0 {
httpx.Fail(w, 2409, "stationNo 必填")
return
}
rows, err := logic.New(svcCtx).ReturnReference(r.Context(), stationNo)
if err != nil {
httpx.Fail(w, 2409, err.Error())
return
}
httpx.Ok(w, map[string]any{"rows": rows})
}
}
// StationInspectionInternalHandler POST /api/internal/station/inspection
// 工位终端提交巡检记录(签到/点检/过程巡检/完工确认/异常),复用与 PAD 巡检终端相同的落库逻辑。
// 工位终端无按钮级权限概念,仅按 category 合法性校验(CreateInspection 内部已做)。