world.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package mysql
  2. import (
  3. "context"
  4. "fmt"
  5. "pji_desktop_http/biz/dal/query"
  6. "pji_desktop_http/biz/model"
  7. )
  8. func AddWorld(ctx context.Context, world model.World) {
  9. w := query.World
  10. err := w.WithContext(ctx).Create(&world)
  11. if err != nil {
  12. panic(err)
  13. }
  14. }
  15. func QueryWorld(ctx context.Context, sceneId string) (*model.World, error) {
  16. w := query.World
  17. world, err := w.WithContext(ctx).Where(w.SceneID.Eq(sceneId)).Order(w.CreatedAt.Desc()).First()
  18. if err != nil {
  19. fmt.Println("Query world failed:", err.Error())
  20. return nil, err
  21. }
  22. fmt.Print("Query world successfully:", world)
  23. return world, err
  24. }
  25. // UpdateWorld 更新world记录
  26. // updateValue中SceneID字段不能为空
  27. func UpdateWorld(ctx context.Context, updateValue model.World) error {
  28. w := query.World
  29. fmt.Println("updateValue.SceneID", updateValue.SceneID)
  30. // 查询对应场景最新的记录
  31. world, err := w.WithContext(ctx).Where(w.SceneID.Eq(updateValue.SceneID)).Order(w.CreatedAt.Desc()).First()
  32. // 根据id更新对应记录
  33. if err != nil {
  34. return err
  35. }
  36. res, err := w.WithContext(ctx).Where(w.ID.Eq(world.ID)).Updates(updateValue)
  37. fmt.Println("res", res)
  38. if err != nil {
  39. fmt.Println("Update world failed:", err.Error())
  40. return err
  41. }
  42. world, err = w.WithContext(ctx).Where(w.ID.Eq(world.ID)).First()
  43. fmt.Print("Update world successfully:", world)
  44. return nil
  45. }