unit_list.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package gate_unit
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "property-device/errors"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. )
  14. func checkGateUnitListParam(req *pb_v1.GateUnitListRequest) error {
  15. switch {
  16. case req.GardenId == 0:
  17. return status.Error(10003, "小区不能为空")
  18. case req.DeviceId == 0:
  19. return status.Error(10003, "设备不能为空")
  20. }
  21. return nil
  22. }
  23. func GateUnitList(ctx context.Context, req *pb_v1.GateUnitListRequest) (reply *pb_v1.GateUnitListReply, err error) {
  24. reply = &pb_v1.GateUnitListReply{}
  25. // 捕获各个task中的异常并返回给调用者
  26. defer func() {
  27. if r := recover(); r != nil {
  28. err = fmt.Errorf("%+v", r)
  29. e := &status.Status{}
  30. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  31. logger.Error("err",
  32. zap.String("system_err", err.Error()),
  33. zap.Stack("stacktrace"))
  34. }
  35. }
  36. }()
  37. err = checkGateUnitListParam(req)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if err != nil {
  42. return nil, err
  43. }
  44. p := &dbmodel.TGateUnit{}
  45. where := [][2]interface{}{}
  46. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  47. where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
  48. list, err := p.List(database.DB(), where, nil, -1, -1)
  49. if err != nil {
  50. return nil, errors.DataBaseError
  51. }
  52. reply.UnitId = make([]int64, len(list))
  53. for i, v := range list {
  54. reply.UnitId[i] = v.UnitId
  55. }
  56. return reply, nil
  57. }