unit_update.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "strings"
  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 checkUnitUpdateParam(req *pb_v1.UnitUpdateRequest) error {
  21. switch {
  22. case req.UnitNumber < 0:
  23. return status.Error(10003, "单元编号不能为空")
  24. case req.UnitName == "":
  25. return status.Error(10003, "单元名称名称不能为空")
  26. case req.UnitLayers < 1:
  27. return status.Error(10003, "层数不能为空")
  28. case req.GardenId < 1:
  29. return status.Error(10003, "小区不能为空")
  30. case req.BuildingId < 1:
  31. return status.Error(10003, "楼栋id不能为空")
  32. case req.Id < 1:
  33. return status.Error(10003, "id 不能为空")
  34. }
  35. return nil
  36. }
  37. //
  38. func UnitUpdate(ctx context.Context, req *pb_v1.UnitUpdateRequest) (reply *pb_v1.UnitUpdateReply, err error) {
  39. reply = &pb_v1.UnitUpdateReply{}
  40. // 捕获各个task中的异常并返回给调用者
  41. defer func() {
  42. if r := recover(); r != nil {
  43. err = fmt.Errorf("%+v", r)
  44. e := &status.Status{}
  45. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  46. logger.Error("err",
  47. zap.String("system_err", err.Error()),
  48. zap.Stack("stacktrace"))
  49. }
  50. }
  51. }()
  52. err = checkUnitUpdateParam(req)
  53. if err != nil {
  54. return nil, err
  55. }
  56. dbname := utils.GetGardenDbName(req.GardenId)
  57. oldUnit := dbmodel.NewUnit(dbname)
  58. where := map[string]interface{}{
  59. "id":req.Id,
  60. //"garden_id":req.GardenId,
  61. }
  62. err = oldUnit.Find(database.DB(), where)
  63. if err != nil && err != gorm.ErrRecordNotFound {
  64. return nil, errors.DataBaseError
  65. }
  66. if oldUnit.ID == 0 {
  67. return nil, errors.ErrRecordNotFound
  68. }
  69. reply.Origin = &pb_v1.UnitUpdateRequest{
  70. Id:req.Id,
  71. GardenId:req.GardenId,
  72. UnitNumber:oldUnit.UnitNumber,
  73. UnitLayers:oldUnit.UnitLayers,
  74. UnitName:oldUnit.UnitName,
  75. BuildingId:oldUnit.BuildingId,
  76. }
  77. if oldUnit.HasLift == 1 {
  78. reply.Origin.HasLift = true
  79. }
  80. newBuilding := dbmodel.NewBuilding(dbname)
  81. where = map[string]interface{}{
  82. "id":req.BuildingId,
  83. //"garden_id":req.GardenId,
  84. }
  85. err = newBuilding.Find(database.DB(), where)
  86. if err != nil && err != gorm.ErrRecordNotFound {
  87. return nil, errors.DataBaseError
  88. }
  89. if newBuilding.ID == 0 {
  90. return nil, errors.ErrRecordNotFound
  91. }
  92. if req.BuildingId != oldUnit.BuildingId || req.UnitNumber != oldUnit.UnitNumber {
  93. house := dbmodel.NewHouse(dbname)
  94. where := map[string]interface{}{
  95. "unit_id":req.Id,
  96. "status >":HouseStatusWait,
  97. }
  98. count, err := house.Count(database.DB(), where, nil)
  99. if err != nil {
  100. return nil, errors.DataBaseError
  101. }
  102. if count > 0 {
  103. return nil, status.Error(10003, "已入住房屋, 不能修改房屋编号,单元号,楼栋号")
  104. }
  105. }
  106. hasLift := int64(2)
  107. if req.HasLift {
  108. hasLift = 1
  109. }
  110. now := time.Now()
  111. unit := dbmodel.NewUnit(dbname)
  112. values := map[string]interface{}{
  113. "unit_name":req.UnitName,
  114. "unit_number":req.UnitNumber,
  115. "unit_layers":req.UnitLayers,
  116. "building_id":req.BuildingId,
  117. "has_lift":hasLift,
  118. "updated_at": now,
  119. }
  120. db := database.DB().Begin()
  121. where = map[string]interface{}{
  122. "id":req.Id,
  123. //"garden_id":req.GardenId,
  124. }
  125. err = unit.Update(db, where, values)
  126. if err != nil {
  127. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  128. db.Rollback()
  129. return nil, errors.ErrRecordExist
  130. }
  131. db.Rollback()
  132. return nil, errors.DataBaseError
  133. }
  134. // 更新房屋
  135. house := dbmodel.NewHouse(dbname)
  136. where = map[string]interface{}{
  137. "unit_id":req.Id,
  138. }
  139. values = map[string]interface{}{}
  140. if oldUnit.HasLift != hasLift {
  141. values["has_lift"] = hasLift
  142. }
  143. if req.BuildingId != oldUnit.BuildingId {
  144. values["building_id"] = req.BuildingId
  145. }
  146. if len(values) > 0 {
  147. err = house.Update(database.DB(), where, values)
  148. if err != nil {
  149. db.Rollback()
  150. return nil, errors.DataBaseError
  151. }
  152. }
  153. if req.BuildingId != oldUnit.BuildingId || req.UnitNumber != oldUnit.UnitNumber {
  154. if err = RefuseHouseApply(req.GardenId, oldUnit.BuildingId, oldUnit.ID, 0); err != nil {
  155. db.Rollback()
  156. return nil, err
  157. }
  158. }
  159. if hasLift != oldUnit.HasLift {
  160. mreq := pb_v1.HouseRentChangeFieldRequest{
  161. GardenId:req.GardenId,
  162. UnitId:req.Id,
  163. HasLift:req.HasLift,
  164. }
  165. err = houseRentChangeField(&mreq, db, dbname)
  166. if err != nil {
  167. db.Rollback()
  168. return nil, err
  169. }
  170. }
  171. db.Commit()
  172. return reply, nil
  173. }