diff --git a/api/game.go b/api/game.go index 5868566..234367f 100644 --- a/api/game.go +++ b/api/game.go @@ -27,6 +27,8 @@ type Game struct { Praise bool `json:"praise" gorm:"column:is_praise"` CollectCount int `json:"collect_count" gorm:"column:collect_num"` PraiseCount int `json:"praise_count" gorm:"column:praise"` + CommentCount int `json:"comment_count" gorm:"column:comment_num"` + TextCount int `json:"text_count" gorm:"column:text_count"` CreateTime time.Time `json:"create_time"` UpdateTime time.Time `json:"update_time"` } @@ -48,11 +50,7 @@ var gameType = graphql.NewObject(graphql.ObjectConfig{ "collect_count": &graphql.Field{Type: graphql.Int, Description: "收藏数"}, "praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞"}, "collect": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否收藏"}, - "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.(Game).ID).Where("text != ''").Count(&count).Error - return int(count), err - }}, + "text_count": &graphql.Field{Type: graphql.Int, Description: "文字数量"}, }, }) @@ -168,7 +166,7 @@ var GameItems = &graphql.Field{ // 字段选择 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 + var praise, praise_join, collect, collect_join, text_count 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) @@ -177,14 +175,17 @@ var GameItems = &graphql.Field{ 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) } + if funk.Contains(fields, "text_count") { + text_count = ",(SELECT COUNT(*) FROM web_images wi WHERE wi.article_id = web_article.id AND wi.text != '') AS text_count" + } sql = fmt.Sprintf(` WITH RankedArticles AS (%s) - SELECT * %s %s FROM web_article INNER JOIN( + SELECT * %s %s %s FROM web_article INNER JOIN( SELECT id, row_num FROM RankedArticles %s ) AS LimitedRanked ON LimitedRanked.id = web_article.id %s %s ORDER BY LimitedRanked.row_num ASC LIMIT %d - `, sql, praise, collect, cursor, praise_join, collect_join, limit) + `, sql, praise, collect, text_count, cursor, praise_join, collect_join, limit) fmt.Println(sql)