world_service.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package world_service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/cloudwego/hertz/pkg/app"
  6. "github.com/cloudwego/hertz/pkg/protocol/consts"
  7. uuid "github.com/satori/go.uuid"
  8. "net/http"
  9. "pji_desktop_http/biz/dal/mysql"
  10. "pji_desktop_http/biz/model"
  11. "pji_desktop_http/common/config"
  12. "pji_desktop_http/common/config/c_log"
  13. "pji_desktop_http/common/entity"
  14. )
  15. // UploadWorldFile 将world文件上传到oss
  16. // @router /world/upload/worldfile [GET]
  17. func UploadWorldFile(ctx context.Context, c *app.RequestContext) {
  18. equipmentNo := c.Query("equipmentNo")
  19. fmt.Println("equipmentNo", equipmentNo)
  20. sceneNo := c.Query("sceneNo")
  21. fmt.Println("sceneNo", sceneNo)
  22. header, err := c.FormFile("file")
  23. if err != nil {
  24. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  25. return
  26. }
  27. fileName := header.Filename
  28. fmt.Println("filename", fileName)
  29. ossWorldObjectKey := config.WorldOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + "map.world"
  30. fmt.Println("ossWorldObjectKey", ossWorldObjectKey)
  31. f, _ := header.Open()
  32. defer f.Close()
  33. config.OssMutex.Lock()
  34. err = config.OssBucket.PutObject(ossWorldObjectKey, f)
  35. config.OssMutex.Unlock()
  36. if err != nil {
  37. c_log.GlobalLogger.Error("程序异常退出。上传world文件", fileName, "->", ossWorldObjectKey, "出错:", err)
  38. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传world文件失败"})
  39. return
  40. }
  41. c_log.GlobalLogger.Info("上传world文件", fileName, "->", ossWorldObjectKey, "成功。")
  42. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传world文件成功"})
  43. // 保存world记录到数据库
  44. world := model.World{ID: uuid.NewV1().String(), SceneID: sceneNo, WorldURL: ossWorldObjectKey}
  45. mysql.AddWorld(ctx, world)
  46. }
  47. // UploadSTLFile 将map.stl文件上传到oss
  48. // @router /world/upload/stlfile [GET]
  49. func UploadSTLFile(ctx context.Context, c *app.RequestContext) {
  50. equipmentNo := c.Query("equipmentNo")
  51. fmt.Println("equipmentNo", equipmentNo)
  52. sceneNo := c.Query("sceneNo")
  53. fmt.Println("sceneNo", sceneNo)
  54. header, err := c.FormFile("file")
  55. if err != nil {
  56. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  57. return
  58. }
  59. fileName := header.Filename
  60. fmt.Println("filename", fileName)
  61. ossSTLObjectKey := config.WorldOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + "map.stl"
  62. fmt.Println("ossSTLObjectKey", ossSTLObjectKey)
  63. f, _ := header.Open()
  64. defer f.Close()
  65. config.OssMutex.Lock()
  66. err = config.OssBucket.PutObject(ossSTLObjectKey, f)
  67. config.OssMutex.Unlock()
  68. if err != nil {
  69. c_log.GlobalLogger.Error("程序异常退出。上传stl文件", fileName, "->", ossSTLObjectKey, "出错:", err)
  70. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传stl文件失败"})
  71. return
  72. }
  73. c_log.GlobalLogger.Info("上传stl文件", fileName, "->", ossSTLObjectKey, "成功。")
  74. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传stl文件成功"})
  75. // 更新stl记录到world数据库
  76. updateValue := model.World{SceneID: sceneNo, StlURL: &ossSTLObjectKey}
  77. //world := model.World{ID: uuid.NewV1().String(), SceneID: sceneNo, WorldURL: ossSTLObjectKey}
  78. err = mysql.UpdateWorld(ctx, updateValue)
  79. if err != nil {
  80. return
  81. }
  82. }
  83. // CheckWorldFileStatus 检查world文件在oss中是否存在
  84. // @router /world/check/file/world/status [GET]
  85. func CheckWorldFileStatus(ctx context.Context, c *app.RequestContext) {
  86. sceneID := c.Query("id")
  87. fmt.Println("id", sceneID)
  88. world, err := mysql.QueryWorld(ctx, sceneID)
  89. if err != nil || world == nil {
  90. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "world文件不存在"})
  91. return
  92. }
  93. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "world文件已存在"})
  94. }