asset-decorate.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package repo
  2. import (
  3. "errors"
  4. "mats/conf"
  5. "mats/db/model"
  6. "time"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. )
  9. type AssetDecorateMeshRepo struct {
  10. Conf *conf.AppAsset
  11. Model *model.AssetDecorateMesh
  12. }
  13. func (repo *AssetDecorateMeshRepo) GetEntity() interface{} {
  14. return repo.Model
  15. }
  16. func (repo *AssetDecorateMeshRepo) Upload(ctx *RepoSession, userId string) (interface{}, error) {
  17. asset := repo.Model
  18. if asset.File == nil || asset.File.Size < 1 || len(asset.File.Url) < 1 {
  19. return nil, errors.New("模型文件不能为空")
  20. }
  21. asset.CreateTime = time.Now()
  22. asset.UserId, _ = primitive.ObjectIDFromHex(userId)
  23. asset.UpdateTime = time.Now()
  24. asset.State = model.AssetState_Created
  25. collectionName := "asset-" + repo.Conf.Type
  26. assetId, err := RepoAddDoc(ctx, collectionName, asset)
  27. if err != nil {
  28. return nil, err
  29. }
  30. _, err = CreateMeshConvTask(ctx, asset.File, assetId, collectionName, userId)
  31. if err != nil { //模型转换失败,删除对应的模型
  32. RepoDeleteDoc(ctx, collectionName, assetId)
  33. return nil, err
  34. }
  35. // _, err = CreateShadowTask(ctx, asset.File, assetId, collectionName, userId)
  36. // if err != nil {
  37. // log.Error("CreateShadowTask err ", err)
  38. // fmt.Println("CreateShadowTask err ", err)
  39. // }
  40. return assetId, err
  41. }
  42. func (repo *AssetDecorateMeshRepo) Update(ctx *RepoSession) (interface{}, error) {
  43. asset := repo.Model
  44. if len(asset.Id) < 1 {
  45. return nil, errors.New("id不能为空!")
  46. }
  47. id := asset.Id.Hex()
  48. asset.Id = primitive.NilObjectID
  49. asset.UpdateTime = time.Now()
  50. collectionName := "asset-" + repo.Conf.Type
  51. return RepoUpdateSetDoc(ctx, collectionName, id, asset)
  52. }
  53. func (repo *AssetDecorateMeshRepo) Detail(ctx *RepoSession, id string) (interface{}, error) {
  54. collectionName := "asset-" + repo.Conf.Type
  55. ok, ret := RepoSeachDocMap(ctx, &DocSearchOptions{
  56. CollectName: collectionName,
  57. Query: Map{"_id": id},
  58. Project: []string{"name", "createTime", "updateTime", "state", "thumbnail", "categories", "osgjs", "file", "shadow", "components"},
  59. })
  60. if !ok {
  61. return nil, errors.New("数据为空!")
  62. }
  63. return ret, nil
  64. }