add.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "gorm.io/gorm"
  9. "property-garden/errors"
  10. "property-garden/impl/v1/statistic"
  11. dbmodel "property-garden/model"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "strings"
  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. SpaceStatusEmpty = 1
  23. SpaceStatusSell = 2
  24. SpaceStatusRent = 3
  25. )
  26. const (
  27. SpaceBindStatusEmpty = 1
  28. SpaceBindStatusBind = 2
  29. )
  30. func checkParkSpaceAddParam(req *pb_v1.ParkSpaceAddRequest) error {
  31. switch {
  32. case req.ParkId < 1:
  33. return status.Error(10003, "停车场不能为空")
  34. case req.SpaceNumber == "":
  35. return status.Error(10003, "车位编号不能为空")
  36. case req.SpaceType < 1:
  37. return status.Error(10003, "车位类型不能为空")
  38. case req.GardenId < 1:
  39. return status.Error(10003, "小区不能为空")
  40. case req.SpaceArea == 0:
  41. return status.Error(10003, "车位面积不能为空")
  42. }
  43. return nil
  44. }
  45. //
  46. func ParkSpaceAdd(ctx context.Context, req *pb_v1.ParkSpaceAddRequest) (reply *pb_v1.ParkSpaceAddReply, err error) {
  47. reply = &pb_v1.ParkSpaceAddReply{}
  48. // 捕获各个task中的异常并返回给调用者
  49. defer func() {
  50. if r := recover(); r != nil {
  51. err = fmt.Errorf("%+v", r)
  52. e := &status.Status{}
  53. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  54. logger.Error("err",
  55. zap.String("system_err", err.Error()),
  56. zap.Stack("stacktrace"))
  57. }
  58. }
  59. }()
  60. // 参数检查
  61. err = checkParkSpaceAddParam(req)
  62. if err != nil {
  63. return nil, err
  64. }
  65. dbname := utils.GetGardenDbName(req.GardenId)
  66. fmt.Printf("xxxxxxx:%v,%v\n", dbname, req.GardenId)
  67. park := dbmodel.NewPark(dbname)
  68. where := map[string]interface{}{
  69. "id":req.ParkId,
  70. //"garden_id":req.GardenId,
  71. }
  72. err = park.Find(database.DB(), where)
  73. if err != nil && err != gorm.ErrRecordNotFound{
  74. return nil, errors.DataBaseError
  75. }
  76. if park.ID == 0 {
  77. return nil, errors.ErrRecordNotFound
  78. }
  79. now := time.Now()
  80. p := &dbmodel.TParkSpace{
  81. ParkId:req.ParkId,
  82. SpaceType:req.SpaceType,
  83. SpaceNumber:req.SpaceNumber,
  84. SpaceArea:req.SpaceArea,
  85. Comment:req.Comment,
  86. UpdatedAt:now,
  87. CreatedAt:now,
  88. Status:SpaceStatusEmpty,
  89. BindStatus:SpaceBindStatusEmpty,
  90. HouseId:req.HouseId,
  91. }
  92. if req.HouseId > 0 {
  93. p.Status = SpaceStatusSell
  94. p.BindStatus = SpaceBindStatusBind
  95. }
  96. p.SetTable(dbname)
  97. db := database.DB().Begin()
  98. err = p.Insert(db)
  99. if err != nil {
  100. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  101. db.Rollback()
  102. return nil, status.Error(10003, "该车位已存在")
  103. }
  104. db.Rollback()
  105. return nil, errors.DataBaseError
  106. }
  107. // 更新车位数
  108. park = dbmodel.NewPark(dbname)
  109. where = map[string]interface{}{
  110. "id":req.ParkId,
  111. }
  112. values := map[string]interface{}{
  113. "space_count":gorm.Expr("space_count + ?", 1),
  114. }
  115. err = park.Update(db, where, values)
  116. if err != nil {
  117. db.Rollback()
  118. return nil, errors.DataBaseError
  119. }
  120. mreq := pb_v1.ObjStatisticSetRequest{
  121. ObjType:statistic.ObjTypeSpace,
  122. TotalIncrease:int64(1),
  123. GardenId:req.GardenId,
  124. }
  125. err = statistic.ObjStatisticSetWithDb(&mreq, db)
  126. if err != nil {
  127. db.Rollback()
  128. return nil, err
  129. }
  130. reply.Id = p.ID
  131. db.Commit()
  132. return reply, nil
  133. }