From 840c1dd5a46db78af313d01df032688651fd494d Mon Sep 17 00:00:00 2001 From: satori Date: Sun, 10 Nov 2024 12:21:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=92=E5=BA=8F=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/graphql.go | 103 +++++++------------------------------------------ 1 file changed, 15 insertions(+), 88 deletions(-) diff --git a/api/graphql.go b/api/graphql.go index 625c4b9..8208377 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -456,9 +456,6 @@ func NewSchema(config Config) (graphql.Schema, error) { } } - var list []int - var id_list [][]int - // 筛选:提取文字 if args.Text != "" { resp, err := models.ZincSearch(map[string]interface{}{ @@ -506,11 +503,23 @@ func NewSchema(config Config) (graphql.Schema, error) { for _, id := range models.GetSimilarImagesIdList(args.Similar, 200) { item = append(item, int(id)) } - id_list = append(id_list, item) - if len(id_list) == 0 { - return map[string]interface{}{"list": []Image{}, "total": 0}, nil + toGoquExpressions := func(ids []int) []goqu.Expression { + expressions := make([]goqu.Expression, len(ids)) + for i, id := range ids { + expressions[i] = goqu.V(id) + } + return expressions } + + expressions := toGoquExpressions(item) + interfaceExpressions := make([]interface{}, len(expressions)) + for i, expr := range expressions { + interfaceExpressions[i] = expr + } + + query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}}). + Order(goqu.Func("FIELD", append([]interface{}{goqu.I("web_images.id")}, interfaceExpressions...)...).Asc()) } // 筛选:兴趣推荐 @@ -578,77 +587,6 @@ func NewSchema(config Config) (graphql.Schema, error) { )) } - // 排序 - - // 截取:取交集 - if len(id_list) != 0 { - list = id_list[0] - if len(id_list) > 1 { - for _, slice := range id_list[1:] { - list = funk.Join(list, slice, funk.InnerJoin).([]int) - } - } - if len(list) == 0 { - return map[string]interface{}{"list": []Image{}, "total": 0}, nil - } - } - - total = len(list) - - //var count int64 - //if len(list) > 0 { - // query = query.Count(&count) - //} - - // 截取: 分页 - if args.After != 0 { - index := -1 - for i, id := range list { - if id == args.After { - index = i - break - } - } - if index != -1 { - list = list[index+1:] - } - } - - if args.Before != 0 { - index := -1 - for i, id := range list { - if id == args.Before { - index = i - break - } - } - if index != -1 { - list = list[:index] - } - } - - // 如果截取范围小于列表长度 - if args.First != 0 && args.First < len(list) { - list = list[:args.First] - } - - // 如果截取范围小于列表长度 - if args.Last != 0 && args.Last < len(list) { - list = list[len(list)-args.Last:] - } - - if args.First == 0 && args.Last == 0 { - if len(list) > 10 { - list = list[:10] - } - } - - // 存在外部筛选条件 - if len(id_list) > 0 && len(list) > 0 { - //query = query.Where("id IN ?", list) - //where = fmt.Sprintf("web_images.id IN ?", ) - } - // 取所有数据的前N条 sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL() @@ -696,17 +634,6 @@ func NewSchema(config Config) (graphql.Schema, error) { return nil, err } - //var order string - //// 排序规则 - //if p.Args["sort"] != nil && p.Args["order"] != nil { - // // query = query.Order(fmt.Sprintf("%s %s", args.Sort, args.Order)) - // order = "ORDER BY " + p.Args["sort"].(string) + " " + p.Args["order"].(string) + " " - //} - - //if total == 0 && count > 0 { - // total = int(count) - //} - return map[string]interface{}{ "list": images, "total": total,