suggestion_order_update.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package suggestion
  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 checkSuggestionOrderUpdateParam(req *pb_v1.SuggestionOrderUpdateRequest) error {
  22. switch {
  23. case req.Id == 0:
  24. return status.Error(10003, "id 不能为空")
  25. case req.GardenId == 0:
  26. return status.Error(10003, "小区不能为空")
  27. case req.SuggestionType < 1:
  28. return status.Error(10003, "投诉类型不能为空")
  29. case req.ApplyPeople == "":
  30. return status.Error(10003, "投诉人不能为空")
  31. case req.ApplyPeoplePhone != "":
  32. if !utils.VerifyMobileFormat(req.ApplyPeoplePhone) {
  33. return status.Error(10003, "手机格式错误")
  34. }
  35. case req.ApplyContent == "":
  36. return status.Error(10003, "投诉内容不能为空")
  37. }
  38. return nil
  39. }
  40. func SuggestionOrderUpdate(ctx context.Context, req *pb_v1.SuggestionOrderUpdateRequest) (reply *pb_v1.SuggestionOrderUpdateReply, err error) {
  41. reply = &pb_v1.SuggestionOrderUpdateReply{}
  42. // 捕获各个task中的异常并返回给调用者
  43. defer func() {
  44. if r := recover(); r != nil {
  45. err = fmt.Errorf("%+v", r)
  46. e := &status.Status{}
  47. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  48. logger.Error("err",
  49. zap.String("system_err", err.Error()),
  50. zap.Stack("stacktrace"))
  51. }
  52. }
  53. }()
  54. // 参数检查
  55. err = checkSuggestionOrderUpdateParam(req)
  56. if err != nil {
  57. return nil, err
  58. }
  59. dbname := utils.GetGardenDbName(req.GardenId)
  60. // 获取旧数据
  61. old := dbmodel.NewSuggestionOrder(dbname)
  62. where := map[string]interface{}{
  63. "id": req.Id,
  64. }
  65. err = old.Find(database.DB(), where)
  66. if err != nil && err != gorm.ErrRecordNotFound {
  67. return nil, errors.DataBaseError
  68. }
  69. if old.ID == 0 {
  70. return nil, errors.ErrRecordNotFound
  71. }
  72. if req.HouseholdUid > 0 && old.HouseholdUid != req.HouseholdUid {
  73. return nil, status.Error(10003, "您无法进行该操作")
  74. }
  75. reply.Origin = &pb_v1.SuggestionOrderUpdateRequest{
  76. GardenId: req.GardenId,
  77. Id: req.Id,
  78. // 分类id
  79. SuggestionType: old.SuggestionType,
  80. // 报修人
  81. ApplyPeople: old.ApplyPeople,
  82. // 报修人电话
  83. ApplyPeoplePhone: old.ApplyPeoplePhone,
  84. // 报修内容
  85. ApplyContent: old.ApplyContent,
  86. HouseholdUid: old.HouseholdUid,
  87. // 报修图片
  88. ApplyPic: strings.Split(old.ApplyPic, ";"),
  89. }
  90. // 更新新数据
  91. now := time.Now()
  92. repairOrder := dbmodel.NewSuggestionOrder(dbname)
  93. values := map[string]interface{}{
  94. "suggestion_type": req.SuggestionType,
  95. // 报修人
  96. "apply_people": req.ApplyPeople,
  97. // 报修人电话
  98. "apply_people_phone": req.ApplyPeoplePhone,
  99. // 报修内容
  100. "apply_content": req.ApplyContent,
  101. // 报修图片
  102. "apply_pic": utils.StringJoin(req.ApplyPic, ";"),
  103. "updated_at": now,
  104. }
  105. where = map[string]interface{}{
  106. "id": req.Id,
  107. }
  108. db := database.DB().Begin()
  109. err = repairOrder.Update(db, where, values)
  110. if err != nil {
  111. db.Rollback()
  112. return nil, errors.DataBaseError
  113. }
  114. if err := oss_utils.OssObjAdd(req.ApplyPic, strings.Split(old.ApplyPic, ";")); err != nil {
  115. db.Rollback()
  116. return nil, err
  117. }
  118. db.Commit()
  119. return reply, nil
  120. }