123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package household
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-garden/errors"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- )
- func checkGardenHouseholdUnitIdsParam(req *pb_v1.GardenHouseholdUnitIdsRequest) error {
- switch {
- case req.GardenId < 1:
- return status.Error(10003, "id不能为空")
- case len(req.Uids) == 0:
- return status.Error(10003, "用户id不能为空")
- }
- return nil
- }
- type UnitIdInfo struct {
- Uid int64 `gorm:"column:uid" json:"uid"`
- UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
- BuildingNumber string `gorm:"column:building_number" json:"building_number"`
- UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
- HouseNumber string `gorm:"column:house_number" json:"house_number"`
- UserType int32 `gorm:"column:user_type" json:"user_type"`
- }
- //
- func GardenHouseholdUnitIds(ctx context.Context, req *pb_v1.GardenHouseholdUnitIdsRequest) (reply *pb_v1.GardenHouseholdUnitIdsReply, err error) {
- reply = &pb_v1.GardenHouseholdUnitIdsReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- err = checkGardenHouseholdUnitIdsParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- p := dbmodel.NewHouseApprovedGarden(dbname)
- where := map[string]interface{}{
- "uid in": req.Uids,
- }
- array := []UnitIdInfo{}
- err = p.SelectList(database.DB(), where, nil, "uid, unit_id, building_number, unit_number, house_number, user_type", -1, -1, &array)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if len(array) == 0 {
- return reply, nil
- }
- uidM := map[int64][]int64{}
- houseNameM := map[int64]string{}
- m := map[string]bool{}
- userType := int32(1)
- for _, v := range array {
- userType = v.UserType
- if houseNameM[v.Uid] == "" {
- houseNameM[v.Uid] = fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
- } else {
- houseNameM[v.Uid] = houseNameM[v.Uid] + ";" + fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
- }
- if m[fmt.Sprintf("%d-%d", v.Uid, v.UnitId)] {
- continue
- }
- uidM[v.Uid] = append(uidM[v.Uid], v.UnitId)
- m[fmt.Sprintf("%d-%d", v.Uid, v.UnitId)] = true
- }
- reply.List = make([]*pb_v1.GardenHouseholdUnitId, len(uidM))
- i := 0
- for k, v := range uidM {
- reply.List[i] = &pb_v1.GardenHouseholdUnitId{Uid: k, UnitIds: v, HouseNames: houseNameM[k], UserType: userType}
- }
- return reply, nil
- }
|