update.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house_rent
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-household/errors"
  10. "property-household/impl/v1/house"
  11. dbmodel "property-household/model"
  12. pb_v1 "property-household/pb/v1"
  13. "property-household/utils"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkHouseRentUpdateParam(req *pb_v1.HouseRentUpdateRequest) error {
  21. switch {
  22. case req.RentPrice == 0:
  23. return status.Error(10003, "租金不能为空")
  24. case req.RentType < 1:
  25. return status.Error(10003, "出租方式不能为空")
  26. case req.ContactPhone == "":
  27. return status.Error(10003, "联系人电话不能为空")
  28. case req.Contacter == "":
  29. return status.Error(10003, "联系人不能为空")
  30. case req.HouseId == 0:
  31. return status.Error(10003, "房屋不能为空")
  32. case req.Id < 1:
  33. return status.Error(10003, "id不能为空")
  34. case req.GardenId < 1:
  35. return status.Error(10003, "小区不能为空")
  36. }
  37. housePic := []string{}
  38. for _, v := range req.HousePic {
  39. if isFix(v) {
  40. continue
  41. }
  42. if v == "" {
  43. continue
  44. }
  45. housePic = append(housePic, v)
  46. }
  47. req.HousePic = housePic
  48. if req.CertPic == nil {
  49. req.CertPic = []string{}
  50. }
  51. return nil
  52. }
  53. //
  54. func HouseRentUpdate(ctx context.Context, req *pb_v1.HouseRentUpdateRequest) (reply *pb_v1.HouseRentUpdateReply, err error) {
  55. reply = &pb_v1.HouseRentUpdateReply{}
  56. // 捕获各个task中的异常并返回给调用者
  57. defer func() {
  58. if r := recover(); r != nil {
  59. err = fmt.Errorf("%+v", r)
  60. e := &status.Status{}
  61. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  62. logger.Error("err",
  63. zap.String("system_err", err.Error()),
  64. zap.Stack("stacktrace"))
  65. }
  66. }
  67. }()
  68. err = checkHouseRentUpdateParam(req)
  69. if err != nil {
  70. return nil, err
  71. }
  72. houseInfo, err := house.GetHouseInfo(req.HouseId, req.GardenId)
  73. if err != nil {
  74. return nil, err
  75. }
  76. p := dbmodel.THouseRent{}
  77. where := map[string]interface{}{
  78. "id": req.Id,
  79. }
  80. err = p.Find(database.DB(), where)
  81. if err != nil && err != gorm.ErrRecordNotFound {
  82. return nil, errors.DataBaseError
  83. }
  84. if p.ID == 0 {
  85. return nil, errors.ErrRecordNotFound
  86. }
  87. if p.ApproveStatus == 2 {
  88. return nil, status.Error(10003, "当前状态不能修改")
  89. }
  90. if p.HouseId != req.HouseId || p.GardenId != req.GardenId {
  91. old := dbmodel.THouseRent{}
  92. oldWhere := map[string]interface{}{
  93. "garden_id": req.GardenId,
  94. "house_id": req.HouseId,
  95. "approve_status <": 4,
  96. }
  97. oldCount, err := old.Count(database.DB(), oldWhere, nil)
  98. if err != nil {
  99. return nil, errors.DataBaseError
  100. }
  101. if oldCount > 0 {
  102. return nil, status.Error(10003, "已存在相同记录")
  103. }
  104. }
  105. now := time.Now()
  106. haseLift := int32(1)
  107. if !houseInfo.HasLift {
  108. haseLift = 2
  109. }
  110. values := map[string]interface{}{
  111. "house_name": fmt.Sprintf("%v-%v-%v", houseInfo.BuildingNumber, houseInfo.UnitNumber, houseInfo.HouseNumber),
  112. "layer": houseInfo.Layer,
  113. "house_area": houseInfo.HouseArea,
  114. "diretion": req.Direction,
  115. "room_count": houseInfo.RoomCount,
  116. "hall_count": houseInfo.HallCount,
  117. "wc_count": req.WcCount,
  118. "decorating": req.Decorating,
  119. "contacter": req.Contacter,
  120. "contact_phone": req.ContactPhone,
  121. "pay_time_type": req.PayTimeType,
  122. "rent_type": req.RentType,
  123. "room_type": req.RoomType,
  124. "room_area": req.RoomArea,
  125. "rent_price": req.RentPrice,
  126. "desposit": req.Desposit,
  127. "in_time": req.InTime,
  128. "service_price": req.ServicePrice,
  129. "intermediary_price": req.IntermediaryPrice,
  130. "base_conf": req.BaseConf,
  131. "special_conf": req.SpecialConf,
  132. "house_pic": utils.StringJoin(req.HousePic, ";"),
  133. "cert_pic": utils.StringJoin(req.CertPic, ";"),
  134. "has_lift": haseLift,
  135. "updated_at": now,
  136. "desc": req.Desc,
  137. "house_id": req.HouseId,
  138. "unit_id": houseInfo.UnitId,
  139. "approve_status": 1,
  140. }
  141. if req.HasLift {
  142. values["has_lift"] = 1
  143. }
  144. db := database.DB().Begin()
  145. err = p.Update(db, where, values)
  146. if err != nil {
  147. db.Rollback()
  148. return nil, errors.DataBaseError
  149. }
  150. err = p.Find(db, where)
  151. if err != nil {
  152. db.Rollback()
  153. return nil, errors.DataBaseError
  154. }
  155. err = houseRentSync(p, false, req.GardenId, 0)
  156. if err != nil {
  157. db.Rollback()
  158. return nil, err
  159. }
  160. db.Commit()
  161. return reply, nil
  162. }