package handler import ( "net/http" "strconv" "strings" "bj_power_wms/internal/svc" "bj_power_wms/internal/syncfailure" ) // syncFailureListHandler GET /api/sync-failures 同步失败台账列表(人工可见,不自动重试) func syncFailureListHandler(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { rows, err := syncfailure.List(ctx.SQLDB, r.URL.Query().Get("status"), r.URL.Query().Get("bizType")) if err != nil { fail(w, http.StatusInternalServerError, "查询同步失败台账失败(部署时请先手工执行迁移 SQL 建表): "+err.Error()) return } ok(w, rows) } } // syncFailureRetryHandler POST /api/sync-failures/retry?id= // 只按台账 endpoint + payload_json 原样重放通知,绝不重跑 doGeneralOutbound 等本地业务。 // 重试前先查对端终态;已终态直接置 SUCCESS,不重复通知。 func syncFailureRetryHandler(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(strings.TrimSpace(r.URL.Query().Get("id")), 10, 64) if err != nil || id <= 0 { fail(w, http.StatusBadRequest, "缺少有效的 id") return } row, err := syncfailure.Get(ctx.SQLDB, id) if err != nil { fail(w, http.StatusNotFound, "台账记录不存在: "+err.Error()) return } if row.Status == "SUCCESS" { ok(w, map[string]any{"id": id, "status": "SUCCESS", "note": "已成功,无需重试"}) return } if ctx.Mes == nil { fail(w, http.StatusInternalServerError, "MES 未配置,无法重放通知") return } payload := syncfailure.Payload(row) // 叫料完成备料:先查 MES 叫料单终态,避免通知已成功但响应丢失时重复回写 if row.BizType == "call.complete" { if requestNo, isStr := payload["requestNo"].(string); isStr && requestNo != "" { status, serr := ctx.Mes.CallStatus(r.Context(), requestNo) if serr == nil && status != "" && status != "PENDING" { _ = syncfailure.MarkSuccess(ctx.SQLDB, id) ok(w, map[string]any{"id": id, "status": "SUCCESS", "note": "对端已终态(" + status + "),无需重放"}) return } } } if err := ctx.Mes.PostRaw(r.Context(), row.Endpoint, payload); err != nil { _ = syncfailure.MarkRetryFailed(ctx.SQLDB, id, err.Error()) // 对端非 PENDING 属「通知其实已成功」,按幂等成功收口 if strings.Contains(err.Error(), "仅待备料的叫料单可完成备料") { _ = syncfailure.MarkSuccess(ctx.SQLDB, id) ok(w, map[string]any{"id": id, "status": "SUCCESS", "note": "对端已终态,无需重放"}) return } fail(w, http.StatusBadGateway, "重试失败(已计入 retry_count): "+err.Error()) return } _ = syncfailure.MarkSuccess(ctx.SQLDB, id) ctx.EventLog.Write(ctx0(), "sync.failure.retry", "warehouse", "sync_failure", strconv.FormatInt(id, 10), "同步失败台账人工重试成功:"+row.BizType+" / "+row.BizKey, nil) ok(w, map[string]any{"id": id, "status": "SUCCESS"}) } } // syncFailureIgnoreHandler POST /api/sync-failures/ignore?id= 线下已处理 → DEAD 留痕 func syncFailureIgnoreHandler(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(strings.TrimSpace(r.URL.Query().Get("id")), 10, 64) if err != nil || id <= 0 { fail(w, http.StatusBadRequest, "缺少有效的 id") return } operator := r.Header.Get("X-Username") if operator == "" { operator = "warehouse" } if err := syncfailure.Ignore(ctx.SQLDB, id, operator); err != nil { fail(w, http.StatusInternalServerError, "标记已处理失败: "+err.Error()) return } ctx.EventLog.Write(ctx0(), "sync.failure.ignore", operator, "sync_failure", strconv.FormatInt(id, 10), "同步失败台账人工标记已处理", nil) ok(w, map[string]any{"id": id, "status": "DEAD"}) } }