package api import ( "assetcenter/db/repo" "errors" "fmt" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" "infish.cn/comm" ) func DbMaterialDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) { // 查询db数据库看是否存在queenter库 search := &comm.Database{} ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: repo.CollectionDatabase, Query: repo.Map{"name": fmt.Sprintf("%s-%s", "queenter", apictx.User.Parent)}}, search) fmt.Println(err) if err != nil { return nil, err } // 找到数据,返回material库信息 if ok { for _, v := range search.Assets { if v.Type == comm.AssetTypeMaterialGroup { return map[string]interface{}{ "dbid": search.Id, "dbAsset": v, }, nil } } return nil, errors.New("没有找到material信息") } // 没有找到queenter库,创建一个 database := &comm.Database{} database.CreateTime = time.Now() database.Categories = &comm.DbCategory{} database.Label = "queenter" database.Name = fmt.Sprintf("%s-%s", "queenter", apictx.User.Parent) database.Assets = CreateDefaultDbAssets(apictx.User.Parent) // 初始化资源表 materialAsset := &comm.DbAsset{} for _, asset := range database.Assets { if asset.Type == comm.AssetTypeMaterialGroup { materialAsset = asset break } } database.UserId = apictx.User.ID id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, database) if err != nil { return nil, err } return map[string]interface{}{ "dbid": id, "dbAsset": materialAsset, }, nil } func CreateDefaultDbAssets(userId string) []*comm.DbAsset { return []*comm.DbAsset{ { Id: primitive.NewObjectID().Hex(), UserId: userId, Label: "模型", Type: comm.AssetTypeMesh, Collection: "mesh", CreateTime: time.Now(), }, { Id: primitive.NewObjectID().Hex(), UserId: userId, Label: "图片", Type: comm.AssetTypeImage, Collection: "image", CreateTime: time.Now(), }, { Id: primitive.NewObjectID().Hex(), UserId: userId, Label: "材质球", Type: comm.AssetTypeMaterial, Collection: "material", CreateTime: time.Now(), }, { Id: primitive.NewObjectID().Hex(), UserId: userId, Label: "材质球组", Type: comm.AssetTypeMaterialGroup, Collection: "materialGroup", CreateTime: time.Now(), }, { Id: primitive.NewObjectID().Hex(), UserId: userId, Label: "环境球", Type: comm.AssetTypeEnv3d, Collection: "env3d", CreateTime: time.Now(), }, } }