add.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/park_space"
  11. "property-garden/impl/v1/statistic"
  12. dbmodel "property-garden/model"
  13. pb_v1 "property-garden/pb/v1"
  14. "property-garden/utils"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. const (
  22. BindTypeNormal = 1
  23. BindTypeSell = 2
  24. BindTypeRent = 3
  25. )
  26. func checkVehicleAddParam(req *pb_v1.VehicleAddRequest) error {
  27. if req.SpaceId == 0 {
  28. //req.BindType = BindTypeNormal
  29. }
  30. switch {
  31. case req.GardenId == 0:
  32. return status.Error(10003, "小区不能为空")
  33. case req.PlateNo == "":
  34. return status.Error(10003, "车牌号不能为空")
  35. case req.Displacement < 1:
  36. //return status.Error(10003, "排量不能为空")
  37. case req.DisplacementUnit == "":
  38. //return status.Error(10003, "排量单位不能为空")
  39. case req.Seat < 1:
  40. //return status.Error(10003, "座位数不能为空")
  41. case req.BindType < 1:
  42. return status.Error(10003, "绑定类型不能为空")
  43. case req.SpaceId > 0 && req.BindType == BindTypeNormal:
  44. return status.Error(10003, "绑定类型错误")
  45. case req.HouseholdUid == 0:
  46. return status.Error(10003, "未绑定住户")
  47. case req.BindType == BindTypeSell && req.SpaceId == 0:
  48. return status.Error(10003, "出售类型请绑定车位")
  49. case req.BindType == BindTypeRent && req.SpaceId == 0:
  50. return status.Error(10003, "出租类型请绑定车位")
  51. }
  52. return nil
  53. }
  54. func checkHouseholdHouse(uid int64, houseId int64, dbname string) (bool, error) {
  55. p := dbmodel.NewHouseApprovedGarden(dbname)
  56. where := map[string]interface{}{
  57. "uid": uid,
  58. "house_id": houseId,
  59. }
  60. count, err := p.Count(database.DB(), where, nil)
  61. if err != nil {
  62. return false, errors.DataBaseError
  63. }
  64. if count > 0 {
  65. return true, nil
  66. }
  67. return false, nil
  68. }
  69. func VehicleAdd(ctx context.Context, req *pb_v1.VehicleAddRequest) (reply *pb_v1.VehicleAddReply, err error) {
  70. reply = &pb_v1.VehicleAddReply{}
  71. // 捕获各个task中的异常并返回给调用者
  72. defer func() {
  73. if r := recover(); r != nil {
  74. err = fmt.Errorf("%+v", r)
  75. e := &status.Status{}
  76. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  77. logger.Error("err",
  78. zap.String("system_err", err.Error()),
  79. zap.Stack("stacktrace"))
  80. }
  81. }
  82. }()
  83. // 参数检查
  84. err = checkVehicleAddParam(req)
  85. if err != nil {
  86. return nil, err
  87. }
  88. now := time.Now()
  89. dbname := utils.GetGardenDbName(req.GardenId)
  90. bindType := req.BindType
  91. space := dbmodel.NewParkSpace(dbname)
  92. if req.SpaceId > 0 {
  93. where := map[string]interface{}{
  94. "id": req.SpaceId,
  95. }
  96. err = space.Find(database.DB(), where)
  97. if err != nil && err != gorm.ErrRecordNotFound {
  98. return nil, errors.DataBaseError
  99. }
  100. if space.ID == 0 {
  101. return nil, status.Error(10003, "车位不存在")
  102. }
  103. if space.VehicleId > 0 {
  104. return nil, status.Error(10003, "车位已绑定车辆")
  105. }
  106. if req.BindType == BindTypeSell && space.HouseId == 0 {
  107. return nil, status.Error(10003, "出售类型请先在车位配置中绑定房屋")
  108. }
  109. if space.HouseId > 0 {
  110. if req.BindType != BindTypeSell {
  111. return nil, status.Error(10003, "车位已出售,不能出租")
  112. }
  113. householdHasThisHouse, err := checkHouseholdHouse(req.HouseholdUid, space.HouseId, dbname)
  114. if err != nil {
  115. return nil, err
  116. }
  117. if !householdHasThisHouse {
  118. return nil, status.Error(10003, "车位已绑定房屋,该用户没有入驻该房屋")
  119. }
  120. }
  121. }
  122. if req.SpaceId == 0 {
  123. bindType = BindTypeNormal
  124. }
  125. veh := dbmodel.TVehicle{
  126. PlateNo: req.PlateNo,
  127. VehicleType: req.VehicleType,
  128. Vin: req.Vin,
  129. EngineNo: req.EngineNo,
  130. Brand: req.Brand,
  131. Color: req.Color,
  132. SpaceId: req.SpaceId,
  133. ParkId: space.ParkId,
  134. HouseholdUid: req.HouseholdUid,
  135. BindType: bindType,
  136. Displacement: req.Displacement,
  137. DisplacementUnit: req.DisplacementUnit,
  138. Seat: req.Seat,
  139. CreatedAt: now,
  140. UpdatedAt: now,
  141. }
  142. db := database.DB().Begin()
  143. defer func() {
  144. if err != nil {
  145. db.Rollback()
  146. }
  147. }()
  148. veh.SetTable(dbname)
  149. err = veh.Insert(db)
  150. if err != nil {
  151. return nil, errors.DataBaseError
  152. }
  153. if space.ID > 0 {
  154. where := map[string]interface{}{
  155. "id": req.SpaceId,
  156. }
  157. values := map[string]interface{}{
  158. "vehicle_id": veh.ID,
  159. }
  160. if bindType == BindTypeSell || bindType == BindTypeRent {
  161. values["status"] = bindType
  162. values["bind_status"] = park_space.SpaceBindStatusBind
  163. }
  164. err = space.Update(db, where, values)
  165. if err != nil {
  166. return nil, errors.DataBaseError
  167. }
  168. }
  169. mreq := pb_v1.ObjStatisticSetRequest{
  170. ObjType: statistic.ObjTypeVehicle,
  171. TotalIncrease: int64(1),
  172. GardenId: req.GardenId,
  173. }
  174. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  175. if err != nil {
  176. return nil, err
  177. }
  178. db.Commit()
  179. reply.Id = veh.ID
  180. return reply, nil
  181. }