102 lines
4.0 KiB
Go
102 lines
4.0 KiB
Go
package handler
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
|
||
"bj_power_workstation/internal/svc"
|
||
)
|
||
|
||
// syncFlushHandler POST /api/sync/flush 手动立即执行一轮同步,返回本次成功条数。
|
||
func syncFlushHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
count := ctx.Syncer.RunOnce(r.Context())
|
||
ok(w, map[string]any{
|
||
"count": count,
|
||
"message": "本轮同步完成",
|
||
})
|
||
}
|
||
}
|
||
|
||
// syncStatsHandler GET /api/sync/stats 返回「当前登录人」尚未成功上报的本地数据(失败列表),
|
||
// 供前端在「同一人重新登录」时仅展示失败信息(不自动打开页面)。他人数据不被返回、不可见。
|
||
// queueDead=死信(MES 业务拒绝、待人工重传处理),与 queue(待自动重传)分开回报。
|
||
func syncStatsHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
if ss, err := ctx.Store.GetStationSession(); err != nil || ss == nil {
|
||
ok(w, map[string]any{"pendingTorque": 0, "pendingQueue": 0, "pendingTotal": 0,
|
||
"deadTotal": 0, "queue": []any{}, "queueDead": []any{}, "torque": []any{}})
|
||
return
|
||
} else {
|
||
queue, _ := ctx.Store.PendingQueueItemsForOperator(ss.UserID, 200)
|
||
queueDead, _ := ctx.Store.DeadQueueItemsForOperator(ss.UserID, 200)
|
||
torque, _ := ctx.Store.PendingTorqueResultsForOperator(ss.UserID, 200)
|
||
ok(w, map[string]any{
|
||
"pendingQueue": len(queue),
|
||
"pendingTorque": len(torque),
|
||
"pendingTotal": len(queue) + len(torque),
|
||
"queue": queue,
|
||
"queueDead": queueDead,
|
||
"deadTotal": len(queueDead),
|
||
"torque": torque,
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
// syncRetryQueueHandler POST /api/sync/retry-queue 人工重传处理:
|
||
// 操作工查看失败原因、重选空闲点位后再次提交。body {id, payload},payload 为修正后的完整 JSON body。
|
||
// 处理:校验归属当前登录人(数据按操作人隔离)→ 写入修正 payload 并重置为待重传 → 立即按当前登录人身份发一轮 →
|
||
// 回报本轮结果(synced=已发送 / rejected=再次被拒(含原因) / retrying=网络失败,下轮继续)。
|
||
func syncRetryQueueHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
ss, err := ctx.Store.GetStationSession()
|
||
if err != nil || ss == nil || ss.Token == "" {
|
||
fail(w, http.StatusUnauthorized, "登录已失效,请重新登录后再重传")
|
||
return
|
||
}
|
||
var req struct {
|
||
ID int64 `json:"id"`
|
||
Payload json.RawMessage `json:"payload"`
|
||
}
|
||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.ID <= 0 {
|
||
fail(w, http.StatusBadRequest, "请求体解析失败")
|
||
return
|
||
}
|
||
if len(req.Payload) == 0 {
|
||
fail(w, http.StatusBadRequest, "缺少上报内容(payload)")
|
||
return
|
||
}
|
||
// 归属校验:只能重传自己的数据(他人数据不被代发、也不可见)
|
||
item, err := ctx.Store.QueueItemByID(req.ID)
|
||
if err != nil {
|
||
fail(w, http.StatusNotFound, "记录不存在或已处理")
|
||
return
|
||
}
|
||
if item.OperatorID != ss.UserID {
|
||
fail(w, http.StatusForbidden, "只能重传本人录入的数据")
|
||
return
|
||
}
|
||
// 写入修正 payload,重置为待重传(同步器按当前登录人身份发送)
|
||
if err := ctx.Store.ResetQueueForRetry(req.ID, string(req.Payload)); err != nil {
|
||
fail(w, http.StatusInternalServerError, "重置重传状态失败")
|
||
return
|
||
}
|
||
ctx.Syncer.RunOnce(r.Context())
|
||
// 回报本轮结果
|
||
after, err := ctx.Store.QueueItemByID(req.ID)
|
||
if err != nil {
|
||
ok(w, map[string]any{"status": "retrying", "message": "已提交重传,结果下轮同步后可见"})
|
||
return
|
||
}
|
||
switch {
|
||
case after.Synced == 1:
|
||
ok(w, map[string]any{"status": "synced", "message": "重传成功,已上报 MES"})
|
||
case after.Synced == 2:
|
||
ok(w, map[string]any{"status": "rejected", "message": after.LastError, "reason": after.LastError})
|
||
default:
|
||
ok(w, map[string]any{"status": "retrying", "message": "MES 暂不可达,已留在队列等待下轮自动重传"})
|
||
}
|
||
}
|
||
}
|