diff --git a/internal/web/fillup.go b/internal/web/fillup.go index 4bfa36a..3e41aa2 100644 --- a/internal/web/fillup.go +++ b/internal/web/fillup.go @@ -315,8 +315,14 @@ func (s *Server) adminFillupApply(w http.ResponseWriter, r *http.Request) { } for _, pid := range ids { - // 框里填的是**活动名**:对得上就用现有的,对不上就新建一个 - if name := strings.TrimSpace(r.PostFormValue("act_" + strconv.FormatInt(pid, 10))); name != "" { + // 框里填的是**活动名**:对得上就用现有的,对不上就新建一个。 + // 下拉选了「其它(自己写)」时,真正要读的是旁边那个文本框——与类别同一套写法。 + actKey := "act_" + strconv.FormatInt(pid, 10) + actName := strings.TrimSpace(r.PostFormValue(actKey)) + if actName == "__custom__" { + actName = strings.TrimSpace(r.PostFormValue(actKey + "__custom")) + } + if name := actName; name != "" { actID, ok := actByName[name] if !ok { pk, err := s.nextSrcPK(ctx, ex, ds.ID, activitySeq) diff --git a/internal/web/fillup_test.go b/internal/web/fillup_test.go index a4a4932..d728505 100644 --- a/internal/web/fillup_test.go +++ b/internal/web/fillup_test.go @@ -266,3 +266,38 @@ func TestFillupCustomCategoryEmptyIsSkipped(t *testing.T) { t.Fatalf("没填内容时不该写库,实际 %q", got) } } + +// 活动下拉选「其它(自己写)」时,读的是旁边文本框里的名字——与类别同一套写法。 +func TestFillupCustomActivity(t *testing.T) { + f := newMovementFixture(t) + placeID, _ := fillupPlace(t, f) + key := "act_" + strconv.FormatInt(placeID, 10) + + applyFillup(t, f, url.Values{key: {"__custom__"}, key + "__custom": {"爬山"}}) + + var got string + if err := f.pg.QueryRowContext(f.ctx, `SELECT a.name FROM visits v JOIN activities a ON a.id=v.activity_id + WHERE v.dataset_id=$1 AND v.place_id=$2 LIMIT 1`, f.dsID, placeID).Scan(&got); err != nil { + t.Fatalf("读活动失败: %v", err) + } + if got != "爬山" { + t.Fatalf("应写入手工填的活动名,实际 %q", got) + } + t.Cleanup(func() { + f.pg.ExecContext(f.ctx, `DELETE FROM activities WHERE dataset_id=$1 AND name=$2`, f.dsID, "爬山") + }) +} + +// 选了「其它」却没写名字:什么都不做,别建一个空名活动。 +func TestFillupCustomActivityEmptyIsSkipped(t *testing.T) { + f := newMovementFixture(t) + placeID, _ := fillupPlace(t, f) + key := "act_" + strconv.FormatInt(placeID, 10) + before := f.scalar(t, `SELECT count(*) FROM activities WHERE dataset_id=$1`, f.dsID) + + applyFillup(t, f, url.Values{key: {"__custom__"}, key + "__custom": {" "}}) + + if n := f.scalar(t, `SELECT count(*) FROM activities WHERE dataset_id=$1`, f.dsID); n != before { + t.Fatalf("没填名字不该建类型,活动数 %d → %d", before, n) + } +} diff --git a/internal/web/templates/admin/fillup.html b/internal/web/templates/admin/fillup.html index 3d93760..e0ee1a3 100644 --- a/internal/web/templates/admin/fillup.html +++ b/internal/web/templates/admin/fillup.html @@ -52,14 +52,21 @@ {{$row.Missing}} {{if $row.NoAct}} - + {{$ahint := $row.ActHint}} + + {{else}}—{{end}} {{if $row.NoCat}} {{$hint := $row.CatHint}} - {{if $hint}}{{end}} {{range $.Categories}}{{if ne .Value $hint}}{{end}}{{end}} @@ -103,16 +110,12 @@ {{end}} - - {{range .Activities}}{{end}} - -