enable.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "property-system/impl/v1/permission"
  11. dbmodel "property-system/model"
  12. pb_v1 "property-system/pb/v1"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkGardenEnableSetParam(req *pb_v1.GardenEnableSetRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区id不能为空")
  23. }
  24. return nil
  25. }
  26. //
  27. func GardenEnableSet(ctx context.Context, req *pb_v1.GardenEnableSetRequest) (reply *pb_v1.GardenEnableSetReply, err error) {
  28. reply = &pb_v1.GardenEnableSetReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. // 参数检查
  42. err = checkGardenEnableSetParam(req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. now := time.Now()
  47. values := map[string]interface{}{
  48. "updated_at": now,
  49. "enable": 1,
  50. }
  51. if !req.Enable {
  52. values["enable"] = 2
  53. }
  54. where := map[string]interface{}{
  55. "id": req.GardenId,
  56. }
  57. p := dbmodel.TGardenApproved{}
  58. err = p.Find(database.DB(), where)
  59. if err != nil && err != gorm.ErrRecordNotFound {
  60. return nil, errors.DataBaseError
  61. }
  62. if p.ID == 0 {
  63. return nil, errors.ErrRecordNotFound
  64. }
  65. err = p.Update(database.DB(), where, values)
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. permission.GlobalPermissionTimeCache(req.GardenId)
  70. return reply, nil
  71. }