|
@@ -2,12 +2,10 @@ package api
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
- "fmt"
|
|
|
"spu3d/db/repo"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
- "go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
"infish.cn/comm"
|
|
|
)
|
|
@@ -162,109 +160,6 @@ var UserCategoryInitArray []*comm.CategoryNode = []*comm.CategoryNode{
|
|
|
}
|
|
|
|
|
|
func CreateCategoryRouter(router *GinRouter) {
|
|
|
-
|
|
|
- //创建分类定义
|
|
|
- router.POSTJWT("/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
|
|
|
- roles, _ := getUserRole(c, apictx)
|
|
|
- // 平台账号 公用分类
|
|
|
- if isSysUser(roles) {
|
|
|
- userId, _ = primitive.ObjectIDFromHex(SystemCategoryUserId)
|
|
|
- }
|
|
|
-
|
|
|
- body := &comm.DbCategory{}
|
|
|
- err := c.ShouldBindJSON(body)
|
|
|
- if err != nil {
|
|
|
- PrintErr(err, "参数错误")
|
|
|
- return nil, errors.New("参数错误!")
|
|
|
- }
|
|
|
- body.UserId = userId
|
|
|
- body.Scope = SCOPE
|
|
|
-
|
|
|
- body.CreateTime = time.Now()
|
|
|
- if body.Id.IsZero() {
|
|
|
- body.CreateTime = time.Now()
|
|
|
- } else {
|
|
|
- body.UpdateTime = time.Now()
|
|
|
- }
|
|
|
-
|
|
|
- curr := &comm.DbCategory{}
|
|
|
- ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionCategories,
|
|
|
- Project: []string{"_id"},
|
|
|
- Query: repo.Map{"userId": body.UserId, "scope": body.Scope},
|
|
|
- }, curr)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- if !ok {
|
|
|
- return nil, errors.New("没有分类数据!")
|
|
|
- }
|
|
|
-
|
|
|
- body.Id = primitive.NilObjectID
|
|
|
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, curr.Id.Hex(), body)
|
|
|
- })
|
|
|
-
|
|
|
- //获取用户的所有分类定义和 资产分类定义
|
|
|
- router.GETJWT("/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
|
|
|
- roles, _ := getUserRole(c, apictx)
|
|
|
- isSys := isSysUser(roles)
|
|
|
- // 平台账号 公用分类
|
|
|
- if isSys {
|
|
|
- userId, _ = primitive.ObjectIDFromHex(SystemCategoryUserId)
|
|
|
- }
|
|
|
- ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionCategories,
|
|
|
- Query: repo.Map{"userId": userId, "scope": SCOPE},
|
|
|
- })
|
|
|
-
|
|
|
- // 没找到数据是创建初始分类
|
|
|
- if !ok {
|
|
|
- var category *comm.DbCategory
|
|
|
- var cateNodes []*comm.CategoryNode
|
|
|
- if isSys {
|
|
|
- for _, cat := range SysCategoryInitArray {
|
|
|
- cat.Id = primitive.NewObjectID().Hex()
|
|
|
- cateNodes = append(cateNodes, cat)
|
|
|
- }
|
|
|
- } else {
|
|
|
- for _, cat := range UserCategoryInitArray {
|
|
|
- cat.Id = primitive.NewObjectID().Hex()
|
|
|
- cateNodes = append(cateNodes, cat)
|
|
|
- }
|
|
|
- }
|
|
|
- category = &comm.DbCategory{
|
|
|
- UserId: userId,
|
|
|
- Scope: SCOPE,
|
|
|
- CreateTime: time.Now(),
|
|
|
- UpdateTime: time.Now(),
|
|
|
- Categories: cateNodes,
|
|
|
- }
|
|
|
- id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, &category)
|
|
|
- if err != nil {
|
|
|
- return PrintErr(err, "分类初始化错误")
|
|
|
- }
|
|
|
- category.Id, _ = primitive.ObjectIDFromHex(id)
|
|
|
- return category, nil
|
|
|
- }
|
|
|
- return ret, nil
|
|
|
- })
|
|
|
-
|
|
|
- // 用户获取平台id
|
|
|
- router.GET("/sys/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
- userId, _ := primitive.ObjectIDFromHex(SystemCategoryUserId)
|
|
|
- ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionCategories,
|
|
|
- Query: repo.Map{"userId": userId, "scope": SCOPE},
|
|
|
- })
|
|
|
- // 没找到数据是创建初始分类
|
|
|
- if !ok {
|
|
|
- return nil, errors.New("未找到数据!")
|
|
|
- }
|
|
|
- return ret, nil
|
|
|
- })
|
|
|
- // 获取spu3d资源分类
|
|
|
router.GETJWT("/spu3d/category", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
_, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
CollectName: repo.CollectionCategories,
|
|
@@ -321,141 +216,3 @@ func CreateCategoryRouter(router *GinRouter) {
|
|
|
})
|
|
|
|
|
|
}
|
|
|
-
|
|
|
-type IteChildren func(n *comm.CategoryNode) []string
|
|
|
-type FindChild func(n *comm.CategoryNode, id string) []string
|
|
|
-
|
|
|
-func FindCategoryIds(c *comm.DbCategory, parents []string) []string {
|
|
|
- out := []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 {
|
|
|
- out = append(out, extends...)
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return out
|
|
|
-}
|
|
|
-
|
|
|
-func ParseCategories(query map[string]interface{}, repoSession *repo.RepoSession, userInfo *UserInfo) 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{"title": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
- keyfilters = append(keyfilters, bson.M{"desc": bson.M{"$regex": keyword, "$options": "$i"}})
|
|
|
- filters = append(filters, bson.M{"$or": keyfilters})
|
|
|
- }
|
|
|
- query["keyword"] = nil
|
|
|
- }
|
|
|
-
|
|
|
- // flag := false
|
|
|
- if query["categories"] != nil {
|
|
|
- // flag = true
|
|
|
- categories := query["categories"].([]interface{})
|
|
|
- //查询所有子内容
|
|
|
- if len(categories) > 0 {
|
|
|
- var cateConf *comm.DbCategory
|
|
|
- scope := userInfo.Scope
|
|
|
- if scope == SPU3D_SCOPE {
|
|
|
- ok, err := repo.RepoSeachDoc(repoSession, &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionCategories,
|
|
|
- Query: repo.Map{"scope": SPU3D_SCOPE},
|
|
|
- }, &cateConf)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if !ok {
|
|
|
- return errors.New("未找到分类数据!")
|
|
|
- }
|
|
|
- } else {
|
|
|
- userId := userInfo.Id
|
|
|
- // 如果是系统账号
|
|
|
- if userInfo.IsSystem {
|
|
|
- userId, _ = primitive.ObjectIDFromHex(SystemCategoryUserId)
|
|
|
- }
|
|
|
- ok, err := repo.RepoSeachDoc(repoSession, &repo.DocSearchOptions{
|
|
|
- CollectName: repo.CollectionCategories,
|
|
|
- Query: repo.Map{"userId": userId},
|
|
|
- }, &cateConf)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if !ok {
|
|
|
- return errors.New("未找到分类数据!")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- filterCaties := []string{}
|
|
|
-
|
|
|
- for _, c := range categories {
|
|
|
- filterCaties = append(filterCaties, c.(string))
|
|
|
- }
|
|
|
-
|
|
|
- extends := FindCategoryIds(cateConf, filterCaties)
|
|
|
- if len(extends) > 0 {
|
|
|
- filter := bson.M{"categories": bson.M{"$elemMatch": bson.M{"$in": extends}}}
|
|
|
- filters = append(filters, filter)
|
|
|
- }
|
|
|
- }
|
|
|
- query["categories"] = nil
|
|
|
- }
|
|
|
-
|
|
|
- if len(filters) > 0 {
|
|
|
- query["$and"] = filters
|
|
|
- }
|
|
|
- // 其他用户查询系统账号
|
|
|
- if userInfo.IsSystem && !userInfo.IsOwner {
|
|
|
- delete(query, "userId")
|
|
|
- }
|
|
|
-
|
|
|
- fmt.Printf("%#+v\n", query)
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|