house_del.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "property-garden/impl/v1/statistic"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkHouseDelParam(req *pb_v1.HouseDelRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case req.Id < 1:
  24. return status.Error(10003, "id不能为空")
  25. }
  26. return nil
  27. }
  28. //
  29. func HouseDel(ctx context.Context, req *pb_v1.HouseDelRequest) (reply *pb_v1.HouseDelReply, err error) {
  30. reply = &pb_v1.HouseDelReply{}
  31. // 捕获各个task中的异常并返回给调用者
  32. defer func() {
  33. if r := recover(); r != nil {
  34. err = fmt.Errorf("%+v", r)
  35. e := &status.Status{}
  36. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  37. logger.Error("err",
  38. zap.String("system_err", err.Error()),
  39. zap.Stack("stacktrace"))
  40. }
  41. }
  42. }()
  43. err = checkHouseDelParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. oldHouse := dbmodel.NewHouse(dbname)
  49. where := map[string]interface{}{
  50. "id": req.Id,
  51. //"garden_id":req.GardenId,
  52. }
  53. err = oldHouse.Find(database.DB(), where)
  54. if err != nil && err != gorm.ErrRecordNotFound {
  55. return nil, errors.DataBaseError
  56. }
  57. if oldHouse.ID == 0 {
  58. return nil, errors.ErrRecordNotFound
  59. }
  60. if oldHouse.Status > HouseStatusWait {
  61. return nil, status.Error(10003, "已入住房屋, 不能删改房屋编号,单元号,楼栋号")
  62. }
  63. reply.Origin = &pb_v1.HouseUpdateRequest{
  64. Id: req.Id,
  65. GardenId: req.GardenId,
  66. UnitId: oldHouse.UnitId,
  67. HouseNumber: oldHouse.HouseNumber,
  68. RoomCount: oldHouse.RoomCount,
  69. HallCount: oldHouse.HallCount,
  70. Layer: oldHouse.Layer,
  71. HouseType: oldHouse.HouseType,
  72. HouseUsedArea: oldHouse.HouseUsedArea,
  73. HouseArea: oldHouse.HouseArea,
  74. }
  75. house := dbmodel.NewHouse(dbname)
  76. where = map[string]interface{}{
  77. "id": req.Id,
  78. }
  79. db := database.DB().Begin()
  80. defer func() {
  81. if err != nil {
  82. db.Rollback()
  83. }
  84. }()
  85. isBind, err := IsHouseBindCharge(req.GardenId, []int64{req.Id}, db)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if isBind {
  90. return nil, status.Error(10003, "请先解除房屋绑定费用")
  91. }
  92. err = house.Delete(db, where)
  93. if err != nil {
  94. return nil, errors.DataBaseError
  95. }
  96. mreq := pb_v1.ObjStatisticSetRequest{
  97. ObjType: statistic.ObjTypeHouse,
  98. TotalIncrease: int64(-1),
  99. GardenId: req.GardenId,
  100. }
  101. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if err = RefuseHouseApply(req.GardenId, oldHouse.BuildingId, oldHouse.UnitId, oldHouse.ID); err != nil {
  106. return nil, err
  107. }
  108. if err = houseCountChange(req.GardenId, int64(-1)); err != nil {
  109. return nil, err
  110. }
  111. db.Commit()
  112. return reply, nil
  113. }