diff --git a/api/graphql.go b/api/graphql.go index 2c8a348..1cb88ff 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -1,11 +1,9 @@ package api import ( - "encoding/json" "fmt" "log" "reflect" - "sort" "strconv" "strings" @@ -460,7 +458,7 @@ func NewSchema(config Config) (graphql.Schema, error) { // 筛选条件 for _, format := range argFormat { if p.Args[format] != nil { - query = query.Where(fmt.Sprintf("%s = ?", format), p.Args[format]) + query = query.Where(fmt.Sprintf("web_images.%s = ?", format), p.Args[format]) } } @@ -526,28 +524,26 @@ func NewSchema(config Config) (graphql.Schema, error) { fmt.Println("Interest:", args.Interest) } - // 数据库中筛选: 关注列表 - if args.Follower != 0 { - // 返回JSON数组(2.5秒) - var item string - if err := db.Raw(` - SELECT JSON_ARRAYAGG(web_images.id) AS json_result - FROM web_fans - INNER JOIN web_images ON web_images.user_id = web_fans.blogger_id - WHERE web_fans.follower_id = ? AND web_images.article_category_top_id = 22 - `, args.Follower).Scan(&item).Error; err != nil { - fmt.Println("获取关注列表失败", err) - return nil, err - } - - var ids []int - json.Unmarshal([]byte(item), &ids) - - sort.Slice(ids, func(i, j int) bool { - return ids[i] > ids[j] // 按照降序排列 - }) - id_list = append(id_list, ids) - } + //// 数据库中筛选: 关注列表 + //if args.Follower != 0 { + // // 返回JSON数组(2.5秒) + // var item string + // if err := db.Raw(` + // SELECT JSON_ARRAYAGG(web_images.id) AS json_result + // FROM web_fans + // INNER JOIN web_images ON web_images.user_id = web_fans.blogger_id + // WHERE web_fans.follower_id = ? AND web_images.article_category_top_id = 22 + // `, args.Follower).Scan(&item).Error; err != nil { + // fmt.Println("获取关注列表失败", err) + // return nil, err + // } + // var ids []int + // json.Unmarshal([]byte(item), &ids) + // sort.Slice(ids, func(i, j int) bool { + // return ids[i] > ids[j] // 按照降序排列 + // }) + // id_list = append(id_list, ids) + //} // 筛选:时间段 applyTimeCondition := func(name string, str string) { @@ -561,15 +557,19 @@ func NewSchema(config Config) (graphql.Schema, error) { } } - if createTime, ok := p.Args["create_time"].(string); ok && createTime != "" { - applyTimeCondition("create_time", createTime) - } else if updateTime, ok := p.Args["update_time"].(string); ok && updateTime != "" { - applyTimeCondition("update_time", updateTime) + // 数据库中筛选:创建时间段 + if p.Args["create_time"] != nil { + applyTimeCondition("create_time", p.Args["create_time"].(string)) + } + + // 数据库中筛选:更新时间段 + if p.Args["update_time"] != nil { + applyTimeCondition("update_time", p.Args["update_time"].(string)) } // 数据库中筛选:横屏纵屏 - if args.Orientation != "" { - query.Where("article_orientation = ?", args.Orientation) + if p.Args["orientation"] != nil { + query.Where("article_orientation = ?", p.Args["orientation"]) } // 数据库中筛选:游戏设备 @@ -582,6 +582,11 @@ func NewSchema(config Config) (graphql.Schema, error) { query = query.Joins("JOIN web_article ON web_images.article_id = web_article.id AND web_article.era = ?", p.Args["era"]) } + // 数据库中筛选:按关注列表 + if p.Args["follower"] != nil { + query = query.Joins("JOIN web_fans ON web_images.user_id = web_fans.blogger_id AND web_fans.follower_id = ?", p.Args["follower"]) + } + // 排序 // 截取:取交集