fix(商品同步): 完善线上商品全量同步
This commit is contained in:
1 parent
0de1ec7635
commit
182cc75e63
4 files changed
+202
-15
No files matched your search
@@ -408,13 +408,21 @@ func (s *Server) syncItemsFromAccount(w http.ResponseWriter, r *http.Request) {
|
||||
if runtimeCookieChanged {
|
||||
s.updateRunningCookie(r.Context(), req.CookieID, runtimeCookie)
|
||||
}
|
||||
saved := s.saveSyncedItems(r.Context(), req.CookieID, res.Items)
|
||||
syncResult, syncErr := s.syncSyncedItems(r.Context(), req.CookieID, res.Items)
|
||||
if syncErr != nil {
|
||||
if s.Logger != nil {
|
||||
s.Logger.Error("同步商品到本地失败", "cookie_id", req.CookieID, "err", syncErr)
|
||||
}
|
||||
writeErr(w, http.StatusInternalServerError, "保存商品同步结果失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"message": "成功获取商品,共 " + strconv.Itoa(len(res.Items)) + " 件,保存 " + strconv.Itoa(saved) + " 件",
|
||||
"total_count": len(res.Items),
|
||||
"total_pages": res.TotalPages,
|
||||
"saved_count": saved,
|
||||
"success": true,
|
||||
"message": "成功获取商品,共 " + strconv.Itoa(len(res.Items)) + " 件,保存 " + strconv.Itoa(syncResult.Saved) + " 件,删除 " + strconv.Itoa(syncResult.Deleted) + " 件",
|
||||
"total_count": len(res.Items),
|
||||
"total_pages": res.TotalPages,
|
||||
"saved_count": syncResult.Saved,
|
||||
"deleted_count": syncResult.Deleted,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -554,6 +562,26 @@ func (s *Server) saveSyncedItems(ctx context.Context, cookieID string, items []m
|
||||
return saved
|
||||
}
|
||||
|
||||
func (s *Server) syncSyncedItems(ctx context.Context, cookieID string, items []mtop.ItemListItem) (db.ItemSyncResult, error) {
|
||||
rows := make([]db.ItemInfoRow, 0, len(items))
|
||||
for _, item := range items {
|
||||
priceText := item.PriceText
|
||||
if priceText == "" {
|
||||
priceText = item.Price
|
||||
}
|
||||
rows = append(rows, db.ItemInfoRow{
|
||||
CookieID: cookieID,
|
||||
ItemID: item.ID,
|
||||
ItemTitle: item.Title,
|
||||
ItemCategory: item.CategoryID,
|
||||
ItemPrice: priceText,
|
||||
ItemDetail: item.ItemDetail,
|
||||
IsMultiSpec: item.IsMultiSpec,
|
||||
})
|
||||
}
|
||||
return s.Store.Items.SyncFromRemote(ctx, cookieID, rows)
|
||||
}
|
||||
|
||||
func (s *Server) enrichSyncedItemMultiSpec(ctx context.Context, client mtop.Client, cookies, cookieID string, items []mtop.ItemListItem) {
|
||||
fetcher, ok := client.(mtop.ItemDetailFetcher)
|
||||
if !ok {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
"xianyu-go/internal/xianyu/mtop"
|
||||
)
|
||||
|
||||
@@ -373,6 +374,21 @@ func TestPublishItemStockPermissionMissing(t *testing.T) {
|
||||
func TestSyncItemsFromAccountSuccess(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
if err := store.Items.Upsert(ctx, &db.ItemInfoRow{
|
||||
CookieID: "acc1", ItemID: "it-sync-1", ItemTitle: "旧标题", ItemDescription: "本地描述", ItemPrice: "¥9.90",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Items.SetMultiSpec(ctx, "acc1", "it-sync-1", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Items.SetMultiQuantity(ctx, "acc1", "it-sync-1", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.Items.Upsert(ctx, &db.ItemInfoRow{CookieID: "acc1", ItemID: "it-removed", ItemTitle: "已删除商品"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prev := srv.MTop
|
||||
srv.MTop = withMTopTransport(roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
body := `{"ret":["SUCCESS::调用成功"],"data":{"cardList":[` +
|
||||
@@ -402,16 +418,18 @@ func TestSyncItemsFromAccountSuccess(t *testing.T) {
|
||||
if res["saved_count"] != float64(1) {
|
||||
t.Fatalf("应保存1件商品: %+v", res)
|
||||
}
|
||||
// 验证 DB 已写入。
|
||||
items, _ := store.Items.AllForCookie(context.Background(), "acc1")
|
||||
found := false
|
||||
for _, it := range items {
|
||||
if it.ItemID == "it-sync-1" && it.ItemTitle == "同步商品A" {
|
||||
found = true
|
||||
}
|
||||
if res["deleted_count"] != float64(1) {
|
||||
t.Fatalf("应删除1件已下架商品: %+v", res)
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("商品未保存: %+v", items)
|
||||
// 验证 DB 已写入。
|
||||
items, _ := store.Items.AllForCookie(ctx, "acc1")
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("同步后本地应只保留1件商品: %+v", items)
|
||||
}
|
||||
item := items[0]
|
||||
if item.ItemID != "it-sync-1" || item.ItemTitle != "同步商品A" || item.ItemPrice != "¥12.50" || item.ItemDescription != "本地描述" ||
|
||||
!item.IsMultiSpec || !item.MultiQuantityDelivery {
|
||||
t.Fatalf("线上商品更新或本地配置保留异常: %+v", item)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user