Files
cmautobuy/admin/service/shopee_delete.go
T

80 lines
1.9 KiB
Go

package service
import (
"database/sql"
"fmt"
"sort"
"strings"
"time"
"cmautobuy/admin/model"
"cmautobuy/admin/repository"
)
func normalizeShopeeIDs(ids []string) []string {
seen := map[string]struct{}{}
result := make([]string, 0, len(ids))
for _, id := range ids {
id = strings.TrimSpace(id)
if id == "" {
continue
}
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
result = append(result, id)
}
sort.Strings(result)
return result
}
// DeleteShopeeProducts 由管理员把商品移入可恢复的删除状态。
func DeleteShopeeProducts(db *sql.DB, actor *model.User, ids []string) (int64, error) {
if actor == nil || !actor.IsAdmin() {
return 0, ErrAdminRequired
}
ids = normalizeShopeeIDs(ids)
if len(ids) == 0 {
return 0, invalidInput("没有勾选任何蝦皮商品")
}
tx, err := db.Begin()
if err != nil {
return 0, err
}
defer tx.Rollback()
busy, err := repository.ShopeeProductsWithActiveTasks(tx, ids)
if err != nil {
return 0, err
}
if len(busy) > 0 {
return 0, invalidInput(fmt.Sprintf("商品 %s 仍有进行中的采集或采购任务,不能删除", strings.Join(busy, "、")))
}
count, err := repository.SetShopeeProductsDeleted(tx, ids, actor.UserID, time.Now().UTC().Format(model.TimeLayout))
if err != nil {
return 0, err
}
return count, tx.Commit()
}
// RestoreShopeeProducts 由管理员恢复软删除商品,原 SKU 和 PDD 关联保持不变。
func RestoreShopeeProducts(db *sql.DB, actor *model.User, ids []string) (int64, error) {
if actor == nil || !actor.IsAdmin() {
return 0, ErrAdminRequired
}
ids = normalizeShopeeIDs(ids)
if len(ids) == 0 {
return 0, invalidInput("没有勾选任何蝦皮商品")
}
tx, err := db.Begin()
if err != nil {
return 0, err
}
defer tx.Rollback()
count, err := repository.SetShopeeProductsDeleted(tx, ids, actor.UserID, "")
if err != nil {
return 0, err
}
return count, tx.Commit()
}