building_update.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 checkBuildingUpdateParam(req *pb_v1.BuildingUpdateRequest) error {
  21. switch {
  22. case req.BuildingNumber == "":
  23. return status.Error(10003, "楼栋编号不能为空")
  24. case req.BuildingName == "":
  25. return status.Error(10003, "楼栋名称不能为空")
  26. case req.Id < 1:
  27. return status.Error(10003, "id 不能为空")
  28. case req.GardenId < 1:
  29. return status.Error(10003, "小区id不能为空")
  30. case strings.Contains(req.BuildingNumber, "-"):
  31. return status.Error(10003, "楼栋号不能包含-")
  32. }
  33. return nil
  34. }
  35. //
  36. func BuildingUpdate(ctx context.Context, req *pb_v1.BuildingUpdateRequest) (reply *pb_v1.BuildingUpdateReply, err error) {
  37. reply = &pb_v1.BuildingUpdateReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. err = checkBuildingUpdateParam(req)
  51. if err != nil {
  52. return nil, err
  53. }
  54. dbname := utils.GetGardenDbName(req.GardenId)
  55. old := dbmodel.NewBuilding(dbname)
  56. where := map[string]interface{}{
  57. "id": req.Id,
  58. //"garden_id":req.GardenId,
  59. }
  60. err = old.Find(database.DB(), where)
  61. if err != nil && err != gorm.ErrRecordNotFound {
  62. return nil, errors.DataBaseError
  63. }
  64. if old.ID == 0 {
  65. return nil, errors.ErrRecordNotFound
  66. }
  67. if req.BuildingNumber != old.BuildingNumber {
  68. house := dbmodel.NewHouse(dbname)
  69. where = map[string]interface{}{
  70. "building_id": req.Id,
  71. "status >": HouseStatusWait,
  72. }
  73. count, err := house.Count(database.DB(), where, nil)
  74. if err != nil {
  75. return nil, errors.DataBaseError
  76. }
  77. if count > 0 {
  78. return nil, status.Error(10003, "已入住房屋, 不能修改房屋编号,单元号,楼栋号")
  79. }
  80. }
  81. reply.Origin = &pb_v1.BuildingUpdateRequest{
  82. Id: req.Id,
  83. GardenId: req.GardenId,
  84. BuildingName: old.BuildingName,
  85. BuildingNumber: old.BuildingNumber,
  86. BuildingArea: old.BuildingArea,
  87. BuildingUsedArea: old.BuildingUsedArea,
  88. }
  89. now := time.Now()
  90. building := dbmodel.NewBuilding(dbname)
  91. values := map[string]interface{}{
  92. "building_name": req.BuildingName,
  93. "building_number": req.BuildingNumber,
  94. "building_area": req.BuildingArea,
  95. "building_used_area": req.BuildingUsedArea,
  96. "comment": req.Comment,
  97. "updated_at": now,
  98. }
  99. db := database.DB().Begin()
  100. where = map[string]interface{}{
  101. "id": req.Id,
  102. //"garden_id":req.GardenId,
  103. }
  104. err = building.Update(db, where, values)
  105. if err != nil {
  106. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  107. db.Rollback()
  108. return nil, errors.ErrRecordExist
  109. }
  110. db.Rollback()
  111. return nil, errors.DataBaseError
  112. }
  113. if req.BuildingNumber != old.BuildingNumber {
  114. // 删除未审核的房屋申请
  115. if err = RefuseHouseApply(req.GardenId, old.ID, 0, 0); err != nil {
  116. db.Rollback()
  117. return nil, err
  118. }
  119. }
  120. db.Commit()
  121. return reply, nil
  122. }