|
@@ -2,6 +2,7 @@ package api
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
"sku3dweb/db/model"
|
|
"sku3dweb/db/model"
|
|
"sku3dweb/db/repo"
|
|
"sku3dweb/db/repo"
|
|
"sku3dweb/log"
|
|
"sku3dweb/log"
|
|
@@ -73,6 +74,16 @@ func DeleteImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func SearchByFields(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func SearchByFields(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
page, size, query := UtilQueryPageSize(c)
|
|
page, size, query := UtilQueryPageSize(c)
|
|
|
|
|
|
|
|
+ // 查询所有分类
|
|
|
|
+ // confCate := &model.Category{}
|
|
|
|
+ // cates := []*model.Category{}
|
|
|
|
+ // repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{CollectName: repo.CollectionCategory}, &cates)
|
|
|
|
+ // confCate.Children = cates
|
|
|
|
+ // err := parseCategories(query, confCate)
|
|
|
|
+ // if err != nil {
|
|
|
|
+ // return nil, err
|
|
|
|
+ // }
|
|
|
|
+
|
|
pageOption := &repo.PageSearchOptions{
|
|
pageOption := &repo.PageSearchOptions{
|
|
CollectName: repo.CollectionMatImages,
|
|
CollectName: repo.CollectionMatImages,
|
|
Page: page,
|
|
Page: page,
|
|
@@ -151,3 +162,122 @@ func SearchByImg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
}
|
|
}
|
|
return out, nil
|
|
return out, nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+type IteChildren func(n *model.Category) []string
|
|
|
|
+type FindChild func(n *model.Category, id string) []string
|
|
|
|
+
|
|
|
|
+func FindCategoryIds(c *model.Category, parents []string) ([]string, [][]string) {
|
|
|
|
+ out := []string{}
|
|
|
|
+ andOut := [][]string{}
|
|
|
|
+ var allChildren IteChildren
|
|
|
|
+ allChildren = func(n *model.Category) []string {
|
|
|
|
+ ids := []string{}
|
|
|
|
+ if n == nil {
|
|
|
|
+ return ids
|
|
|
|
+ }
|
|
|
|
+ ids = append(ids, n.Id.Hex())
|
|
|
|
+ if n.Children != nil {
|
|
|
|
+ for _, c := range n.Children {
|
|
|
|
+ cids := allChildren(c)
|
|
|
|
+ if len(cids) > 0 {
|
|
|
|
+ ids = append(ids, cids...)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ids
|
|
|
|
+ }
|
|
|
|
+ var findChildrens FindChild
|
|
|
|
+ findChildrens = func(n *model.Category, id string) []string {
|
|
|
|
+ ids := []string{}
|
|
|
|
+ if n == nil {
|
|
|
|
+ return ids
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if n.Id.Hex() == id {
|
|
|
|
+ ids = allChildren(n)
|
|
|
|
+ return ids
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if n.Children == nil {
|
|
|
|
+ return ids
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //查找孩子节点
|
|
|
|
+ for _, c := range n.Children {
|
|
|
|
+ ids = findChildrens(c, id)
|
|
|
|
+ if len(ids) > 0 {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ids
|
|
|
|
+ }
|
|
|
|
+ for _, v := range parents {
|
|
|
|
+ for _, catnode := range c.Children {
|
|
|
|
+ extends := findChildrens(catnode, v)
|
|
|
|
+ if len(extends) > 0 {
|
|
|
|
+ andOut = append(andOut, extends)
|
|
|
|
+ out = append(out, extends...)
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return out, andOut
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func parseCategories(query map[string]interface{}, cateConf *model.Category) error {
|
|
|
|
+
|
|
|
|
+ filters := []bson.M{}
|
|
|
|
+ keyfilters := []bson.M{}
|
|
|
|
+ //keyword
|
|
|
|
+ if query["keyword"] != nil {
|
|
|
|
+ keyword := query["keyword"].(string)
|
|
|
|
+ if len(keyword) > 0 {
|
|
|
|
+ keyfilters = append(keyfilters, bson.M{"nameCn": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
|
+ keyfilters = append(keyfilters, bson.M{"nameEn": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
|
+ keyfilters = append(keyfilters, bson.M{"from": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
|
+ filters = append(filters, bson.M{"$or": keyfilters})
|
|
|
|
+ }
|
|
|
|
+ delete(query, "keyword")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if query["categories"] != nil {
|
|
|
|
+ categories := query["categories"].([]interface{})
|
|
|
|
+ delete(query, "categories")
|
|
|
|
+
|
|
|
|
+ //查询所有子内容
|
|
|
|
+ if len(categories) > 0 {
|
|
|
|
+ filterCaties := []string{}
|
|
|
|
+ for _, c := range categories {
|
|
|
|
+ if c != nil {
|
|
|
|
+ if str, ok := c.(string); ok {
|
|
|
|
+ filterCaties = append(filterCaties, str)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ oldextends, andExtends := FindCategoryIds(cateConf, filterCaties)
|
|
|
|
+ fmt.Println("old =>: ", oldextends)
|
|
|
|
+ fmt.Println("and =>: ", andExtends)
|
|
|
|
+
|
|
|
|
+ andArrayCons := []bson.M{}
|
|
|
|
+ if len(andExtends) > 0 {
|
|
|
|
+ for _, cateCon := range andExtends {
|
|
|
|
+ if len(cateCon) > 0 {
|
|
|
|
+ andArrayCons = append(andArrayCons, bson.M{"categories": bson.M{"$elemMatch": bson.M{"$in": cateCon}}})
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if len(andArrayCons) > 0 {
|
|
|
|
+ filters = append(filters, bson.M{"$and": andArrayCons})
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(filters) > 0 {
|
|
|
|
+ query["$and"] = filters
|
|
|
|
+ }
|
|
|
|
+ fmt.Printf("分类条件 =>: %#v\n", query)
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+}
|