2026-08-28 15:06:01 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_workstation/internal/svc"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// processDoneHandler POST /api/report/process-done
|
2026-08-31 08:06:55 +08:00
|
|
|
|
// {sn, processCode, stationNo, steps:[{stepId,name,value,text}]}
|
|
|
|
|
|
// 流程:先冲刷一轮积压 → 实时报 MES /api/internal/station/report;
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// MES 不可达则写入 report_queue(kind=process_done) 并返回 data.queued=true(离线缓存稍后自动重传)。
|
|
|
|
|
|
func processDoneHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
Sn string `json:"sn"`
|
2026-08-31 08:06:55 +08:00
|
|
|
|
OrderNo string `json:"orderNo"`
|
|
|
|
|
|
ProcessCode int `json:"processCode"`
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
|
Steps []struct {
|
|
|
|
|
|
StepId int `json:"stepId"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Value float64 `json:"value"`
|
|
|
|
|
|
Text string `json:"text"`
|
|
|
|
|
|
} `json:"steps"`
|
|
|
|
|
|
Operator string `json:"operator"`
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if req.Sn == "" || req.ProcessCode <= 0 || req.StationNo <= 0 {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "sn/processCode/stationNo 必填")
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 先尽力冲刷历史积压,失败不影响本次上报
|
|
|
|
|
|
ctx.Syncer.RunOnce(r.Context())
|
|
|
|
|
|
|
|
|
|
|
|
body := map[string]any{
|
|
|
|
|
|
"sn": req.Sn,
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"orderNo": req.OrderNo,
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"processCode": req.ProcessCode,
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"stationNo": req.StationNo,
|
|
|
|
|
|
"steps": req.Steps,
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"operator": req.Operator,
|
|
|
|
|
|
}
|
|
|
|
|
|
payloadBytes, _ := json.Marshal(body)
|
|
|
|
|
|
|
|
|
|
|
|
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "process_done",
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"工序完成上报 sn="+req.Sn+" stationNo="+itoa(req.StationNo)+" processCode="+itoa(req.ProcessCode))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if _, _, err := ctx.Mes.Post("/api/internal/station/report", json.RawMessage(payloadBytes)); err != nil {
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if qerr := ctx.Store.InsertQueueItem("process_done", string(payloadBytes)); qerr != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
logx.Errorf("工序完成上报失败已离线排队: %v", err)
|
|
|
|
|
|
ok(w, map[string]any{"queued": true})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ok(w, map[string]any{"queued": false})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// tempStoreHandler POST /api/report/temp-store
|
2026-08-31 08:06:55 +08:00
|
|
|
|
// {sn, orderNo, stationNo, processCode, operator} → 调 MES /api/internal/station/checkin(半成品入库)。
|
|
|
|
|
|
// 不可达时以同样 payload 写入 report_queue(kind=temp_store)。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func tempStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req struct {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
Sn string `json:"sn"`
|
|
|
|
|
|
OrderNo string `json:"orderNo"`
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
|
ProcessCode int `json:"processCode"`
|
|
|
|
|
|
Operator string `json:"operator"`
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
if err := parseJSON(r, &req); err != nil {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if req.Sn == "" {
|
|
|
|
|
|
fail(w, http.StatusBadRequest, "缺少 sn")
|
2026-08-28 15:06:01 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 先尽力冲刷历史积压,失败不影响本次上报
|
|
|
|
|
|
ctx.Syncer.RunOnce(r.Context())
|
|
|
|
|
|
|
|
|
|
|
|
body := map[string]any{
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"sn": req.Sn,
|
|
|
|
|
|
"orderNo": req.OrderNo,
|
|
|
|
|
|
"stationNo": req.StationNo,
|
|
|
|
|
|
"processCode": req.ProcessCode,
|
|
|
|
|
|
"operator": req.Operator,
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
payloadBytes, _ := json.Marshal(body)
|
|
|
|
|
|
|
|
|
|
|
|
ctx.Store.AddEventLog(req.OrderNo, req.Operator, "temp_store",
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"暂存/退回库房 sn="+req.Sn+" stationNo="+itoa(req.StationNo))
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
if _, _, err := ctx.Mes.Post("/api/internal/station/checkin", json.RawMessage(payloadBytes)); err != nil {
|
|
|
|
|
|
if qerr := ctx.Store.InsertQueueItem("temp_store", string(payloadBytes)); qerr != nil {
|
|
|
|
|
|
fail(w, http.StatusInternalServerError, "上报失败且离线队列写入失败:"+qerr.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
logx.Errorf("暂存退库上报失败已离线排队: %v", err)
|
|
|
|
|
|
ok(w, map[string]any{"queued": true})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ok(w, map[string]any{"queued": false})
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|