household_unitid.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 checkGardenHouseholdUnitIdsParam(req *pb_v1.GardenHouseholdUnitIdsRequest) error {
  18. switch {
  19. case req.GardenId < 1:
  20. return status.Error(10003, "id不能为空")
  21. case len(req.Uids) == 0:
  22. return status.Error(10003, "用户id不能为空")
  23. }
  24. return nil
  25. }
  26. type UnitIdInfo struct {
  27. Uid int64 `gorm:"column:uid" json:"uid"`
  28. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  29. BuildingNumber string `gorm:"column:building_number" json:"building_number"`
  30. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  31. HouseNumber string `gorm:"column:house_number" json:"house_number"`
  32. UserType int32 `gorm:"column:user_type" json:"user_type"`
  33. }
  34. //
  35. func GardenHouseholdUnitIds(ctx context.Context, req *pb_v1.GardenHouseholdUnitIdsRequest) (reply *pb_v1.GardenHouseholdUnitIdsReply, err error) {
  36. reply = &pb_v1.GardenHouseholdUnitIdsReply{}
  37. // 捕获各个task中的异常并返回给调用者
  38. defer func() {
  39. if r := recover(); r != nil {
  40. err = fmt.Errorf("%+v", r)
  41. e := &status.Status{}
  42. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  43. logger.Error("err",
  44. zap.String("system_err", err.Error()),
  45. zap.Stack("stacktrace"))
  46. }
  47. }
  48. }()
  49. err = checkGardenHouseholdUnitIdsParam(req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. dbname := utils.GetGardenDbName(req.GardenId)
  54. p := dbmodel.NewHouseApprovedGarden(dbname)
  55. where := map[string]interface{}{
  56. "uid in": req.Uids,
  57. }
  58. array := []UnitIdInfo{}
  59. err = p.SelectList(database.DB(), where, nil, "uid, unit_id, building_number, unit_number, house_number, user_type", -1, -1, &array)
  60. if err != nil {
  61. return nil, errors.DataBaseError
  62. }
  63. if len(array) == 0 {
  64. return reply, nil
  65. }
  66. uidM := map[int64][]int64{}
  67. houseNameM := map[int64]string{}
  68. m := map[string]bool{}
  69. userType := int32(1)
  70. for _, v := range array {
  71. userType = v.UserType
  72. if houseNameM[v.Uid] == "" {
  73. houseNameM[v.Uid] = fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
  74. } else {
  75. houseNameM[v.Uid] = houseNameM[v.Uid] + ";" + fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
  76. }
  77. if m[fmt.Sprintf("%d-%d", v.Uid, v.UnitId)] {
  78. continue
  79. }
  80. uidM[v.Uid] = append(uidM[v.Uid], v.UnitId)
  81. m[fmt.Sprintf("%d-%d", v.Uid, v.UnitId)] = true
  82. }
  83. reply.List = make([]*pb_v1.GardenHouseholdUnitId, len(uidM))
  84. i := 0
  85. for k, v := range uidM {
  86. reply.List[i] = &pb_v1.GardenHouseholdUnitId{Uid: k, UnitIds: v, HouseNames: houseNameM[k], UserType: userType}
  87. }
  88. return reply, nil
  89. }