|
@@ -2,11 +2,14 @@ package api
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"spu3d/db/model"
|
|
|
"spu3d/db/repo"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
+ "infish.cn/comm"
|
|
|
)
|
|
|
|
|
|
// 这里需要nats获取通用用户信息,因为spu3d中功能聚合了lancher的功能
|
|
@@ -44,3 +47,127 @@ func isSysUser(roles []string) bool {
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
+
|
|
|
+type IteChildren func(n *comm.CategoryNode) []string
|
|
|
+type FindChild func(n *comm.CategoryNode, id string) []string
|
|
|
+
|
|
|
+func FindCategoryIds(c *comm.DbCategory, parents []string) ([]string, [][]string) {
|
|
|
+ out := []string{}
|
|
|
+ andOut := [][]string{}
|
|
|
+ var allChildren IteChildren
|
|
|
+ allChildren = func(n *comm.CategoryNode) []string {
|
|
|
+ ids := []string{}
|
|
|
+ if n == nil {
|
|
|
+ return ids
|
|
|
+ }
|
|
|
+ ids = append(ids, n.Id)
|
|
|
+ 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 *comm.CategoryNode, id string) []string {
|
|
|
+ ids := []string{}
|
|
|
+ if n == nil {
|
|
|
+ return ids
|
|
|
+ }
|
|
|
+
|
|
|
+ if n.Id == 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.Categories {
|
|
|
+ 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{}, repoSession *repo.RepoSession, cateConf *comm.DbCategory) 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{"name": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
+ keyfilters = append(keyfilters, bson.M{"cusNum": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
+ filters = append(filters, bson.M{"$or": keyfilters})
|
|
|
+ }
|
|
|
+ query["keyword"] = nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if query["categories"] != nil && cateConf != nil {
|
|
|
+
|
|
|
+ categories := query["categories"].([]interface{})
|
|
|
+
|
|
|
+ //查询所有子内容
|
|
|
+ if len(categories) > 0 {
|
|
|
+
|
|
|
+ filterCaties := []string{}
|
|
|
+
|
|
|
+ for _, c := range categories {
|
|
|
+ filterCaties = append(filterCaties, c.(string))
|
|
|
+ }
|
|
|
+ oldextends, andExtends := FindCategoryIds(cateConf, filterCaties)
|
|
|
+ // extends, andExtends := FindCategoryIds(cateConf, filterCaties)
|
|
|
+ // if len(extends) > 0 {
|
|
|
+ // filter := bson.M{"categories": bson.M{"$elemMatch": bson.M{"$in": extends}}}
|
|
|
+ // filters = append(filters, filter)
|
|
|
+ // }
|
|
|
+ fmt.Println("old and :=============================================")
|
|
|
+ fmt.Println(oldextends)
|
|
|
+ fmt.Println(andExtends)
|
|
|
+ // todo待测试
|
|
|
+ // !多个分类筛选,包含在所有分类中
|
|
|
+ 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})
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // query["categories"] = nil
|
|
|
+ delete(query, "categories")
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(filters) > 0 {
|
|
|
+ query["$and"] = filters
|
|
|
+ }
|
|
|
+ fmt.Println("分类条件:==========================================>")
|
|
|
+ fmt.Printf("%#v\n", query)
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|