fix: 多件档口入库码逐件回写 (#250)

This commit is contained in:
chengma
2026-08-18 15:18:35 +08:00
parent d2acbd821c
commit b8f49fe4bb
14 changed files with 547 additions and 111 deletions
+26 -2
View File
@@ -753,6 +753,30 @@ func (c *Client) DeleteInnerCode(ctx context.Context, detailID int64) error {
return classifyInnerCodeWriteError(err)
}
// CreateInnerCodeDetail 为同一货运单额外创建一件零价占位商品,并返回新的 detailId。
// 请求只发送一次;结果未知时返回 ErrWriteResultUnknown,调用方不得自动重试。
func (c *Client) CreateInnerCodeDetail(ctx context.Context, stockID int64, title string) (int64, error) {
title = strings.TrimSpace(title)
if stockID <= 0 {
return 0, fmt.Errorf("货运单 id 必须是正整数")
}
if title == "" || utf8.RuneCountInString(title) > 128 {
return 0, fmt.Errorf("占位商品标题不能为空且不能超过 128 个字符")
}
data, err := c.do(ctx, http.MethodPost, "/am/stock/detail/createDetail", nil, map[string]any{
"id": nil, "productTitle": title, "productSpec": nil,
"productQty": 1, "productPrice": 0, "stockId": stockID,
})
if err != nil {
return 0, classifyInnerCodeWriteError(err)
}
var detailID int64
if err := json.Unmarshal(data, &detailID); err != nil || detailID <= 0 {
return 0, fmt.Errorf("%w:新增明细响应缺少有效 detailId", ErrWriteResultUnknown)
}
return detailID, nil
}
// UpdateDetailCode 把档口入库码写入货运明细的 innerExpCode。
// 请求只发送一次;结果未知时返回 ErrWriteResultUnknown,调用方不得重试。
func (c *Client) UpdateDetailCode(ctx context.Context, stockID, detailID int64, code string) error {
@@ -771,8 +795,8 @@ func (c *Client) UpdateDetailCode(ctx context.Context, stockID, detailID int64,
return fmt.Errorf("code 不能包含控制字符")
}
}
_, err := c.do(ctx, http.MethodGet, "/am/stock/detail/updateDetailCode", url.Values{
"t": {"0"}, "id": {strconv.FormatInt(stockID, 10)},
_, err := c.do(ctx, http.MethodPost, "/am/stock/detail/updateDetailCode", url.Values{
"t": {strconv.FormatInt(time.Now().UnixMilli(), 10)}, "id": {strconv.FormatInt(stockID, 10)},
"detailId": {strconv.FormatInt(detailID, 10)}, "code": {code},
}, nil)
return classifyInnerCodeWriteError(err)
+25 -3
View File
@@ -569,8 +569,8 @@ func TestClient_DetailListByStock_超过100个id报错(t *testing.T) {
}
}
func TestClient_InnerCodeWrite_聚合码参数和路径正确且只发送一次(t *testing.T) {
targetCode := "DK260815A160101,DK260815A160102"
func TestClient_InnerCodeWrite_单件码参数和路径正确且只发送一次(t *testing.T) {
targetCode := "DK260815A160101"
var requests atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests.Add(1)
@@ -582,7 +582,7 @@ func TestClient_InnerCodeWrite_聚合码参数和路径正确且只发送一次(
}
case "/am/stock/detail/updateDetailCode":
query := r.URL.Query()
if query.Get("t") != "0" || query.Get("id") != "11" || query.Get("detailId") != "22" || query.Get("code") != targetCode {
if r.Method != http.MethodPost || query.Get("t") == "" || query.Get("id") != "11" || query.Get("detailId") != "22" || query.Get("code") != targetCode {
t.Errorf("update query=%v", query)
}
default:
@@ -606,6 +606,28 @@ func TestClient_InnerCodeWrite_聚合码参数和路径正确且只发送一次(
}
}
func TestClient_CreateInnerCodeDetail_使用零价单件请求并返回ID(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/am/stock/detail/createDetail" {
t.Fatalf("request=%s %s", r.Method, r.URL.Path)
}
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Fatal(err)
}
if body["stockId"] != float64(11) || body["productQty"] != float64(1) || body["productPrice"] != float64(0) || body["productTitle"] != "档口第2件-IC1" {
t.Fatalf("body=%v", body)
}
io.WriteString(w, `{"status":true,"msg":"创建成功","data":147372531,"code":null}`)
}))
defer server.Close()
client, _ := New(server.URL)
detailID, err := client.CreateInnerCodeDetail(context.Background(), 11, "档口第2件-IC1")
if err != nil || detailID != 147372531 {
t.Fatalf("detailID=%d err=%v", detailID, err)
}
}
func TestClient_InnerCodeWrite_未知结果与明确业务失败分开(t *testing.T) {
for _, tc := range []struct {
name string