feat: 展示任务PDD店铺和客户端名称 (#158)
This commit is contained in:
+39
-5
@@ -130,12 +130,38 @@ func isTaskWarn(s model.TaskStatus) bool {
|
||||
// `[必须]` 无主任务显示 `—`,不是空白也不是 `<nil>`——#17 之后
|
||||
// 采集任务默认无主,这一列会大量为空,显示不出来会让人以为页面坏了。
|
||||
func clientText(assignedClient string) string {
|
||||
if assignedClient == "" {
|
||||
if strings.TrimSpace(assignedClient) == "" {
|
||||
return placeholder
|
||||
}
|
||||
return assignedClient
|
||||
}
|
||||
|
||||
// clientDisplayName 优先使用操作员看得懂的客户端名称。
|
||||
// 名称为空或客户端记录已删除时回退稳定 ID,不能把已分配任务显示成空白。
|
||||
func clientDisplayName(clientID, clientName string) string {
|
||||
if name := strings.TrimSpace(clientName); name != "" {
|
||||
return name
|
||||
}
|
||||
return clientText(clientID)
|
||||
}
|
||||
|
||||
// clientDisplayTitle 给列表截断单元格提供完整名称和稳定 ID。
|
||||
func clientDisplayTitle(clientID, clientName string) string {
|
||||
name := strings.TrimSpace(clientName)
|
||||
id := strings.TrimSpace(clientID)
|
||||
if name != "" && id != "" {
|
||||
return name + "(" + id + ")"
|
||||
}
|
||||
return clientDisplayName(id, name)
|
||||
}
|
||||
|
||||
func optionalDisplayText(value string) string {
|
||||
if value = strings.TrimSpace(value); value != "" {
|
||||
return value
|
||||
}
|
||||
return placeholder
|
||||
}
|
||||
|
||||
// ---------- 目标列 ----------
|
||||
|
||||
// buildTarget 把一行任务的业务字段拼成「目标」列要显示的一句话。
|
||||
@@ -236,9 +262,11 @@ type TaskView struct {
|
||||
TypeText string
|
||||
ExecutionModeText string
|
||||
Target string
|
||||
PddShopText string
|
||||
StatusText string
|
||||
IsWarn bool // 失败 / 需人工,标黄提醒
|
||||
ClientText string
|
||||
ClientTitle string
|
||||
CreatorText string
|
||||
UpdatedAt string
|
||||
}
|
||||
@@ -321,9 +349,11 @@ func ListTasksView(db *sql.DB, filter repository.TaskFilter, requestedPage int)
|
||||
TypeText: taskTypeText(r.TaskType),
|
||||
ExecutionModeText: taskExecutionModeText(r.TaskType, r.ExecutionMode),
|
||||
Target: buildTarget(r),
|
||||
PddShopText: optionalDisplayText(r.PddShopName),
|
||||
StatusText: taskStatusText(r.Status),
|
||||
IsWarn: isTaskWarn(r.Status),
|
||||
ClientText: clientText(r.AssignedClient),
|
||||
ClientText: clientDisplayName(r.AssignedClient, r.ClientName),
|
||||
ClientTitle: clientDisplayTitle(r.AssignedClient, r.ClientName),
|
||||
CreatorText: taskCreatorText(r.CreatedByUsername),
|
||||
UpdatedAt: formatLocalTime(r.UpdatedAt),
|
||||
})
|
||||
@@ -421,11 +451,13 @@ type TaskDetailView struct {
|
||||
ExecutionModeText string
|
||||
StatusText string
|
||||
ClientText string
|
||||
ClientIDText string
|
||||
CreatorText string
|
||||
ClaimedAt string
|
||||
FinishedAt string
|
||||
|
||||
PddGoodsURL string
|
||||
PddShopText string
|
||||
|
||||
// IsPurchase 为 false 时,模板要把下面四个采购专有字段整段隐藏,
|
||||
// 不能显示空行——见 #19。
|
||||
@@ -458,7 +490,7 @@ func GetTaskDetail(db *sql.DB, taskID string) (*TaskDetailView, error) {
|
||||
if err != nil || t == nil {
|
||||
return nil, err
|
||||
}
|
||||
creatorUsername, err := repository.GetTaskCreatorUsername(db, taskID)
|
||||
displayContext, err := repository.GetTaskDisplayContext(db, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -468,11 +500,13 @@ func GetTaskDetail(db *sql.DB, taskID string) (*TaskDetailView, error) {
|
||||
TypeText: taskTypeText(t.TaskType),
|
||||
ExecutionModeText: taskExecutionModeText(t.TaskType, t.ExecutionMode),
|
||||
StatusText: taskStatusText(t.Status),
|
||||
ClientText: clientText(t.AssignedClient),
|
||||
CreatorText: taskCreatorText(creatorUsername),
|
||||
ClientText: clientDisplayName(t.AssignedClient, displayContext.ClientName),
|
||||
ClientIDText: clientText(t.AssignedClient),
|
||||
CreatorText: taskCreatorText(displayContext.CreatorUsername),
|
||||
ClaimedAt: formatLocalTime(t.ClaimedAt),
|
||||
FinishedAt: formatLocalTime(t.FinishedAt),
|
||||
PddGoodsURL: t.PddGoodsURL,
|
||||
PddShopText: optionalDisplayText(displayContext.PddShopName),
|
||||
IsPurchase: t.TaskType == model.TaskPurchase,
|
||||
}
|
||||
if v.IsPurchase {
|
||||
|
||||
@@ -285,6 +285,76 @@ func TestListTasksView_无主任务客户端列显示占位符(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTasksView_显示PDD店铺和客户端名称并保留ID提示(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
now := model.NowISO()
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO pdd_products (goods_id, url, title, shop_name, collect_status, created_at, updated_at)
|
||||
VALUES ('737116531267', 'https://mobile.yangkeduo.com/goods.html?goods_id=737116531267',
|
||||
'测试商品', '测试旗舰店', 'collected', ?, ?)`, now, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO clients (client_id, name, last_seen_at, created_at, updated_at)
|
||||
VALUES ('client-001', '办公室手机', ?, ?, ?)`, now, now, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
insertTestTask(t, db, testTaskParams{
|
||||
taskID: "COL-SHOP", taskType: model.TaskCollect, status: model.TaskAssigned,
|
||||
pddGoodsID: "737116531267", assignedClient: "client-001",
|
||||
})
|
||||
|
||||
r, err := ListTasksView(db, repository.TaskFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(r.Rows) != 1 || r.Rows[0].PddShopText != "测试旗舰店" {
|
||||
t.Fatalf("列表应显示 PDD 店铺,实际 %+v", r.Rows)
|
||||
}
|
||||
if r.Rows[0].ClientText != "办公室手机" ||
|
||||
r.Rows[0].ClientTitle != "办公室手机(client-001)" {
|
||||
t.Errorf("客户端应显示名称并在提示保留 ID,实际 text=%q title=%q",
|
||||
r.Rows[0].ClientText, r.Rows[0].ClientTitle)
|
||||
}
|
||||
|
||||
if _, err := db.Exec(`UPDATE clients SET name='仓库手机' WHERE client_id='client-001'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
renamed, err := ListTasksView(db, repository.TaskFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if renamed.Rows[0].ClientText != "仓库手机" {
|
||||
t.Errorf("客户端重命名后列表应显示当前名称,实际 %q", renamed.Rows[0].ClientText)
|
||||
}
|
||||
|
||||
detail, err := GetTaskDetail(db, "COL-SHOP")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.PddShopText != "测试旗舰店" || detail.ClientText != "仓库手机" ||
|
||||
detail.ClientIDText != "client-001" {
|
||||
t.Errorf("详情展示信息不完整:%+v", detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTasksView_关联缺失时店铺占位且客户端回退ID(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
insertTestTask(t, db, testTaskParams{
|
||||
taskID: "COL-FALLBACK", taskType: model.TaskCollect, status: model.TaskAssigned,
|
||||
pddGoodsID: "missing-product", assignedClient: "deleted-client",
|
||||
})
|
||||
|
||||
r, err := ListTasksView(db, repository.TaskFilter{}, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.Rows[0].PddShopText != "—" || r.Rows[0].ClientText != "deleted-client" ||
|
||||
r.Rows[0].ClientTitle != "deleted-client" {
|
||||
t.Errorf("关联缺失回退错误:%+v", r.Rows[0])
|
||||
}
|
||||
}
|
||||
|
||||
// ── 统计跟随筛选 ──────────────────────────────────────
|
||||
|
||||
func TestListTasksView_统计跟随当前筛选(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user