Files
bj_power/bj_power_mes/internal/handler/station_ops.go
T
SunYF 46fcb59079 feat: 退料闭环定稿(损耗台账/备料清单/退料AGV自动派车/剩余量参考)+ 工位托盘守卫
- MES 新增损耗台账 loss_ledger,差异确认损耗接口 ConfirmReceiveLoss(按工单归集)
- 备料清单 preparation:MES 按 日计划×工单×工位×物料 实时计算,WMS 只读展示 PreparationList
- 退料闭环:N5 工位托盘守卫(未完成禁止退料)、退料 AGV 自动派车(JBT21 空位判定 + 托盘回收链路)、退料参考剩余量接口(应剩余=实备-已耗-已退-已损)
- 新增 AGV 车辆状态表 agv_vehicle_state 及查询
- 整合接驳台/叫料池/库存锁既有闭环,清理 sql 目录注释生成脚本与 catalog,更新文档
2026-11-04 19:06:03 +08:00

272 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"encoding/base64"
"errors"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/handler/production"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
)
// ============ 工位终端 → MES(X-API-TOKEN 保护内部接口) ============
// StationEmergencyInternalHandler POST /api/internal/station/emergency
// 工位终端应急呼叫:落预警中心消息(type=emergency_call),pad 顶部轮询提示。
func StationEmergencyInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
StationNo int `json:"stationNo"`
OrderNo string `json:"orderNo"`
Remark string `json:"remark"`
Operator string `json:"operator"`
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
production.LogImpersonation(svcCtx, r, req.Operator, "/api/internal/station/emergency")
if err := logic.New(svcCtx).EmergencyCall(r.Context(), req.StationNo, req.OrderNo, req.Remark, production.StationOp(r, req.Operator)); err != nil {
httpx.Fail(w, 2401, err.Error())
return
}
httpx.OkMessage(w, "应急呼叫已发送", nil)
}
}
// StationQtyReportInternalHandler POST /api/internal/station/qty-report
// 工位数量不符上报:落 material_qty_report + 预警中心消息(type=qty_diff)。
func StationQtyReportInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.QtyReportReq
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
production.LogImpersonation(svcCtx, r, req.Operator, "/api/internal/station/qty-report")
req.Operator = production.StationOp(r, req.Operator)
if err := logic.New(svcCtx).CreateQtyReport(r.Context(), req); err != nil {
httpx.Fail(w, 2402, err.Error())
return
}
httpx.OkMessage(w, "数量不符已上报", nil)
}
}
// StationReturnMaterialInternalHandler POST /api/internal/station/return-material
// 工位退料回库房:落事件日志 + 调 WMS 预建待确认退库单(WMS 确认收货后库存加回)。
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"`
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, "请求体解析失败")
return
}
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, 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
}
httpx.OkMessage(w, "退库单已提交", nil)
}
}
// 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 内部已做)。
func StationInspectionInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.InspectionReq
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
production.LogImpersonation(svcCtx, r, req.Operator, "/api/internal/station/inspection")
req.Operator = production.StationOp(r, req.Operator)
msg, err := logic.New(svcCtx).CreateInspection(r.Context(), req, req.Operator)
if err != nil {
httpx.Fail(w, 2301, err.Error())
return
}
httpx.OkMessage(w, msg, nil)
}
}
// StationInspectionUploadInternalHandler POST /api/internal/station/inspection-upload
// 工位终端巡检拍照上传(手机设备拍照)。前端把图片读成 base64 传 {filename, data},
// 服务端解码落盘到 Upload.Dir/日期目录,返回系统文件名(落库到 inspection_record.photo)。
func StationInspectionUploadInternalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Filename string `json:"filename"`
Data string `json:"data"` // base64(可带 data:image/xxx;base64, 前缀)
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
if req.Data == "" {
httpx.BadRequest(w, "图片数据为空")
return
}
raw := req.Data
if i := strings.Index(raw, ","); i >= 0 && strings.Contains(raw[:i], "base64") {
raw = raw[i+1:]
}
b, err := base64.StdEncoding.DecodeString(raw)
if err != nil {
httpx.Fail(w, 2306, "图片解码失败:"+err.Error())
return
}
sub := time.Now().Format("2006-01-02")
dir := filepath.Join(svcCtx.Config.Upload.Dir, sub)
if err := os.MkdirAll(dir, 0o755); err != nil {
httpx.Fail(w, 2306, "目录创建失败:"+err.Error())
return
}
name := req.Filename
if name == "" || strings.ContainsAny(name, "/\\") {
name = "insp_" + time.Now().Format("150405") + ".png"
}
// 文件名加日期前缀防重复
saveName := time.Now().Format("20060102") + "_" + filepath.Base(name)
full := filepath.Join(dir, saveName)
if err := os.WriteFile(full, b, 0o644); err != nil {
httpx.Fail(w, 2306, "照片保存失败:"+err.Error())
return
}
httpx.Ok(w, map[string]any{"filename": sub + "/" + saveName, "url": "/api/v1/files/" + sub + "/" + saveName})
}
}
// ============ MES 前端(JWT 保护) ============
// EmergencyCallHandler POST /api/v1/alert/emergency(MES 前端也可发起应急呼叫)
func EmergencyCallHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
StationNo int `json:"stationNo"`
OrderNo string `json:"orderNo"`
Remark string `json:"remark"`
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
if err := logic.New(svcCtx).EmergencyCall(r.Context(), req.StationNo, req.OrderNo, req.Remark, operator(r, "")); err != nil {
httpx.Fail(w, 2401, err.Error())
return
}
httpx.OkMessage(w, "应急呼叫已发送", nil)
}
}
// GenerateInterProcessInspectionHandler POST /api/v1/inspections/generate-inter-process
// 一键生成《工序间检验记录》:汇总该工单 PROCESS 类巡检记录(按步骤维度)。
func GenerateInterProcessInspectionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
OrderNo string `json:"orderNo"`
}
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
data, err := logic.New(svcCtx).GenerateInterProcessInspection(r.Context(), req.OrderNo)
if err != nil {
httpx.Fail(w, 2405, err.Error())
return
}
httpx.Ok(w, data)
}
}