feat: PDD 批量采集支持可选客户端 (#74)
This commit is contained in:
+27
-3
@@ -558,7 +558,7 @@ func (r CollectTaskResult) Skipped() int {
|
||||
return r.SkippedCollecting + r.SkippedCollected + r.SkippedDeleted
|
||||
}
|
||||
|
||||
// CreatePddCollectTasks 为勾选的 PDD 商品创建采集任务。
|
||||
// CreatePddCollectTasks 为勾选的 PDD 商品创建不指定客户端的采集任务。
|
||||
//
|
||||
// `[必须]` **不指定客户端**(assigned_client 为 NULL,status 为 pending),
|
||||
// 谁领到就在领取时标记谁,见 #17。采集是纯读取操作,哪台机器跑都一样,
|
||||
@@ -576,6 +576,20 @@ func (r CollectTaskResult) Skipped() int {
|
||||
// 返回的跳过分类必须显示给操作员。静默跳过的话,
|
||||
// 操作员会以为任务建好了,等半天没动静也不知道为什么。
|
||||
func CreatePddCollectTasks(db *sql.DB, goodsIDs []string) (CollectTaskResult, error) {
|
||||
return createPddCollectTasks(db, goodsIDs, "", "")
|
||||
}
|
||||
|
||||
// CreatePddCollectTasksForUser 为 PDD 批量页面创建可选客户端的采集任务。
|
||||
// 即使浏览器伪造 clientID,也必须重新按当前登录账号校验可见范围。
|
||||
func CreatePddCollectTasksForUser(db *sql.DB, actor *model.User, goodsIDs []string, clientID string) (CollectTaskResult, error) {
|
||||
visibleUserID, err := visibleClientUserID(actor)
|
||||
if err != nil {
|
||||
return CollectTaskResult{}, err
|
||||
}
|
||||
return createPddCollectTasks(db, goodsIDs, strings.TrimSpace(clientID), visibleUserID)
|
||||
}
|
||||
|
||||
func createPddCollectTasks(db *sql.DB, goodsIDs []string, clientID, visibleUserID string) (CollectTaskResult, error) {
|
||||
var result CollectTaskResult
|
||||
|
||||
goodsIDs = dedupe(goodsIDs)
|
||||
@@ -589,6 +603,16 @@ func CreatePddCollectTasks(db *sql.DB, goodsIDs []string) (CollectTaskResult, er
|
||||
}
|
||||
defer tx.Rollback() // 已提交的事务再 Rollback 是空操作,安全
|
||||
|
||||
if clientID != "" {
|
||||
visible, err := repository.ClientVisibleToUser(tx, clientID, visibleUserID)
|
||||
if err != nil {
|
||||
return CollectTaskResult{}, err
|
||||
}
|
||||
if !visible {
|
||||
return CollectTaskResult{}, invalidInput("所选客户端不存在或不在当前账号可见范围")
|
||||
}
|
||||
}
|
||||
|
||||
// 跳过原因里"正在采集"的那些,各自还要等多久才超时可重试;
|
||||
// 取其中最快的一个,给操作员一个"下一步该等多久"的具体数字。
|
||||
var (
|
||||
@@ -632,8 +656,8 @@ func CreatePddCollectTasks(db *sql.DB, goodsIDs []string) (CollectTaskResult, er
|
||||
continue
|
||||
}
|
||||
|
||||
if err := repository.InsertCollectTask(
|
||||
tx, newCollectTaskID(), p.GoodsID, p.URL); err != nil {
|
||||
if err := repository.InsertCollectTaskForClient(
|
||||
tx, newCollectTaskID(), p.GoodsID, p.URL, clientID); err != nil {
|
||||
return CollectTaskResult{}, err
|
||||
}
|
||||
result.Created++
|
||||
|
||||
Reference in New Issue
Block a user