Переглянути джерело

feat: 添加stl文件上传接口

HeWang 9 місяців тому
батько
коміт
b17d2c770d
3 змінених файлів з 72 додано та 2 видалено
  1. 24 2
      biz/dal/mysql/world.go
  2. 45 0
      biz/handler/world_service/world_service.go
  3. 3 0
      router.go

+ 24 - 2
biz/dal/mysql/world.go

@@ -19,9 +19,31 @@ func QueryWorld(ctx context.Context, sceneId string) (*model.World, error) {
 	w := query.World
 	world, err := w.WithContext(ctx).Where(w.SceneID.Eq(sceneId)).Order(w.CreatedAt.Desc()).First()
 	if err != nil {
-		fmt.Println("query world failed:", err.Error())
+		fmt.Println("Query world failed:", err.Error())
 		return nil, err
 	}
-	fmt.Print("query world successfully:", world)
+	fmt.Print("Query world successfully:", world)
 	return world, err
 }
+
+// UpdateWorld 更新world记录
+// updateValue中SceneID字段不能为空
+func UpdateWorld(ctx context.Context, updateValue model.World) error {
+	w := query.World
+	fmt.Println("updateValue.SceneID", updateValue.SceneID)
+	// 查询对应场景最新的记录
+	world, err := w.WithContext(ctx).Where(w.SceneID.Eq(updateValue.SceneID)).Order(w.CreatedAt.Desc()).First()
+	// 根据id更新对应记录
+	if err != nil {
+		return err
+	}
+	res, err := w.WithContext(ctx).Where(w.ID.Eq(world.ID)).Updates(updateValue)
+	fmt.Println("res", res)
+	if err != nil {
+		fmt.Println("Update world failed:", err.Error())
+		return err
+	}
+	world, err = w.WithContext(ctx).Where(w.ID.Eq(world.ID)).First()
+	fmt.Print("Update world successfully:", world)
+	return nil
+}

+ 45 - 0
biz/handler/world_service/world_service.go

@@ -55,6 +55,51 @@ func UploadWorldFile(ctx context.Context, c *app.RequestContext) {
 	mysql.AddWorld(ctx, world)
 }
 
+// UploadSTLFile 将map.stl文件上传到oss
+// @router /world/upload/stlfile [GET]
+func UploadSTLFile(ctx context.Context, c *app.RequestContext) {
+
+	equipmentNo := c.Query("equipmentNo")
+	fmt.Println("equipmentNo", equipmentNo)
+
+	sceneNo := c.Query("sceneNo")
+	fmt.Println("sceneNo", sceneNo)
+
+	header, err := c.FormFile("file")
+	if err != nil {
+		c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
+		return
+	}
+
+	fileName := header.Filename
+	fmt.Println("filename", fileName)
+
+	ossSTLObjectKey := config.WorldOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + "map.stl"
+	fmt.Println("ossSTLObjectKey", ossSTLObjectKey)
+
+	f, _ := header.Open()
+	defer f.Close()
+
+	config.OssMutex.Lock()
+	err = config.OssBucket.PutObject(ossSTLObjectKey, f)
+	config.OssMutex.Unlock()
+	if err != nil {
+		c_log.GlobalLogger.Error("程序异常退出。上传stl文件", fileName, "->", ossSTLObjectKey, "出错:", err)
+		c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传stl文件失败"})
+		return
+	}
+	c_log.GlobalLogger.Info("上传stl文件", fileName, "->", ossSTLObjectKey, "成功。")
+	c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传stl文件成功"})
+
+	// 更新stl记录到world数据库
+	updateValue := model.World{SceneID: sceneNo, StlURL: &ossSTLObjectKey}
+	//world := model.World{ID: uuid.NewV1().String(), SceneID: sceneNo, WorldURL: ossSTLObjectKey}
+	err = mysql.UpdateWorld(ctx, updateValue)
+	if err != nil {
+		return
+	}
+}
+
 // CheckWorldFileStatus 检查world文件在oss中是否存在
 // @router /world/check/file/world/status [GET]
 func CheckWorldFileStatus(ctx context.Context, c *app.RequestContext) {

+ 3 - 0
router.go

@@ -28,6 +28,9 @@ func customizedRegister(r *server.Hertz) {
 	r.GET("/world/upload/worldfile", world_service.UploadWorldFile)
 	r.POST("/world/upload/worldfile", world_service.UploadWorldFile)
 
+	r.GET("/world/upload/stlfile", world_service.UploadSTLFile)
+	r.POST("/world/upload/stlfile", world_service.UploadSTLFile)
+
 	r.GET("/world/check/file/world/status", world_service.CheckWorldFileStatus)
 
 	r.GET("/simulation/download/zipfile", simulation_service.DownloadSimulationZipFile)