uids_from_unitid.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package household
  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. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. func checkGardenHouseholdUidsFromUnitIdParam(req *pb_v1.GardenHouseholdUidsFromUnitIdRequest) error {
  18. switch {
  19. case req.GardenId < 1:
  20. return status.Error(10003, "id不能为空")
  21. case len(req.UnitIds) == 0:
  22. return status.Error(10003, "单元id不能为空")
  23. }
  24. return nil
  25. }
  26. type UidInfo struct {
  27. Uid int64 `gorm:"column:uid" json:"uid"`
  28. }
  29. //
  30. func GardenHouseholdUidsFromUnitId(ctx context.Context, req *pb_v1.GardenHouseholdUidsFromUnitIdRequest) (reply *pb_v1.GardenHouseholdUidsFromUnitIdReply, err error) {
  31. reply = &pb_v1.GardenHouseholdUidsFromUnitIdReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. err = checkGardenHouseholdUidsFromUnitIdParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. p := dbmodel.NewHouseApprovedGarden(dbname)
  50. where := map[string]interface{}{
  51. "unit_id in": req.UnitIds,
  52. }
  53. array := []UidInfo{}
  54. err = p.SelectGroup(database.DB(), where, nil, "uid", "uid", -1, -1, &array)
  55. if err != nil {
  56. return nil, errors.DataBaseError
  57. }
  58. if len(array) == 0 {
  59. return reply, nil
  60. }
  61. reply.Uids = make([]int64, len(array))
  62. for i, v := range array {
  63. reply.Uids[i] = v.Uid
  64. }
  65. return reply, nil
  66. }