game 合并查询
This commit is contained in:
@@ -70,11 +70,6 @@ var articleType = graphql.NewObject(graphql.ObjectConfig{
|
|||||||
}
|
}
|
||||||
return false, nil
|
return false, nil
|
||||||
}},
|
}},
|
||||||
"text_count": &graphql.Field{Type: graphql.Int, Description: "文字数量", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
||||||
var count int64
|
|
||||||
err := db.Table("web_images").Where("article_id = ?", p.Source.(Article).ID).Where("text != ''").Count(&count).Error
|
|
||||||
return int(count), err
|
|
||||||
}},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
78
api/game.go
78
api/game.go
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/doug-martin/goqu/v9"
|
"github.com/doug-martin/goqu/v9"
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
|
"github.com/thoas/go-funk"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
@@ -22,8 +23,10 @@ type Game struct {
|
|||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
Images string `json:"images"`
|
Images string `json:"images"`
|
||||||
User User `json:"user" gorm:"foreignKey:UserId"`
|
User User `json:"user" gorm:"foreignKey:UserId"`
|
||||||
|
Collect bool `json:"collect" gorm:"column:is_collect"`
|
||||||
|
Praise bool `json:"praise" gorm:"column:is_praise"`
|
||||||
CollectCount int `json:"collect_count" gorm:"column:collect_num"`
|
CollectCount int `json:"collect_count" gorm:"column:collect_num"`
|
||||||
PraiseCount int `json:"praise_count" gorm:"column:praise_num"`
|
PraiseCount int `json:"praise_count" gorm:"column:praise"`
|
||||||
CreateTime time.Time `json:"create_time"`
|
CreateTime time.Time `json:"create_time"`
|
||||||
UpdateTime time.Time `json:"update_time"`
|
UpdateTime time.Time `json:"update_time"`
|
||||||
}
|
}
|
||||||
@@ -43,31 +46,12 @@ var gameType = graphql.NewObject(graphql.ObjectConfig{
|
|||||||
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "游戏更新时间"},
|
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "游戏更新时间"},
|
||||||
"praise_count": &graphql.Field{Type: graphql.Int, Description: "点赞数"},
|
"praise_count": &graphql.Field{Type: graphql.Int, Description: "点赞数"},
|
||||||
"collect_count": &graphql.Field{Type: graphql.Int, Description: "收藏数"},
|
"collect_count": &graphql.Field{Type: graphql.Int, Description: "收藏数"},
|
||||||
"praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
"praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞"},
|
||||||
var user_id = p.Context.Value("user_id").(int)
|
"collect": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否收藏"},
|
||||||
if user_id != 0 {
|
"text_count": &graphql.Field{Type: graphql.Int, Description: "文字数量", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
var praise int64
|
var count int64
|
||||||
if err := db.Table("web_praise").Where("user_id = ?", user_id).Where("praise_id = ?", p.Source.(Game).ID).Where("type = ?", 0).Count(&praise); err != nil {
|
err := db.Table("web_images").Where("article_id = ?", p.Source.(Game).ID).Where("text != ''").Count(&count).Error
|
||||||
return false, nil
|
return int(count), err
|
||||||
}
|
|
||||||
if praise > 0 {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}},
|
|
||||||
"collect": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否收藏", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
||||||
var user_id = p.Context.Value("user_id").(int)
|
|
||||||
if user_id != 0 {
|
|
||||||
var collect int64
|
|
||||||
if err := db.Table("web_collect").Where("user_id = ?", user_id).Where("collect_id = ?", p.Source.(Game).ID).Where("type = ?", 0).Count(&collect); err != nil {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if collect > 0 {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -84,6 +68,8 @@ var GameItems = &graphql.Field{
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
Args: graphql.FieldConfigArgument{
|
Args: graphql.FieldConfigArgument{
|
||||||
|
"collect": &graphql.ArgumentConfig{Type: graphql.Int, Description: "按指定用户收藏筛选"},
|
||||||
|
"praise": &graphql.ArgumentConfig{Type: graphql.Int, Description: "按指定用户点赞筛选"},
|
||||||
"style": &graphql.ArgumentConfig{Type: graphql.String, Description: "按风格筛选游戏"},
|
"style": &graphql.ArgumentConfig{Type: graphql.String, Description: "按风格筛选游戏"},
|
||||||
"device": &graphql.ArgumentConfig{Type: graphql.String, Description: "按平台筛选游戏"},
|
"device": &graphql.ArgumentConfig{Type: graphql.String, Description: "按平台筛选游戏"},
|
||||||
"orientation": &graphql.ArgumentConfig{Type: graphql.String, Description: "按屏幕方向筛选游戏"},
|
"orientation": &graphql.ArgumentConfig{Type: graphql.String, Description: "按屏幕方向筛选游戏"},
|
||||||
@@ -118,6 +104,24 @@ var GameItems = &graphql.Field{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按指定用户点赞筛选
|
||||||
|
if p.Args["praise"] != nil {
|
||||||
|
query = query.Join(goqu.T("web_praise"), goqu.On(
|
||||||
|
goqu.I("web_article.id").Eq(goqu.I("web_praise.praise_id")),
|
||||||
|
goqu.I("web_praise.user_id").Eq(p.Args["praise"]),
|
||||||
|
goqu.I("web_praise.type").Eq(0),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按指定用户收藏筛选
|
||||||
|
if p.Args["collect"] != nil {
|
||||||
|
query = query.Join(goqu.T("web_collect"), goqu.On(
|
||||||
|
goqu.I("web_article.id").Eq(goqu.I("web_collect.collect_id")),
|
||||||
|
goqu.I("web_collect.user_id").Eq(p.Args["collect"]),
|
||||||
|
goqu.I("web_collect.type").Eq(0),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
// 如果查询了 total 字段
|
// 如果查询了 total 字段
|
||||||
if existField(p.Info.FieldASTs[0].SelectionSet.Selections, "total") {
|
if existField(p.Info.FieldASTs[0].SelectionSet.Selections, "total") {
|
||||||
sql, _, _ := query.ToSQL()
|
sql, _, _ := query.ToSQL()
|
||||||
@@ -147,7 +151,6 @@ var GameItems = &graphql.Field{
|
|||||||
|
|
||||||
// 取所有数据的前N条
|
// 取所有数据的前N条
|
||||||
sql, _, _ := query.ToSQL()
|
sql, _, _ := query.ToSQL()
|
||||||
fmt.Println(sql)
|
|
||||||
|
|
||||||
// 遊標截取篩選結果集的前N条
|
// 遊標截取篩選結果集的前N条
|
||||||
var cursor string
|
var cursor string
|
||||||
@@ -162,13 +165,28 @@ var GameItems = &graphql.Field{
|
|||||||
limit = p.Args["last"].(int)
|
limit = p.Args["last"].(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 字段选择
|
||||||
|
var user_id = p.Context.Value("user_id").(int)
|
||||||
|
var fields = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
|
||||||
|
var praise, praise_join, collect, collect_join string
|
||||||
|
if funk.Contains(fields, "praise") {
|
||||||
|
praise = ",CASE WHEN web_praise.id IS NOT NULL THEN TRUE ELSE FALSE END AS is_praise"
|
||||||
|
praise_join = fmt.Sprintf("LEFT JOIN web_praise ON web_praise.praise_id = web_article.id AND web_praise.user_id = %d AND web_praise.type = 0", user_id)
|
||||||
|
}
|
||||||
|
if funk.Contains(fields, "collect") {
|
||||||
|
collect = ",CASE WHEN web_collect.id IS NOT NULL THEN TRUE ELSE FALSE END AS is_collect"
|
||||||
|
collect_join = fmt.Sprintf("LEFT JOIN web_collect ON web_collect.collect_id = web_article.id AND web_collect.user_id = %d AND web_collect.type = 0", user_id)
|
||||||
|
}
|
||||||
|
|
||||||
sql = fmt.Sprintf(`
|
sql = fmt.Sprintf(`
|
||||||
WITH RankedArticles AS (%s)
|
WITH RankedArticles AS (%s)
|
||||||
SELECT * FROM web_article INNER JOIN(
|
SELECT * %s %s FROM web_article INNER JOIN(
|
||||||
SELECT id, row_num FROM RankedArticles %s
|
SELECT id, row_num FROM RankedArticles %s
|
||||||
) AS LimitedRanked ON LimitedRanked.id = web_article.id
|
) AS LimitedRanked ON LimitedRanked.id = web_article.id %s %s
|
||||||
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
||||||
`, sql, cursor, limit)
|
`, sql, praise, collect, cursor, praise_join, collect_join, limit)
|
||||||
|
|
||||||
|
fmt.Println(sql)
|
||||||
|
|
||||||
if err := db.Raw(sql).Scan(&games).Error; err != nil {
|
if err := db.Raw(sql).Scan(&games).Error; err != nil {
|
||||||
fmt.Println("获取游戏列表失败", err)
|
fmt.Println("获取游戏列表失败", err)
|
||||||
|
@@ -129,8 +129,8 @@ func ListItem(requestedFields []ast.Selection) (data []string) {
|
|||||||
for _, field := range requestedFields {
|
for _, field := range requestedFields {
|
||||||
fieldAST, _ := field.(*ast.Field)
|
fieldAST, _ := field.(*ast.Field)
|
||||||
if fieldAST.Name.Value == "list" {
|
if fieldAST.Name.Value == "list" {
|
||||||
for _, str := range LoadItem(fieldAST.SelectionSet.Selections) {
|
for _, field := range fieldAST.SelectionSet.Selections {
|
||||||
data = append(data, str)
|
data = append(data, field.(*ast.Field).Name.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user