building_del.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/charge_utils"
  15. "property-garden/impl/v1/statistic"
  16. dbmodel "property-garden/model"
  17. "property-garden/pb"
  18. pb_v1 "property-garden/pb/v1"
  19. "property-garden/utils"
  20. )
  21. func checkBuildingDelParam(req *pb_v1.BuildingDelRequest) error {
  22. switch {
  23. case req.Id < 1:
  24. return status.Error(10003, "id 不能为空")
  25. case req.GardenId < 1:
  26. return status.Error(10003, "小区id不能为空")
  27. }
  28. return nil
  29. }
  30. const (
  31. HouseholdApproveStatusWait = 1
  32. HouseholdApproveStatusSuccess = 2
  33. HouseholdApproveStatusFail = 3
  34. )
  35. func RefuseHouseApply(gardenId int64, buildingId int64, unitId int64, houseId int64) error {
  36. mreq := pb_v1.HouseholdRefuseRequest{
  37. GardenId: gardenId,
  38. HouseId: houseId,
  39. BuildingId: buildingId,
  40. UnitId: unitId,
  41. Feedback: "房屋变更,请重新申请",
  42. }
  43. _, err := pb.Household.HouseholdRefuse(context.Background(), &mreq)
  44. if err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func GetHouseIds(buildingId int64, unitId int64, houseId int64, dbname string) ([]int64, error) {
  50. if houseId > 0 {
  51. return []int64{houseId}, nil
  52. }
  53. where := map[string]interface{}{}
  54. if unitId > 0 {
  55. where["unit_id"] = unitId
  56. }
  57. if buildingId > 0 {
  58. where["building_id"] = buildingId
  59. }
  60. p := dbmodel.NewHouse(dbname)
  61. ids, err := p.GetIds(database.DB(), where)
  62. if err != nil {
  63. return nil, errors.DataBaseError
  64. }
  65. return ids, nil
  66. }
  67. func IsHouseBindCharge(gardenId int64, houseIds []int64, db *gorm.DB) (bool, error) {
  68. dbname := utils.GetGardenDbName(gardenId)
  69. p := dbmodel.NewChargeBind(dbname)
  70. where := map[string]interface{}{
  71. "obj_id in": houseIds,
  72. "obj_type": charge_utils.ObjTypeHouse,
  73. }
  74. count, err := p.Count(db, where, nil)
  75. if err != nil {
  76. return false, errors.DataBaseError
  77. }
  78. if count > 0 {
  79. return true, nil
  80. }
  81. return false, nil
  82. }
  83. //
  84. func BuildingDel(ctx context.Context, req *pb_v1.BuildingDelRequest) (reply *pb_v1.BuildingDelReply, err error) {
  85. reply = &pb_v1.BuildingDelReply{}
  86. // 捕获各个task中的异常并返回给调用者
  87. defer func() {
  88. if r := recover(); r != nil {
  89. err = fmt.Errorf("%+v", r)
  90. e := &status.Status{}
  91. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  92. logger.Error("err",
  93. zap.String("system_err", err.Error()),
  94. zap.Stack("stacktrace"))
  95. }
  96. }
  97. }()
  98. err = checkBuildingDelParam(req)
  99. if err != nil {
  100. return nil, err
  101. }
  102. dbname := utils.GetGardenDbName(req.GardenId)
  103. old := dbmodel.NewBuilding(dbname)
  104. where := map[string]interface{}{
  105. "id": req.Id,
  106. //"garden_id": req.GardenId,
  107. }
  108. err = old.Find(database.DB(), where)
  109. if err != nil && err != gorm.ErrRecordNotFound {
  110. return nil, errors.DataBaseError
  111. }
  112. if old.ID == 0 {
  113. return nil, errors.ErrRecordNotFound
  114. }
  115. reply.Origin = &pb_v1.BuildingUpdateRequest{
  116. Id: req.Id,
  117. GardenId: req.GardenId,
  118. BuildingName: old.BuildingName,
  119. BuildingNumber: old.BuildingNumber,
  120. BuildingArea: old.BuildingArea,
  121. BuildingUsedArea: old.BuildingUsedArea,
  122. }
  123. house := dbmodel.NewHouse(dbname)
  124. where = map[string]interface{}{
  125. "building_id": req.Id,
  126. "status >": HouseStatusWait,
  127. }
  128. count, err := house.Count(database.DB(), where, nil)
  129. if err != nil {
  130. return nil, errors.DataBaseError
  131. }
  132. if count > 0 {
  133. return nil, status.Error(10003, "已入住房屋, 不能修改房屋编号,单元号,楼栋号")
  134. }
  135. houseIds, err := GetHouseIds(req.Id, 0, 0, dbname)
  136. if err != nil {
  137. return nil, err
  138. }
  139. db := database.DB().Begin()
  140. defer func() {
  141. if err != nil {
  142. db.Rollback()
  143. }
  144. }()
  145. // 删楼栋
  146. building := dbmodel.NewBuilding(dbname)
  147. where = map[string]interface{}{
  148. "id": req.Id,
  149. }
  150. err = building.Delete(db, where)
  151. if err != nil {
  152. return nil, errors.DataBaseError
  153. }
  154. // 删楼栋下的单元
  155. unit := dbmodel.NewUnit(dbname)
  156. where = map[string]interface{}{
  157. "building_id": req.Id,
  158. }
  159. err = unit.Delete(db, where)
  160. if err != nil {
  161. return nil, errors.DataBaseError
  162. }
  163. // 删楼栋下的房屋
  164. house = dbmodel.NewHouse(dbname)
  165. where = map[string]interface{}{
  166. "building_id": req.Id,
  167. }
  168. houseCount, err := house.Count(db, where, nil)
  169. if err != nil {
  170. return nil, errors.DataBaseError
  171. }
  172. err = house.Delete(db, where)
  173. if err != nil {
  174. return nil, errors.DataBaseError
  175. }
  176. isBind, err := IsHouseBindCharge(req.GardenId, houseIds, db)
  177. if err != nil {
  178. return nil, err
  179. }
  180. if isBind {
  181. return nil, status.Error(10003, "请先解除房屋绑定费用")
  182. }
  183. mg := dbmodel.NewBuildingManager(dbname)
  184. err = mg.Delete(db, where)
  185. if err != nil {
  186. return nil, errors.DataBaseError
  187. }
  188. mreq := pb_v1.ObjStatisticSetRequest{
  189. ObjType: statistic.ObjTypeHouse,
  190. TotalIncrease: int64(0 - houseCount),
  191. GardenId: req.GardenId,
  192. }
  193. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  194. if err != nil {
  195. return nil, err
  196. }
  197. // 拒绝还未审批的房屋申请
  198. if err = RefuseHouseApply(req.GardenId, old.ID, 0, 0); err != nil {
  199. return nil, err
  200. }
  201. db.Commit()
  202. return reply, nil
  203. }