material-asset.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package api
  2. import (
  3. "assetcenter/db/repo"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "infish.cn/comm"
  10. )
  11. func DbMaterialDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  12. // 查询db数据库看是否存在queenter库
  13. search := &comm.Database{}
  14. ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  15. CollectName: repo.CollectionDatabase,
  16. Query: repo.Map{"name": fmt.Sprintf("%s-%s", "queenter", apictx.User.Parent)}},
  17. search)
  18. fmt.Println(err)
  19. if err != nil {
  20. return nil, err
  21. }
  22. // 找到数据,返回material库信息
  23. if ok {
  24. for _, v := range search.Assets {
  25. if v.Type == comm.AssetTypeMaterialGroup {
  26. return map[string]interface{}{
  27. "dbid": search.Id,
  28. "dbAsset": v,
  29. }, nil
  30. }
  31. }
  32. return nil, errors.New("没有找到material信息")
  33. }
  34. // 没有找到queenter库,创建一个
  35. database := &comm.Database{}
  36. database.CreateTime = time.Now()
  37. database.Categories = &comm.DbCategory{}
  38. database.Label = "queenter"
  39. database.Name = fmt.Sprintf("%s-%s", "queenter", apictx.User.Parent)
  40. database.Assets = CreateDefaultDbAssets(apictx.User.Parent)
  41. // 初始化资源表
  42. materialAsset := &comm.DbAsset{}
  43. for _, asset := range database.Assets {
  44. if asset.Type == comm.AssetTypeMaterialGroup {
  45. materialAsset = asset
  46. break
  47. }
  48. }
  49. database.UserId = apictx.User.ID
  50. id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, database)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return map[string]interface{}{
  55. "dbid": id,
  56. "dbAsset": materialAsset,
  57. }, nil
  58. }
  59. func CreateDefaultDbAssets(userId string) []*comm.DbAsset {
  60. return []*comm.DbAsset{
  61. {
  62. Id: primitive.NewObjectID().Hex(),
  63. UserId: userId,
  64. Label: "模型",
  65. Type: comm.AssetTypeMesh,
  66. Collection: "mesh",
  67. CreateTime: time.Now(),
  68. },
  69. {
  70. Id: primitive.NewObjectID().Hex(),
  71. UserId: userId,
  72. Label: "图片",
  73. Type: comm.AssetTypeImage,
  74. Collection: "image",
  75. CreateTime: time.Now(),
  76. },
  77. {
  78. Id: primitive.NewObjectID().Hex(),
  79. UserId: userId,
  80. Label: "材质球",
  81. Type: comm.AssetTypeMaterial,
  82. Collection: "material",
  83. CreateTime: time.Now(),
  84. },
  85. {
  86. Id: primitive.NewObjectID().Hex(),
  87. UserId: userId,
  88. Label: "材质球组",
  89. Type: comm.AssetTypeMaterialGroup,
  90. Collection: "materialGroup",
  91. CreateTime: time.Now(),
  92. },
  93. {
  94. Id: primitive.NewObjectID().Hex(),
  95. UserId: userId,
  96. Label: "环境球",
  97. Type: comm.AssetTypeEnv3d,
  98. Collection: "env3d",
  99. CreateTime: time.Now(),
  100. },
  101. }
  102. }