repair_order_update.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package repair
  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 checkRepairOrderUpdateParam(req *pb_v1.RepairOrderUpdateRequest) 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.ClassId < 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 RepairOrderUpdate(ctx context.Context, req *pb_v1.RepairOrderUpdateRequest) (reply *pb_v1.RepairOrderUpdateReply, err error) {
  41. reply = &pb_v1.RepairOrderUpdateReply{}
  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 = checkRepairOrderUpdateParam(req)
  56. if err != nil {
  57. return nil, err
  58. }
  59. dbname := utils.GetGardenDbName(req.GardenId)
  60. // 获取旧数据
  61. old := dbmodel.NewRepairOrder(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.RepairOrderUpdateRequest{
  76. GardenId: req.GardenId,
  77. Id: req.Id,
  78. // 分类id
  79. ClassId: old.ClassId,
  80. // 报修人
  81. ApplyPeople: old.ApplyPeople,
  82. // 报修人电话
  83. ApplyPeoplePhone: old.ApplyPeoplePhone,
  84. // 预约时间
  85. Appointment: old.Appointment.Unix(),
  86. // 房屋id
  87. HouseId: old.HouseId,
  88. // 报修内容
  89. ApplyContent: old.ApplyContent,
  90. HouseholdUid: old.HouseholdUid,
  91. // 报修图片
  92. ApplyPic: strings.Split(old.ApplyPic, ";"),
  93. }
  94. // 更新新数据
  95. now := time.Now()
  96. repairOrder := dbmodel.NewRepairOrder(dbname)
  97. values := map[string]interface{}{
  98. "class_id": req.ClassId,
  99. // 报修人
  100. "apply_people": req.ApplyPeople,
  101. // 报修人电话
  102. "apply_people_phone": req.ApplyPeoplePhone,
  103. // 预约时间
  104. "appointment": time.Unix(req.Appointment, 0),
  105. // 报修内容
  106. "apply_content": req.ApplyContent,
  107. // 报修图片
  108. "apply_pic": utils.StringJoin(req.ApplyPic, ";"),
  109. "updated_at": now,
  110. }
  111. if req.HouseId == 0 {
  112. values["building_id"] = 0
  113. values["unit_id"] = 0
  114. values["house_id"] = 0
  115. values["house_name"] = ""
  116. }
  117. if req.HouseId > 0 && req.HouseId != old.HouseId {
  118. houseInfo, err := getHouseInfo(req.HouseId, req.GardenId)
  119. if err != nil {
  120. return nil, errors.DataBaseError
  121. }
  122. values["building_id"] = houseInfo.BuildingId
  123. values["unit_id"] = houseInfo.UnitId
  124. values["house_id"] = req.HouseId
  125. values["house_name"] = houseInfo.HouseName
  126. }
  127. where = map[string]interface{}{
  128. "id": req.Id,
  129. }
  130. db := database.DB().Begin()
  131. err = repairOrder.Update(db, where, values)
  132. if err != nil {
  133. db.Rollback()
  134. return nil, errors.DataBaseError
  135. }
  136. if err := oss_utils.OssObjAdd(req.ApplyPic, strings.Split(old.ApplyPic, ";")); err != nil {
  137. db.Rollback()
  138. return nil, err
  139. }
  140. db.Commit()
  141. return reply, nil
  142. }