feat: 增加批量铺货发布强制间隔
This commit is contained in:
1 parent
a90c9422c1
commit
be080e1500
39 files changed
+362
-75
No files matched your search
@@ -192,6 +192,12 @@ func (s *Server) previewItemPublishBatch(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// batchLocation 用于本次流程后续判断的批次地址
|
||||
var batchLocation publishBatchLocation
|
||||
// publishIntervalSeconds 保存用户为本批次设置的最终发布最小间隔,单位为秒。
|
||||
publishIntervalSeconds, intervalErr := parsePublishIntervalSeconds(r.FormValue("publish_interval_seconds"))
|
||||
if intervalErr != nil {
|
||||
writeErr(w, http.StatusBadRequest, intervalErr.Error())
|
||||
return
|
||||
}
|
||||
// locationJSON 用于本次流程后续判断的地址JSON
|
||||
locationJSON := strings.TrimSpace(r.FormValue("location"))
|
||||
if locationJSON != "" {
|
||||
@@ -307,7 +313,8 @@ func (s *Server) previewItemPublishBatch(w http.ResponseWriter, r *http.Request)
|
||||
// preview、err 保存预检结果持久化服务返回的应用模型及错误。
|
||||
preview, err := s.itemBatchPreviewPersistenceApplication().Persist(r.Context(), itemapp.BatchPreviewPersistenceBatch{
|
||||
ID: batchID, UserID: sess.UserID, DefaultCookieID: defaultCookieID, Filename: sourceName,
|
||||
UploadDir: uploadDir, Location: itemapp.Location{Area: batchLocation.Area, City: batchLocation.City, DivisionID: batchLocation.DivisionID, Longitude: batchLocation.Longitude, Latitude: batchLocation.Latitude, POIID: batchLocation.POIID, POIName: batchLocation.POIName, Province: batchLocation.Province},
|
||||
UploadDir: uploadDir, PublishIntervalSeconds: publishIntervalSeconds,
|
||||
Location: itemapp.Location{Area: batchLocation.Area, City: batchLocation.City, DivisionID: batchLocation.DivisionID, Longitude: batchLocation.Longitude, Latitude: batchLocation.Latitude, POIID: batchLocation.POIID, POIName: batchLocation.POIName, Province: batchLocation.Province},
|
||||
}, previewRows)
|
||||
if err != nil {
|
||||
if errors.Is(err, itemapp.ErrBatchPreviewNoRows) {
|
||||
@@ -618,7 +625,7 @@ func publishBatchApplicationToResponse(batch itemapp.BatchInfo, rows []itemapp.B
|
||||
ID: batch.ID, Status: batch.Status, Filename: batch.Filename,
|
||||
Total: batch.TotalCount, Success: batch.SuccessCount, Failed: batch.FailedCount,
|
||||
Pending: pending, Running: running, Retryable: retryable, Rows: outRows,
|
||||
Location: json.RawMessage(locationJSON), CreatedAt: batch.CreatedAt, UpdatedAt: batch.UpdatedAt,
|
||||
Location: json.RawMessage(locationJSON), PublishIntervalSeconds: batch.PublishIntervalSeconds, CreatedAt: batch.CreatedAt, UpdatedAt: batch.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -708,6 +715,24 @@ func atoiPublishDefault(raw string, def int) int {
|
||||
return def
|
||||
}
|
||||
|
||||
// parsePublishIntervalSeconds 解析批量铺货的最终发布最小间隔,空值使用五秒默认值。
|
||||
func parsePublishIntervalSeconds(raw string) (int, error) {
|
||||
// normalized 保存去除空白后的用户输入;空值保持兼容默认五秒。
|
||||
normalized := strings.TrimSpace(raw)
|
||||
if normalized == "" {
|
||||
return 5, nil
|
||||
}
|
||||
// interval、err 保存严格解析后的秒数与输入错误。
|
||||
interval, err := strconv.Atoi(normalized)
|
||||
if err != nil {
|
||||
return 0, errors.New("发布间隔必须是整数秒")
|
||||
}
|
||||
if interval < 1 || interval > 3600 {
|
||||
return 0, errors.New("发布间隔必须是 1 到 3600 秒")
|
||||
}
|
||||
return interval, nil
|
||||
}
|
||||
|
||||
// firstNonEmpty 返回参数中第一个非空白字符串。
|
||||
func firstNonEmpty(values ...string) string {
|
||||
// v 是当前遍历到的候选字符串。
|
||||
@@ -728,7 +753,7 @@ type serverBatchPublisher struct {
|
||||
}
|
||||
|
||||
// PublishRow 调用批量远端端口并收口本地结果,应用层不接触数据库或 MTOP 类型。
|
||||
func (p serverBatchPublisher) PublishRow(ctx context.Context, userID int64, row itemapp.BatchRow, workerToken string) error {
|
||||
func (p serverBatchPublisher) PublishRow(ctx context.Context, userID int64, row itemapp.BatchRow, workerToken string, beforePublish func(context.Context) error) error {
|
||||
if p.remotePort == nil {
|
||||
return errors.New("批量发布端口未装配")
|
||||
}
|
||||
@@ -736,7 +761,7 @@ func (p serverBatchPublisher) PublishRow(ctx context.Context, userID int64, row
|
||||
return errors.New("批量发布本地收口端口未装配")
|
||||
}
|
||||
// outcome、err 保存远端发布结果及适配器错误。
|
||||
outcome, err := p.remotePort.PublishRemoteRow(ctx, userID, row, workerToken)
|
||||
outcome, err := p.remotePort.PublishRemoteRow(ctx, userID, row, workerToken, beforePublish)
|
||||
if err != nil {
|
||||
// uncertainErr 表示远端调用已经发生但结果无法可靠确认。
|
||||
var uncertainErr *itemapp.UncertainRemotePublishError
|
||||
|
||||
@@ -531,7 +531,7 @@ func TestPublishBatchRetryResumesSavedRemoteResultWithoutPublishingAgain(t *test
|
||||
// applicationRow 保存恢复测试所需的非敏感批次明细模型。
|
||||
applicationRow := itemapp.BatchRow{ID: rows[0].ID, BatchID: rows[0].BatchID, RowNo: rows[0].RowNo, CookieID: rows[0].CookieID, Title: rows[0].Title, Description: rows[0].Description, Price: rows[0].Price, OriginalPrice: rows[0].OriginalPrice, Quantity: rows[0].Quantity, PostageMode: rows[0].PostageMode, Postage: rows[0].Postage, ImagesJSON: rows[0].ImagesJSON, CategoryJSON: rows[0].CategoryJSON, AutomationJSON: rows[0].AutomationJSON, RawJSON: rows[0].RawJSON, ItemID: rows[0].ItemID, ItemURL: rows[0].ItemURL, Status: rows[0].Status, ErrorMessage: rows[0].ErrorMessage, FailureKind: rows[0].FailureKind, WorkerToken: rows[0].WorkerToken, CreatedAt: rows[0].CreatedAt, UpdatedAt: rows[0].UpdatedAt}
|
||||
// err 保存应用层批量发布适配器的恢复结果。
|
||||
err = (serverBatchPublisher{remotePort: port, localService: localService}).PublishRow(ctx, admin.ID, applicationRow, "worker-2")
|
||||
err = (serverBatchPublisher{remotePort: port, localService: localService}).PublishRow(ctx, admin.ID, applicationRow, "worker-2", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("resume local persistence: %v", err)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,38 @@ import (
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// TestParsePublishIntervalSeconds 验证批量发布间隔的默认值与边界校验。
|
||||
func TestParsePublishIntervalSeconds(t *testing.T) {
|
||||
// cases 保存空值、有效值和越界值的确定性解析样例。
|
||||
cases := []struct {
|
||||
name string
|
||||
raw string
|
||||
want int
|
||||
bad bool
|
||||
}{
|
||||
{name: "default", raw: "", want: 5},
|
||||
{name: "custom", raw: "12", want: 12},
|
||||
{name: "not-number", raw: "abc", bad: true},
|
||||
{name: "zero", raw: "0", bad: true},
|
||||
{name: "too-large", raw: "3601", bad: true},
|
||||
}
|
||||
for _, testCase := range cases { // testCase 表示当前发布间隔解析样例。
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
// got、err 保存当前样例的解析结果。
|
||||
got, err := parsePublishIntervalSeconds(testCase.raw)
|
||||
if testCase.bad {
|
||||
if err == nil {
|
||||
t.Fatalf("非法间隔应返回错误,got=%d", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || got != testCase.want {
|
||||
t.Fatalf("解析结果=%d err=%v want=%d", got, err, testCase.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestParsePublishSheetBytes 表格解析各格式。
|
||||
func TestParsePublishSheetBytes(t *testing.T) {
|
||||
// CSV。
|
||||
@@ -69,7 +101,7 @@ func TestParsePublishSheetBytesWithLimitRejectsTooManyRows(t *testing.T) {
|
||||
// TestPreviewItemPublishBatchCSV 预检 CSV 批量发布(含图片 zip)。
|
||||
func TestPreviewItemPublishBatchCSV(t *testing.T) {
|
||||
// srv、cleanup 用于本次流程后续判断的srv、cleanup
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// h 用于本次流程后续判断的h
|
||||
h := srv.Router()
|
||||
@@ -95,6 +127,7 @@ func TestPreviewItemPublishBatchCSV(t *testing.T) {
|
||||
_ = mw.WriteField("fallback_category_id", "5001")
|
||||
_ = mw.WriteField("fallback_category_name", "虚拟商品")
|
||||
_ = mw.WriteField("fallback_channel_category_id", "6001")
|
||||
_ = mw.WriteField("publish_interval_seconds", "12")
|
||||
// csvField 用于本次流程后续判断的csv字段
|
||||
csvField, _ := mw.CreateFormFile("file", "products.csv")
|
||||
csvField.Write([]byte("账号ID,标题,价格,库存,图片\nacc1,商品A,12.50,5,img/a.png\n"))
|
||||
@@ -126,6 +159,11 @@ func TestPreviewItemPublishBatchCSV(t *testing.T) {
|
||||
if category["cat_id"] != "5001" || category["cat_name"] != "虚拟商品" {
|
||||
t.Fatalf("预检未保存兜底类目: %+v", category)
|
||||
}
|
||||
// storedBatch 验证页面设置的发布间隔已经进入批次持久化模型。
|
||||
storedBatch, storedErr := store.PublishBatches.Get(context.Background(), 1, res["preview_id"].(string))
|
||||
if storedErr != nil || storedBatch.PublishIntervalSeconds != 12 {
|
||||
t.Fatalf("批量发布间隔未持久化: batch=%+v err=%v", storedBatch, storedErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPreviewItemPublishBatchAllowsEmptyDefaultCategory 封装TestPreview商品发布批次AllowsEmptyDefault分类业务协调。
|
||||
|
||||
@@ -1151,6 +1151,8 @@ type itemPublishBatchResponse struct {
|
||||
Rows []itemPublishBatchRowResponse `json:"rows"`
|
||||
// Location 是批次统一发货地对象。
|
||||
Location any `json:"location"`
|
||||
// PublishIntervalSeconds 是相邻两次最终商品发布请求的最小间隔秒数。
|
||||
PublishIntervalSeconds int `json:"publish_interval_seconds"`
|
||||
// CreatedAt 是任务创建时间。
|
||||
CreatedAt string `json:"created_at"`
|
||||
// UpdatedAt 是任务更新时间。
|
||||
|
||||
Reference in new issue
Block a user