neighbor_class_update.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package neighbor
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/oss_utils"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "strings"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. func checkNeighborClassUpdateParam(req *pb_v1.NeighborClassUpdateRequest) error {
  22. switch {
  23. case req.GardenId == 0:
  24. return status.Error(10003, "小区不能为空")
  25. case req.ClassName == "":
  26. return status.Error(10003, "名称不能为空")
  27. case req.Id == 0:
  28. return status.Error(10003, "id 不能为空")
  29. }
  30. return nil
  31. }
  32. func NeighborClassUpdate(ctx context.Context, req *pb_v1.NeighborClassUpdateRequest) (reply *pb_v1.NeighborClassUpdateReply, err error) {
  33. reply = &pb_v1.NeighborClassUpdateReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. // 参数检查
  47. err = checkNeighborClassUpdateParam(req)
  48. if err != nil {
  49. return nil, err
  50. }
  51. dbname := utils.GetGardenDbName(req.GardenId)
  52. // 获取旧数据
  53. old := dbmodel.NewNeighborClass(dbname)
  54. where := map[string]interface{}{
  55. "id":req.Id,
  56. }
  57. err = old.Find(database.DB(), where)
  58. if err != nil && err != gorm.ErrRecordNotFound {
  59. return nil, errors.DataBaseError
  60. }
  61. if old.ID == 0 {
  62. return nil, errors.ErrRecordNotFound
  63. }
  64. // 更新新数据
  65. now := time.Now()
  66. class := dbmodel.NewNeighborClass(dbname)
  67. enable := 1
  68. if !req.Enable {
  69. enable = 2
  70. }
  71. values := map[string]interface{}{
  72. "class_name":req.ClassName,
  73. "enable":enable,
  74. "updated_at":now,
  75. "class_pic":req.ClassPic,
  76. }
  77. where = map[string]interface{}{
  78. "id":req.Id,
  79. }
  80. db := database.DB().Begin()
  81. err = class.Update(db, where, values)
  82. if err != nil {
  83. db.Rollback()
  84. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  85. return nil, status.Error(10003, "分类名已存在")
  86. }
  87. return nil, errors.DataBaseError
  88. }
  89. if err := oss_utils.OssObjAdd([]string{req.ClassPic}, []string{old.ClassPic}); err != nil {
  90. db.Rollback()
  91. return nil, err
  92. }
  93. db.Commit()
  94. return reply, nil
  95. }