house_limit.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package garden
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-system/errors"
  10. dbmodel "property-system/model"
  11. pb_v1 "property-system/pb/v1"
  12. "git.getensh.com/common/gopkgs/database"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. //
  18. func GardenHouseCountLimit(ctx context.Context, req *pb_v1.GardenHouseCountLimitRequest) (reply *pb_v1.GardenHouseCountLimitReply, err error) {
  19. reply = &pb_v1.GardenHouseCountLimitReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.GardenId == 0 {
  33. return nil, errors.ParamsError
  34. }
  35. garden := &dbmodel.TGardenApproved{}
  36. where := map[string]interface{}{
  37. "in_use": true,
  38. "id": req.GardenId,
  39. }
  40. err = garden.Find(database.DB(), where)
  41. if err != nil && err != gorm.ErrRecordNotFound {
  42. return nil, errors.DataBaseError
  43. }
  44. if garden.ID == 0 {
  45. return nil, errors.ErrRecordNotFound
  46. }
  47. reply.HouseCountLimit = garden.HouseCountLimit
  48. reply.HouseCount = garden.HouseCount
  49. return reply, nil
  50. }