排序规则
This commit is contained in:
103
api/graphql.go
103
api/graphql.go
@@ -456,9 +456,6 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var list []int
|
|
||||||
var id_list [][]int
|
|
||||||
|
|
||||||
// 筛选:提取文字
|
// 筛选:提取文字
|
||||||
if args.Text != "" {
|
if args.Text != "" {
|
||||||
resp, err := models.ZincSearch(map[string]interface{}{
|
resp, err := models.ZincSearch(map[string]interface{}{
|
||||||
@@ -506,11 +503,23 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
for _, id := range models.GetSimilarImagesIdList(args.Similar, 200) {
|
for _, id := range models.GetSimilarImagesIdList(args.Similar, 200) {
|
||||||
item = append(item, int(id))
|
item = append(item, int(id))
|
||||||
}
|
}
|
||||||
id_list = append(id_list, item)
|
|
||||||
|
|
||||||
if len(id_list) == 0 {
|
toGoquExpressions := func(ids []int) []goqu.Expression {
|
||||||
return map[string]interface{}{"list": []Image{}, "total": 0}, nil
|
expressions := make([]goqu.Expression, len(ids))
|
||||||
|
for i, id := range ids {
|
||||||
|
expressions[i] = goqu.V(id)
|
||||||
}
|
}
|
||||||
|
return expressions
|
||||||
|
}
|
||||||
|
|
||||||
|
expressions := toGoquExpressions(item)
|
||||||
|
interfaceExpressions := make([]interface{}, len(expressions))
|
||||||
|
for i, expr := range expressions {
|
||||||
|
interfaceExpressions[i] = expr
|
||||||
|
}
|
||||||
|
|
||||||
|
query = query.Where(goqu.Ex{"web_images.id": goqu.Op{"in": item}}).
|
||||||
|
Order(goqu.Func("FIELD", append([]interface{}{goqu.I("web_images.id")}, interfaceExpressions...)...).Asc())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 筛选:兴趣推荐
|
// 筛选:兴趣推荐
|
||||||
@@ -578,77 +587,6 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 排序
|
|
||||||
|
|
||||||
// 截取:取交集
|
|
||||||
if len(id_list) != 0 {
|
|
||||||
list = id_list[0]
|
|
||||||
if len(id_list) > 1 {
|
|
||||||
for _, slice := range id_list[1:] {
|
|
||||||
list = funk.Join(list, slice, funk.InnerJoin).([]int)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(list) == 0 {
|
|
||||||
return map[string]interface{}{"list": []Image{}, "total": 0}, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
total = len(list)
|
|
||||||
|
|
||||||
//var count int64
|
|
||||||
//if len(list) > 0 {
|
|
||||||
// query = query.Count(&count)
|
|
||||||
//}
|
|
||||||
|
|
||||||
// 截取: 分页
|
|
||||||
if args.After != 0 {
|
|
||||||
index := -1
|
|
||||||
for i, id := range list {
|
|
||||||
if id == args.After {
|
|
||||||
index = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if index != -1 {
|
|
||||||
list = list[index+1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Before != 0 {
|
|
||||||
index := -1
|
|
||||||
for i, id := range list {
|
|
||||||
if id == args.Before {
|
|
||||||
index = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if index != -1 {
|
|
||||||
list = list[:index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果截取范围小于列表长度
|
|
||||||
if args.First != 0 && args.First < len(list) {
|
|
||||||
list = list[:args.First]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果截取范围小于列表长度
|
|
||||||
if args.Last != 0 && args.Last < len(list) {
|
|
||||||
list = list[len(list)-args.Last:]
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.First == 0 && args.Last == 0 {
|
|
||||||
if len(list) > 10 {
|
|
||||||
list = list[:10]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 存在外部筛选条件
|
|
||||||
if len(id_list) > 0 && len(list) > 0 {
|
|
||||||
//query = query.Where("id IN ?", list)
|
|
||||||
//where = fmt.Sprintf("web_images.id IN ?", )
|
|
||||||
}
|
|
||||||
|
|
||||||
// 取所有数据的前N条
|
// 取所有数据的前N条
|
||||||
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()
|
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 22}).ToSQL()
|
||||||
|
|
||||||
@@ -696,17 +634,6 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
return nil, 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{}{
|
return map[string]interface{}{
|
||||||
"list": images,
|
"list": images,
|
||||||
"total": total,
|
"total": total,
|
||||||
|
Reference in New Issue
Block a user