diff --git a/common/dto/search.go b/common/dto/search.go index 72cc60e4..58f50aca 100644 --- a/common/dto/search.go +++ b/common/dto/search.go @@ -13,21 +13,17 @@ type GeneralDelDto struct { func (g GeneralDelDto) GetIds() []int { ids := make([]int, 0) - if g.Id != 0 { + // Id 此前在 else 分支里被重复追加:仅传 Id 时会得到 [5 5], + // 同一条记录被执行两次删除 + if g.Id > 0 { ids = append(ids, g.Id) } - if len(g.Ids) > 0 { - for _, id := range g.Ids { - if id > 0 { - ids = append(ids, id) - } - } - } else { - if g.Id > 0 { - ids = append(ids, g.Id) + for _, id := range g.Ids { + if id > 0 { + ids = append(ids, id) } } - if len(ids) <= 0 { + if len(ids) == 0 { //方式全部删除 ids = append(ids, 0) } diff --git a/common/dto/search_test.go b/common/dto/search_test.go new file mode 100644 index 00000000..16897fe1 --- /dev/null +++ b/common/dto/search_test.go @@ -0,0 +1,32 @@ +package dto + +import ( + "reflect" + "testing" +) + +// GetIds 曾在 else 分支中重复追加 Id:仅传 Id 时返回 [5 5], +// 导致删除接口对同一条记录执行两次。 +func TestGeneralDelDtoGetIds(t *testing.T) { + cases := []struct { + name string + dto GeneralDelDto + want []int + }{ + {"仅 Id", GeneralDelDto{Id: 5}, []int{5}}, + {"仅 Ids", GeneralDelDto{Ids: []int{1, 2}}, []int{1, 2}}, + {"Id 与 Ids 并存", GeneralDelDto{Id: 5, Ids: []int{1, 2}}, []int{5, 1, 2}}, + {"Ids 含非正数被过滤", GeneralDelDto{Ids: []int{0, -1, 3}}, []int{3}}, + {"Id 为 0 视为未传", GeneralDelDto{Id: 0, Ids: []int{7}}, []int{7}}, + {"全部为空时回退到 0(全量删除约定)", GeneralDelDto{}, []int{0}}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got := c.dto.GetIds() + if !reflect.DeepEqual(got, c.want) { + t.Errorf("GetIds() = %v, want %v", got, c.want) + } + }) + } +}