翻页
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/graphql-go/graphql"
|
||||
"github.com/graphql-go/graphql/language/ast"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
func NewSchema(config Config) (graphql.Schema, error) {
|
||||
@@ -52,7 +53,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
},
|
||||
})
|
||||
|
||||
// 图像中的文字提取 [{"text": "角色选择", "confidence": 0.8484202027320862, "coordinate": [[666.0, 66.0], [908.0, 81.0], [903.0, 174.0], [661.0, 160.0]]}
|
||||
// 图像中的文字提取
|
||||
text := graphql.NewObject(graphql.ObjectConfig{
|
||||
Name: "Text",
|
||||
Fields: graphql.Fields{
|
||||
@@ -172,26 +173,38 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
},
|
||||
}),
|
||||
Args: graphql.FieldConfigArgument{
|
||||
"id": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"width": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"height": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中指定ID的"},
|
||||
"width": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中指定宽度的"},
|
||||
"height": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中指定高度的"},
|
||||
"content": &graphql.ArgumentConfig{Type: graphql.String},
|
||||
"remark": &graphql.ArgumentConfig{Type: graphql.String},
|
||||
"description": &graphql.ArgumentConfig{Type: graphql.String},
|
||||
"tags": &graphql.ArgumentConfig{Type: graphql.String},
|
||||
"rank": &graphql.ArgumentConfig{Type: graphql.String},
|
||||
"text": &graphql.ArgumentConfig{Type: graphql.String}, // 查找图像中的文字
|
||||
"text": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定文字的"},
|
||||
"comment_num": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"praise_count": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"collect_count": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"article_id": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"user_id": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||
"article_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中属于指定文章ID的"},
|
||||
"user_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中属于指定用户ID的"},
|
||||
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||
"first": &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10}, // 翻页参数
|
||||
"after": &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"}, // 翻页参数
|
||||
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
|
||||
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
|
||||
"after": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
|
||||
"before": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
|
||||
},
|
||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||
// 定义参数结构体
|
||||
var args struct {
|
||||
First int
|
||||
Last int
|
||||
After int
|
||||
Before int
|
||||
Text string
|
||||
}
|
||||
mapstructure.Decode(p.Args, &args)
|
||||
|
||||
// 返回字段
|
||||
var fields []string
|
||||
requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
|
||||
@@ -220,8 +233,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
first := p.Args["first"]
|
||||
after := p.Args["after"]
|
||||
|
||||
fields_str := strings.Join(fields, ",")
|
||||
|
||||
// 参数到 SQL 格式字符串的映射
|
||||
@@ -252,10 +264,11 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
}
|
||||
|
||||
// 特殊处理 text 参数
|
||||
if p.Args["text"] != nil {
|
||||
id_list := models.ElasticsearchSearch(p.Args["text"].(string)).GetIDList()
|
||||
var id_list []string
|
||||
if args.Text != "" {
|
||||
fmt.Println("args:", args)
|
||||
id_list = models.ElasticsearchSearch(args.Text).GetIDList(args.First, args.Last, args.After, args.Before)
|
||||
id_list_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(id_list)), ","), "[]")
|
||||
fmt.Println("id_list_str:", id_list_str)
|
||||
if id_list_str == "" {
|
||||
return map[string]interface{}{
|
||||
"list": []Image{},
|
||||
@@ -263,14 +276,15 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
"total": 0,
|
||||
}, nil
|
||||
}
|
||||
where = append(where, fmt.Sprintf("id IN (%s)", id_list_str))
|
||||
where = append(where, fmt.Sprintf("id IN (%s) LIMIT %d", id_list_str, len(id_list)))
|
||||
}
|
||||
|
||||
where_str := strings.Join(where, " AND ")
|
||||
|
||||
// 执行查询
|
||||
var query strings.Builder
|
||||
query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %d OFFSET %s", fields_str, where_str, first, after))
|
||||
query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s", fields_str, where_str))
|
||||
fmt.Println("query:", query.String())
|
||||
|
||||
var images ImageList
|
||||
if err := connection.Select(&images, query.String()); err != nil {
|
||||
@@ -278,6 +292,11 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 按照 id_list 的顺序重新排序
|
||||
if len(id_list) > 0 {
|
||||
images.SortByIDList(id_list)
|
||||
}
|
||||
|
||||
// 获取用户信息(如果图像列表不为空且请求字段中包含user)
|
||||
if len(images) > 0 && strings.Contains(fields_str, "user") {
|
||||
user_ids_str := images.ToAllUserID().ToString()
|
||||
|
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -16,6 +17,20 @@ func (ids IDS) ToString() (str string) {
|
||||
|
||||
type ImageList []Image
|
||||
|
||||
// 按照ID排序
|
||||
func (image *ImageList) SortByIDList(id_list []string) {
|
||||
var sortedImageList ImageList
|
||||
for _, id := range id_list {
|
||||
id_number, _ := strconv.Atoi(id)
|
||||
for _, image := range *image {
|
||||
if image.ID == id_number {
|
||||
sortedImageList = append(sortedImageList, image)
|
||||
}
|
||||
}
|
||||
}
|
||||
*image = sortedImageList
|
||||
}
|
||||
|
||||
// 取到所有的文章ID, 去除重复
|
||||
func (images *ImageList) ToAllArticleID() (uniqueIds IDS) {
|
||||
article_ids := make(map[int]bool)
|
||||
|
Reference in New Issue
Block a user