del.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package park
  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. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. )
  19. /*
  20. func isParkBindCharge(id int64, dbname string) (bool, error) {
  21. p := dbmodel.NewChargeSpace(dbname)
  22. where := map[string]interface{}{
  23. "park_id":id,
  24. }
  25. count, err := p.Count(database.DB(), where, nil)
  26. if err != nil {
  27. return false, errors.DataBaseError
  28. }
  29. if count > 0 {
  30. return true, nil
  31. }
  32. return false, nil
  33. }
  34. */
  35. //
  36. func ParkDel(ctx context.Context, req *pb_v1.ParkDelRequest) (reply *pb_v1.ParkDelReply, err error) {
  37. reply = &pb_v1.ParkDelReply{}
  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. // 参数检查
  51. if req.Id < 1 || req.GardenId < 1 {
  52. return nil, errors.ParamsError
  53. }
  54. dbname := utils.GetGardenDbName(req.GardenId)
  55. p := dbmodel.NewPark(dbname)
  56. where := map[string]interface{}{
  57. "id":req.Id,
  58. //"garden_id":req.GardenId,
  59. }
  60. old := dbmodel.NewPark(dbname)
  61. err = old.Find(database.DB(), where)
  62. if err != nil && err != gorm.ErrRecordNotFound{
  63. return nil, errors.DataBaseError
  64. }
  65. if old.ID == 0 {
  66. return nil, errors.ErrRecordNotFound
  67. }
  68. // todo
  69. //isBind, err := isParkBindCharge(req.Id, dbname)
  70. //if err != nil {
  71. // return nil, err
  72. //}
  73. //if isBind {
  74. // return nil, status.Error(10003, "该区域有车位已绑定费用,请先解除绑定")
  75. //}
  76. reply.Origin = &pb_v1.ParkUpdateRequest{
  77. Id:req.Id,
  78. ParkType:old.ParkType,
  79. ParkNumber:old.ParkNumber,
  80. ParkName:old.ParkName,
  81. Comment:old.Comment,
  82. GardenId:req.GardenId,
  83. }
  84. db := database.DB().Begin()
  85. err = p.Delete(db, where)
  86. if err != nil {
  87. db.Rollback()
  88. return nil, errors.DataBaseError
  89. }
  90. space := dbmodel.NewParkSpace(dbname)
  91. where = map[string]interface{}{
  92. "park_id":req.Id,
  93. }
  94. list, _ := space.List(database.DB(), where, nil, -1, -1)
  95. ids := make([]int64, len(list))
  96. for i, v := range list {
  97. if v.VehicleId > 0 {
  98. return nil, status.Error(10003, "有车辆绑定车位,请先解除绑定")
  99. }
  100. ids[i] = v.ID
  101. }
  102. if len(ids) > 0 {
  103. isBind, err := charge_utils.IsObjsBindCharge(ids, charge_utils.ObjTypeSpace, dbname)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if isBind {
  108. return nil, status.Error(10003, "有车位已绑定费用,请先解除绑定")
  109. }
  110. }
  111. err = space.Delete(db, where)
  112. if err != nil {
  113. db.Rollback()
  114. return nil, errors.DataBaseError
  115. }
  116. db.Commit()
  117. return reply, nil
  118. }