窗口分页
This commit is contained in:
134
api/graphql.go
134
api/graphql.go
@@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.satori.love/gameui/webp/models"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
|
||||
"github.com/graphql-go/graphql"
|
||||
"github.com/graphql-go/graphql/language/ast"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -396,7 +398,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
Args: graphql.FieldConfigArgument{
|
||||
"era": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中指定上线年份的"},
|
||||
"device": &graphql.ArgumentConfig{Type: graphql.String, Description: "游戏设备"},
|
||||
"sort": &graphql.ArgumentConfig{Type: graphql.String, Description: "排序方法"},
|
||||
"sort": &graphql.ArgumentConfig{Type: graphql.String, Description: "排序方法", DefaultValue: "id"},
|
||||
"order": &graphql.ArgumentConfig{Type: orderType, Description: "排序方向", DefaultValue: "ASC"},
|
||||
"orientation": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选横屏竖屏图像"},
|
||||
"follower": &graphql.ArgumentConfig{Type: graphql.Int, Description: "获取指定ID用户的关注列表发布的图像"},
|
||||
@@ -441,16 +443,18 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
mapstructure.Decode(p.Args, &args)
|
||||
|
||||
// 限制长度防止全表扫描
|
||||
var limit = 10
|
||||
if args.First != 0 {
|
||||
limit = args.First
|
||||
} else if args.Last != 0 {
|
||||
limit = args.Last
|
||||
}
|
||||
//var limit = 10
|
||||
//if args.First != 0 {
|
||||
// limit = args.First
|
||||
//} else if args.Last != 0 {
|
||||
// limit = args.Last
|
||||
//}
|
||||
|
||||
var total int
|
||||
var images []Image
|
||||
var query = db.Limit(limit)
|
||||
//var query = db.Limit(limit).Table("web_images")
|
||||
//order := "ORDER BY " + p.Args["sort"].(string) + " " + p.Args["order"].(string)
|
||||
var query = goqu.Dialect("mysql").Select("id", goqu.L("ROW_NUMBER() OVER(ORDER BY id ASC)").As("row_num")).From("web_images")
|
||||
|
||||
// 参数映射
|
||||
var argFormat = []string{"id", "width", "height", "content", "remark", "description", "tags", "rank", "comment_num", "praise_count", "collect_count", "article_id", "user_id"}
|
||||
@@ -458,7 +462,8 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
// 筛选条件
|
||||
for _, format := range argFormat {
|
||||
if p.Args[format] != nil {
|
||||
query = query.Where(fmt.Sprintf("web_images.%s = ?", format), p.Args[format])
|
||||
//query = query.Where(fmt.Sprintf("web_images.%s = ?", format), p.Args[format])
|
||||
query.Where(goqu.Ex{fmt.Sprintf("web_images.%s = ?", format): p.Args[format]})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -503,7 +508,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
return map[string]interface{}{"list": []Image{}, "total": 0}, nil
|
||||
}
|
||||
|
||||
query = query.Where("web_images.id IN (?)", item)
|
||||
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}})
|
||||
}
|
||||
|
||||
// 筛选:相似图像
|
||||
@@ -524,36 +529,16 @@ 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)
|
||||
//}
|
||||
|
||||
// 筛选:时间段
|
||||
applyTimeCondition := func(name string, str string) {
|
||||
parts := strings.Split(str, "-")
|
||||
query = query.Where(fmt.Sprintf("YEAR(%s) = ?", name), parts[0])
|
||||
query = query.Where(goqu.Ex{fmt.Sprintf("YEAR(%s)", name): parts[0]})
|
||||
|
||||
if len(parts) > 1 {
|
||||
query = query.Where(fmt.Sprintf("MONTH(%s) = ?", name), parts[1])
|
||||
query = query.Where(goqu.Ex{fmt.Sprintf("MONTH(%s)", name): parts[1]})
|
||||
}
|
||||
if len(parts) > 2 {
|
||||
query = query.Where(fmt.Sprintf("DAY(%s) = ?", name), parts[2])
|
||||
query = query.Where(goqu.Ex{fmt.Sprintf("DAY(%s)", name): parts[2]})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -569,22 +554,31 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
|
||||
// 数据库中筛选:横屏纵屏
|
||||
if p.Args["orientation"] != nil {
|
||||
query.Where("article_orientation = ?", p.Args["orientation"])
|
||||
query = query.Where(goqu.Ex{"article_orientation": p.Args["orientation"]})
|
||||
}
|
||||
|
||||
// 数据库中筛选:游戏设备
|
||||
if p.Args["device"] != nil {
|
||||
query = query.Joins("JOIN web_article ON web_images.article_id = web_article.id AND web_article.device = ?", p.Args["device"])
|
||||
query = query.Join(goqu.T("web_article"), goqu.On(
|
||||
goqu.I("web_images.article_id").Eq(goqu.I("web_article.id")),
|
||||
goqu.I("web_article.device").Eq(p.Args["device"]),
|
||||
))
|
||||
}
|
||||
|
||||
// 数据库中筛选:游戏上线年份
|
||||
if p.Args["era"] != nil {
|
||||
query = query.Joins("JOIN web_article ON web_images.article_id = web_article.id AND web_article.era = ?", p.Args["era"])
|
||||
query = query.Join(goqu.T("web_article"), goqu.On(
|
||||
goqu.I("web_images.article_id").Eq(goqu.I("web_article.id")),
|
||||
goqu.I("web_article.era").Eq(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"])
|
||||
query = query.Join(goqu.T("web_fans"), goqu.On(
|
||||
goqu.I("web_images.user_id").Eq(goqu.I("web_fans.blogger_id")),
|
||||
goqu.I("web_fans.follower_id").Eq(p.Args["follower"]),
|
||||
))
|
||||
}
|
||||
|
||||
// 排序
|
||||
@@ -604,10 +598,10 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
|
||||
total = len(list)
|
||||
|
||||
var count int64
|
||||
if len(list) > 0 {
|
||||
query = query.Count(&count)
|
||||
}
|
||||
//var count int64
|
||||
//if len(list) > 0 {
|
||||
// query = query.Count(&count)
|
||||
//}
|
||||
|
||||
// 截取: 分页
|
||||
if args.After != 0 {
|
||||
@@ -654,29 +648,61 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
|
||||
// 存在外部筛选条件
|
||||
if len(id_list) > 0 && len(list) > 0 {
|
||||
query = query.Where("id IN ?", list)
|
||||
//query = query.Where("id IN ?", list)
|
||||
//where = fmt.Sprintf("web_images.id IN ?", )
|
||||
}
|
||||
|
||||
for index, item := range LoadItem(p.Info.FieldASTs[0].SelectionSet.Selections) {
|
||||
fmt.Println(index, item)
|
||||
query = query.Preload(item)
|
||||
// 取所有数据的前N条
|
||||
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()
|
||||
|
||||
// 取所有数据的前N条
|
||||
var cursor string
|
||||
if args.After != 0 {
|
||||
cursor = fmt.Sprintf(`WHERE row_num > (SELECT row_num FROM RankedArticles WHERE id = %d)`, args.After)
|
||||
}
|
||||
|
||||
// 排序规则
|
||||
if args.Sort != "" && args.Order != "" {
|
||||
query = query.Order(fmt.Sprintf("%s %s", args.Sort, args.Order))
|
||||
}
|
||||
// 取大于游标的数据
|
||||
sql = fmt.Sprintf(`
|
||||
WITH RankedArticles AS (%s)
|
||||
SELECT web_images.* FROM web_images JOIN(
|
||||
SELECT id FROM RankedArticles %s ORDER BY row_num LIMIT 5
|
||||
) AS LimitedRanked ON web_images.id = LimitedRanked.id;
|
||||
`, sql, cursor)
|
||||
|
||||
// 输出(游戏截图分类ID为22)
|
||||
if err := query.Where("article_category_top_id = 22").Find(&images).Error; err != nil {
|
||||
fmt.Println(sql)
|
||||
|
||||
if err := db.Raw(sql).Scan(&images).Error; err != nil {
|
||||
fmt.Println("获取图像列表失败", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if total == 0 && count > 0 {
|
||||
total = int(count)
|
||||
var ids []int
|
||||
for _, item := range images {
|
||||
ids = append(ids, item.ID)
|
||||
}
|
||||
|
||||
var find = db.Where("web_images.id IN ?", ids)
|
||||
for index, item := range LoadItem(p.Info.FieldASTs[0].SelectionSet.Selections) {
|
||||
fmt.Println(index, item)
|
||||
find = find.Preload(item)
|
||||
}
|
||||
|
||||
if err := find.Find(&images).Error; err != nil {
|
||||
fmt.Println("获取图像列表失败", err)
|
||||
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,
|
||||
|
Reference in New Issue
Block a user