unit_del.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-garden/errors"
  14. "property-garden/impl/v1/statistic"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. )
  19. func checkUnitDelParam(req *pb_v1.UnitDelRequest) 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 UnitDel(ctx context.Context, req *pb_v1.UnitDelRequest) (reply *pb_v1.UnitDelReply, err error) {
  30. reply = &pb_v1.UnitDelReply{}
  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 = checkUnitDelParam(req)
  44. if err != nil {
  45. return nil, err
  46. }
  47. dbname := utils.GetGardenDbName(req.GardenId)
  48. oldUnit := dbmodel.NewUnit(dbname)
  49. where := map[string]interface{}{
  50. "id": req.Id,
  51. //"garden_id": req.GardenId,
  52. }
  53. err = oldUnit.Find(database.DB(), where)
  54. if err != nil && err != gorm.ErrRecordNotFound {
  55. return nil, errors.DataBaseError
  56. }
  57. if oldUnit.ID == 0 {
  58. return nil, errors.ErrRecordNotFound
  59. }
  60. reply.Origin = &pb_v1.UnitUpdateRequest{
  61. Id:req.Id,
  62. GardenId:req.GardenId,
  63. UnitNumber:oldUnit.UnitNumber,
  64. UnitLayers:oldUnit.UnitLayers,
  65. UnitName:oldUnit.UnitName,
  66. BuildingId:oldUnit.BuildingId,
  67. }
  68. if oldUnit.HasLift == 1 {
  69. reply.Origin.HasLift = true
  70. }
  71. house := dbmodel.NewHouse(dbname)
  72. where = map[string]interface{}{
  73. "unit_id": req.Id,
  74. "status >": HouseStatusWait,
  75. }
  76. count, err := house.Count(database.DB(), where, nil)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. if count > 0 {
  81. return nil, status.Error(10003, "已入住房屋, 不能修改房屋编号,单元号,楼栋号")
  82. }
  83. houseIds, err := GetHouseIds(req.Id, 0, 0, dbname)
  84. if err != nil {
  85. return nil, err
  86. }
  87. db := database.DB().Begin()
  88. unit := dbmodel.NewUnit(dbname)
  89. where = map[string]interface{}{
  90. "id":req.Id,
  91. }
  92. err = unit.Delete(db, where)
  93. if err != nil {
  94. db.Rollback()
  95. return nil, errors.DataBaseError
  96. }
  97. house = dbmodel.NewHouse(dbname)
  98. where = map[string]interface{}{
  99. "unit_id":req.Id,
  100. }
  101. houseCount, err := house.Count(database.DB(), where, nil)
  102. if err != nil {
  103. db.Rollback()
  104. return nil, errors.DataBaseError
  105. }
  106. err = house.Delete(db, where)
  107. if err != nil {
  108. db.Rollback()
  109. return nil, errors.DataBaseError
  110. }
  111. isBind, err := IsHouseBindCharge(req.GardenId, houseIds, db)
  112. if err != nil {
  113. db.Rollback()
  114. return nil, err
  115. }
  116. if isBind {
  117. db.Rollback()
  118. return nil, status.Error(10003, "请先解除房屋绑定费用")
  119. }
  120. mreq := pb_v1.ObjStatisticSetRequest{
  121. ObjType:statistic.ObjTypeHouse,
  122. TotalIncrease:int64(0-houseCount),
  123. GardenId:req.GardenId,
  124. }
  125. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  126. if err != nil {
  127. db.Rollback()
  128. return nil, err
  129. }
  130. if err = RefuseHouseApply(req.GardenId, oldUnit.BuildingId, oldUnit.ID, 0); err != nil {
  131. db.Rollback()
  132. return nil, err
  133. }
  134. db.Commit()
  135. return reply, nil
  136. }