funeralplay 8 months ago
parent
commit
a38154c42d
7 changed files with 270 additions and 19 deletions
  1. 1 1
      comm
  2. 98 0
      src/api/boxLib.go
  3. 22 18
      src/api/category.go
  4. 1 0
      src/api/router.go
  5. 127 0
      src/api/utils.go
  6. 20 0
      src/db/model/boxLib.go
  7. 1 0
      src/db/repo/repo.go

+ 1 - 1
comm

@@ -1 +1 @@
-Subproject commit af0feb8cf4b07240a151ac45dde549a75627d6bd
+Subproject commit b9a8fc0691a9f991c2b168727a74c1b9331e6ae8

+ 98 - 0
src/api/boxLib.go

@@ -0,0 +1,98 @@
+package api
+
+import (
+	"errors"
+	"spu3d/db/model"
+	"spu3d/db/repo"
+	"spu3d/log"
+	"time"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+func BoxLib(r *GinRouter) {
+	r.POST("/boxlib/create", CreateBoxLib)
+	r.POST("/boxlib/delete/:id", DeleteBoxLib)
+	r.GET("/boxlib/list", BoxLibList)
+	r.GET("/boxlib/detail/:id", BoxLibDetail)
+	r.POST("/boxlib/update", UpdateBoxLib)
+}
+
+func CreateBoxLib(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var box model.BoxLib
+	err := c.ShouldBindJSON(&box)
+	if err != nil {
+		log.Error(err)
+		return nil, err
+	}
+	box.CreateTime = time.Now()
+	box.UpdateTime = time.Now()
+	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionBoxLib, &box)
+}
+
+func DeleteBoxLib(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_id := c.Param("id")
+	id, _ := primitive.ObjectIDFromHex(_id)
+	if id.IsZero() {
+		return nil, errors.New("id错误")
+	}
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBoxLib, _id)
+}
+
+func BoxLibList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+	catsConf, err := GetSpu3dBoxLibCategory(apictx)
+	if err != nil {
+		return nil, err
+	}
+	err = ParseCategories(query, apictx.CreateRepoCtx(), catsConf)
+	if err != nil {
+		return nil, err
+	}
+
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionBoxLib,
+		Page:        page,
+		Size:        size,
+		Query:       query,
+		Sort:        bson.M{"createTime": -1},
+	})
+}
+
+func BoxLibDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_id := c.Param("id")
+	id, _ := primitive.ObjectIDFromHex(_id)
+	if id.IsZero() {
+		return nil, errors.New("id错误")
+	}
+	box := &model.BoxLib{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionBoxLib,
+		Query:       repo.Map{"_id": id},
+	}, box)
+	if err != nil {
+		log.Error(err)
+		return nil, err
+	}
+
+	if !found {
+		return nil, errors.New("未找到该数据")
+	}
+	return box, nil
+}
+
+func UpdateBoxLib(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var box model.BoxLib
+	err := c.ShouldBindJSON(&box)
+	if err != nil {
+		log.Error(err)
+		return nil, err
+	}
+	if box.Id.IsZero() {
+		return nil, errors.New("id错误")
+	}
+	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBoxLib, box.Id.Hex(), &box)
+}

+ 22 - 18
src/api/category.go

@@ -10,39 +10,44 @@ import (
 	"infish.cn/comm"
 )
 
-// 64d598e3ccc9b7d39470c95a
-// const SCOPE = "queenshow"
-const SPU3D_SCOPE = "spu3d"
+const BOXLIB_SCOPE = "boxlib"
+
+func GetSpu3dBoxLibCategory(apictx *ApiSession) (*comm.DbCategory, error) {
+	curr := &comm.DbCategory{}
+	ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionCategories,
+		Query:       repo.Map{"scope": BOXLIB_SCOPE},
+	}, curr)
+	if ok {
+		return curr, nil
+	}
+	return nil, err
+}
 
 func CreateCategoryRouter(router *GinRouter) {
-	router.GETJWT("/spu3d/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	router.GET("/spu3d/boxlib/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		_, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
 			CollectName: repo.CollectionCategories,
-			Query:       repo.Map{"scope": SPU3D_SCOPE},
+			Query:       repo.Map{"scope": BOXLIB_SCOPE},
 		})
-		// 没找到数据是创建初始分类
-		// if !ok {
-		// 	return nil, errors.New("未找到数据!")
-		// }
 		return ret, nil
 	})
 
 	// 创建/更新 spu3d资源分类
-	router.POSTJWT("/spu3d/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-		roles, _ := getUserRole(c, apictx)
-		// 公共分类 只有管理员能创建该分类
-		if !isSysUser(roles) {
-			return nil, errors.New("没有权限!")
-
-		}
+	router.POST("/spu3d/boxlib/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+		// roles, _ := getUserRole(c, apictx)
+		// // 公共分类 只有管理员能创建该分类
+		// if !isSysUser(roles) {
+		// 	return nil, errors.New("没有权限!")
 
+		// }
 		body := &comm.DbCategory{}
 		err := c.ShouldBindJSON(body)
 		if err != nil {
 			PrintErr(err, "参数错误")
 			return nil, errors.New("参数错误!")
 		}
-		body.Scope = SPU3D_SCOPE
+		body.Scope = BOXLIB_SCOPE
 		body.CreateTime = time.Now()
 		if body.Id.IsZero() {
 			body.CreateTime = time.Now()
@@ -65,7 +70,6 @@ func CreateCategoryRouter(router *GinRouter) {
 		if curr.Id.IsZero() {
 			return nil, errors.New("id 错误!")
 		}
-
 		body.Id = primitive.NilObjectID
 		return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, curr.Id.Hex(), body)
 	})

+ 1 - 0
src/api/router.go

@@ -33,6 +33,7 @@ func RegRouters(svc *Service) {
 
 	CreateCategoryRouter(r)
 	Asset(r)
+	BoxLib(r)
 
 	r.GET("/printr", Printr)
 }

+ 127 - 0
src/api/utils.go

@@ -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
+}

+ 20 - 0
src/db/model/boxLib.go

@@ -0,0 +1,20 @@
+package model
+
+import (
+	"time"
+
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+// 画廊
+type BoxLib struct {
+	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	Name       string             `bson:"name,omitempty" json:"name"`
+	CusNum     string             `bson:"cusNum,omitempty" json:"cusNum"`
+	Cover      string             `bson:"cover,omitempty" json:"cover"`
+	Order      int32              `bson:"order,omitempty" json:"order"`
+	DotQueen   string             `bson:"dotqueen,omitempty" json:"dotqueen"`
+	Categories []string           `bson:"categories,omitempty" json:"categories"`
+	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+}

+ 1 - 0
src/db/repo/repo.go

@@ -21,6 +21,7 @@ const (
 	CollectionCategories = "categories"
 	CollectionNav        = "nav"
 	CollectionGallery    = "gallery"
+	CollectionBoxLib     = "boxlib"
 )
 
 type Map map[string]interface{}