can_add.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "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-company/consts"
  14. "property-company/errors"
  15. dbmodel "property-company/model"
  16. pb_v1 "property-company/pb/v1"
  17. )
  18. func checkGardenCanAddParam(req *pb_v1.CompanyCanAddGardenRequest) error {
  19. switch {
  20. case req.Cid == 0:
  21. return status.Error(10003, "公司id不能为空")
  22. }
  23. return nil
  24. }
  25. //
  26. func CompanyCanAddGarden(ctx context.Context, req *pb_v1.CompanyCanAddGardenRequest) (reply *pb_v1.CompanyCanAddGardenReply, err error) {
  27. reply = &pb_v1.CompanyCanAddGardenReply{}
  28. // 捕获各个task中的异常并返回给调用者
  29. defer func() {
  30. if r := recover(); r != nil {
  31. err = fmt.Errorf("%+v", r)
  32. e := &status.Status{}
  33. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  34. logger.Error("err",
  35. zap.String("system_err", err.Error()),
  36. zap.Stack("stacktrace"))
  37. }
  38. }
  39. }()
  40. // 检查参数
  41. err = checkGardenCanAddParam(req)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // 检查公司
  46. company := dbmodel.TCompany{}
  47. where := map[string]interface{}{
  48. "id": req.Cid,
  49. }
  50. err = company.Find(database.DB(), where)
  51. if err != nil && err != gorm.ErrRecordNotFound {
  52. return nil, errors.DataBaseError
  53. }
  54. if company.ID == 0 {
  55. return nil, errors.UserNotExist
  56. }
  57. if company.ApproveStatus != consts.ApproveStatusSuccess {
  58. return nil, errors.UserStatusError
  59. }
  60. //if company.FreeGardenCount >= 0 && company.GardenCount >= company.FreeGardenCount {
  61. // return reply, nil
  62. //}
  63. reply.CanAdd = true
  64. return reply, nil
  65. }