防止重复

This commit is contained in:
2024-12-28 19:43:37 +08:00
parent 4e95885a4c
commit 1c86dfb107
5 changed files with 21 additions and 73 deletions

View File

@@ -191,31 +191,21 @@ 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, text_count string
var praise, collect, 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)
praise_join = fmt.Sprintf("LEFT JOIN (SELECT DISTINCT praise_id FROM web_praise WHERE user_id = %d AND type = 0) AS web_praise ON web_praise.praise_id = web_article.id", user_id)
praise = fmt.Sprintf(",CASE WHEN EXISTS (SELECT 1 FROM web_praise WHERE web_praise.praise_id = web_article.id AND web_praise.user_id = %d AND web_praise.type = 0) THEN TRUE ELSE FALSE END AS is_praise", 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)
collect_join = fmt.Sprintf(`
LEFT JOIN (
SELECT DISTINCT collect_id
FROM web_collect
WHERE user_id = %d AND type = 0
) AS web_collect ON web_collect.collect_id = web_article.id
`, user_id)
collect = fmt.Sprintf(",CASE WHEN EXISTS (SELECT 1 FROM web_collect WHERE web_collect.collect_id = web_article.id AND web_collect.user_id = %d AND web_collect.type = 0) THEN TRUE ELSE FALSE END AS is_collect", user_id)
}
sql = fmt.Sprintf(`
WITH RankedArticles AS (%s)
SELECT * %s %s %s FROM web_article INNER JOIN(
SELECT web_article.* %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
) AS LimitedRanked ON LimitedRanked.id = web_article.id
ORDER BY LimitedRanked.row_num ASC LIMIT %d
`, sql, praise, collect, text_count, cursor, praise_join, collect_join, limit)
`, sql, praise, collect, text_count, cursor, limit)
fmt.Println(sql)