del.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package park_space
  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. pb_v1 "property-garden/pb/v1"
  18. "property-garden/utils"
  19. )
  20. func getVehicleInfo(vehicleId int64, dbname string) (dbmodel.TVehicle, error) {
  21. p := dbmodel.NewVehicle(dbname)
  22. where := map[string]interface{}{
  23. "id":vehicleId,
  24. //"garden_id":req.GardenId,
  25. }
  26. err := p.Find(database.DB(), where)
  27. if err != nil && err != gorm.ErrRecordNotFound{
  28. return *p, errors.DataBaseError
  29. }
  30. return *p, nil
  31. }
  32. /*
  33. func isSpaceBindCharge(id int64, dbname string) (bool, error) {
  34. p := dbmodel.NewChargeSpace(dbname)
  35. where := map[string]interface{}{
  36. "space_id":id,
  37. }
  38. count, err := p.Count(database.DB(), where, nil)
  39. if err != nil {
  40. return false, errors.DataBaseError
  41. }
  42. if count > 0 {
  43. return true, nil
  44. }
  45. return false, nil
  46. }
  47. */
  48. //
  49. func ParkSpaceDel(ctx context.Context, req *pb_v1.ParkSpaceDelRequest) (reply *pb_v1.ParkSpaceDelReply, err error) {
  50. reply = &pb_v1.ParkSpaceDelReply{}
  51. // 捕获各个task中的异常并返回给调用者
  52. defer func() {
  53. if r := recover(); r != nil {
  54. err = fmt.Errorf("%+v", r)
  55. e := &status.Status{}
  56. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  57. logger.Error("err",
  58. zap.String("system_err", err.Error()),
  59. zap.Stack("stacktrace"))
  60. }
  61. }
  62. }()
  63. // 参数检查
  64. if req.Id < 1 || req.GardenId < 1 {
  65. return nil, errors.ParamsError
  66. }
  67. dbname := utils.GetGardenDbName(req.GardenId)
  68. p := dbmodel.NewParkSpace(dbname)
  69. where := map[string]interface{}{
  70. "id":req.Id,
  71. //"garden_id":req.GardenId,
  72. }
  73. // 原数据
  74. old := dbmodel.NewParkSpace(dbname)
  75. err = old.Find(database.DB(), where)
  76. if err != nil && err != gorm.ErrRecordNotFound{
  77. return nil, errors.DataBaseError
  78. }
  79. if old.ID == 0 {
  80. return nil, errors.ErrRecordNotFound
  81. }
  82. isBind, err := charge_utils.IsObjBindCharge(req.Id, charge_utils.ObjTypeSpace, dbname)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if isBind {
  87. return nil, status.Error(10003, "该车位已绑定费用,请先解除绑定")
  88. }
  89. if old.VehicleId > 0 {
  90. vinfo, err := getVehicleInfo(old.VehicleId, dbname)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if vinfo.ID > 0 {
  95. return nil, status.Error(10003, fmt.Sprintf("请先在车辆%s中解除对该车位的绑定", vinfo.PlateNo))
  96. }
  97. }
  98. reply.Origin = &pb_v1.ParkSpaceUpdateRequest{
  99. Id:req.Id,
  100. ParkId:old.ParkId,
  101. Comment:old.Comment,
  102. GardenId:req.GardenId,
  103. SpaceArea:old.SpaceArea,
  104. SpaceType:old.SpaceType,
  105. SpaceNumber:old.SpaceNumber,
  106. }
  107. // 删除车位
  108. db := database.DB().Begin()
  109. err = p.Delete(db, where)
  110. if err != nil {
  111. db.Rollback()
  112. return nil, errors.DataBaseError
  113. }
  114. // 更新停车场车位数
  115. park := dbmodel.NewPark(dbname)
  116. where = map[string]interface{}{
  117. "id":old.ParkId,
  118. }
  119. values := map[string]interface{}{
  120. "space_count":gorm.Expr("space_count - ?", 1),
  121. }
  122. err = park.Update(db, where, values)
  123. if err != nil {
  124. db.Rollback()
  125. return nil, errors.DataBaseError
  126. }
  127. mreq := pb_v1.ObjStatisticSetRequest{
  128. ObjType:statistic.ObjTypeSpace,
  129. TotalIncrease:int64(-1),
  130. GardenId:req.GardenId,
  131. }
  132. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  133. if err != nil {
  134. db.Rollback()
  135. return nil, err
  136. }
  137. db.Commit()
  138. return reply, nil
  139. }