del.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package vehicle
  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/park_space"
  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 checkVehicleDelParam(req *pb_v1.VehicleDelRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. case req.Id < 1:
  25. return status.Error(10003, "id 不能为空")
  26. }
  27. return nil
  28. }
  29. func VehicleDel(ctx context.Context, req *pb_v1.VehicleDelRequest) (reply *pb_v1.VehicleDelReply, err error) {
  30. reply = &pb_v1.VehicleDelReply{}
  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. // 参数检查
  44. err = checkVehicleDelParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. // 获取旧数据
  50. old := dbmodel.NewVehicle(dbname)
  51. where := map[string]interface{}{
  52. "id":req.Id,
  53. }
  54. err = old.Find(database.DB(), where)
  55. if err != nil && err != gorm.ErrRecordNotFound {
  56. return nil, errors.DataBaseError
  57. }
  58. if old.ID == 0 {
  59. return nil, errors.ErrRecordNotFound
  60. }
  61. reply.Origin = &pb_v1.VehicleUpdateRequest{
  62. GardenId:req.GardenId,
  63. Id:req.Id,
  64. PlateNo:old.PlateNo,
  65. VehicleType:old.VehicleType,
  66. Vin:old.Vin,
  67. EngineNo:old.EngineNo,
  68. Brand:old.Brand,
  69. Color:old.Color,
  70. SpaceId:old.SpaceId,
  71. HouseholdUid:old.HouseholdUid,
  72. BindType:old.BindType,
  73. Displacement:old.Displacement,
  74. DisplacementUnit:old.DisplacementUnit,
  75. Seat:old.Seat,
  76. }
  77. // 更新数据
  78. veh := dbmodel.NewVehicle(dbname)
  79. db := database.DB().Begin()
  80. defer func() {
  81. if err != nil {
  82. db.Rollback()
  83. }
  84. }()
  85. err = veh.Delete(db, where)
  86. if err != nil {
  87. return nil, errors.DataBaseError
  88. }
  89. mreq := pb_v1.ObjStatisticSetRequest{
  90. ObjType:statistic.ObjTypeVehicle,
  91. TotalIncrease:int64(-1),
  92. GardenId:req.GardenId,
  93. }
  94. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  95. if err != nil {
  96. return nil, err
  97. }
  98. // 更新旧车位数据
  99. if old.SpaceId > 0 {
  100. oldSpace := dbmodel.NewParkSpace(dbname)
  101. where := map[string]interface{}{
  102. "id":old.SpaceId,
  103. "vehicle_id":req.Id,
  104. }
  105. err = oldSpace.Find(db, where)
  106. if err != nil && err != gorm.ErrRecordNotFound {
  107. return nil, errors.DataBaseError
  108. }
  109. if oldSpace.ID == 0 {
  110. db.Commit()
  111. return reply, nil
  112. }
  113. values := map[string]interface{}{
  114. "vehicle_id":0,
  115. }
  116. if oldSpace.HouseId == 0 {
  117. values["status"] = park_space.SpaceStatusEmpty
  118. values["bind_status"] = park_space.SpaceBindStatusEmpty
  119. }
  120. err = oldSpace.Update(db, where, values)
  121. if err != nil {
  122. return nil, errors.DataBaseError
  123. }
  124. }
  125. db.Commit()
  126. return reply, nil
  127. }