// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package house_rent 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" "property-garden/parser" "property-garden/pb" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" ) func checkGardenHouseRentListParam(req *pb_v1.GardenHouseRentListRequest) error { if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } if req.GardenId < 1 { return status.Error(10003, "小区不能为空") } return nil } func BaseConfToBool(data int64) pb_v1.HouseRentBaseConf { conf := pb_v1.HouseRentBaseConf{} one := int64(1) if data&one > 0 { conf.Bed = true } if data&(one<<1) > 0 { conf.Gas = true } if data&(one<<2) > 0 { conf.WarmGas = true } if data&(one<<3) > 0 { conf.Broadband = true } if data&(one<<4) > 0 { conf.Refragerator = true } if data&(one<<5) > 0 { conf.Wardobe = true } if data&(one<<6) > 0 { conf.Sofa = true } if data&(one<<7) > 0 { conf.Aircondition = true } if data&(one<<8) > 0 { conf.Tv = true } if data&(one<<9) > 0 { conf.Heater = true } if data&(one<<10) > 0 { conf.Warshing = true } return conf } func SpecialConfToBool(data int64) pb_v1.HouseRentSpecialConf { conf := pb_v1.HouseRentSpecialConf{} one := int64(1) if data&one > 0 { conf.IntelligentLock = true } if data&(one<<1) > 0 { conf.Wifi = true } if data&(one<<2) > 0 { conf.Metro = true } if data&(one<<3) > 0 { conf.ParkSpace = true } if data&(one<<4) > 0 { conf.IndependentWc = true } if data&(one<<5) > 0 { conf.PrivateBalcony = true } if data&(one<<6) > 0 { conf.FirstRent = true } return conf } func BaseConfToBitmap(conf *pb_v1.HouseRentBaseConf) int64 { ret := int64(0) one := int64(1) if conf.Bed { ret = ret | (one) } if conf.Gas { ret = ret | (one << 1) } if conf.WarmGas { ret = ret | (one << 2) } if conf.Broadband { ret = ret | (one << 3) } if conf.Refragerator { ret = ret | (one << 4) } if conf.Wardobe { ret = ret | (one << 5) } if conf.Sofa { ret = ret | (one << 6) } if conf.Aircondition { ret = ret | (one << 7) } if conf.Tv { ret = ret | (one << 8) } if conf.Heater { ret = ret | (one << 9) } if conf.Warshing { ret = ret | (one << 10) } return ret } func SpecialConfToBitmap(conf *pb_v1.HouseRentSpecialConf) int64 { ret := int64(0) one := int64(1) if conf.IntelligentLock { ret = ret | (one) } if conf.Wifi { ret = ret | (one << 1) } if conf.Metro { ret = ret | (one << 2) } if conf.ParkSpace { ret = ret | (one << 3) } if conf.IndependentWc { ret = ret | (one << 4) } if conf.PrivateBalcony { ret = ret | (one << 5) } if conf.FirstRent { ret = ret | (one << 6) } return ret } func getGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) { ret := map[int64]pb_v1.GardenItem{} if len(ids) == 0 { return ret, nil } mreq := pb_v1.GardenInfosRequest{ Ids: ids, } mreply, err := pb.System.GardenInfos(context.Background(), &mreq) if err != nil { return nil, err } if len(mreply.List) == 0 { return ret, nil } for _, v := range mreply.List { ret[v.Id] = *v } return ret, nil } func getUserInfo(uids []int64, dbname string) (map[int64]dbmodel.THouseholdUser, error) { p := dbmodel.NewHouseholdUser(dbname) where := map[string]interface{}{ "id in": uids, } list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } ret := map[int64]dbmodel.THouseholdUser{} for _, v := range list { ret[v.ID] = v } return ret, nil } // func GardenHouseRentList(ctx context.Context, req *pb_v1.GardenHouseRentListRequest) (reply *pb_v1.GardenHouseRentListReply, err error) { reply = &pb_v1.GardenHouseRentListReply{} // 捕获各个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 = checkGardenHouseRentListParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) p := dbmodel.NewHouseRent(dbname) where := map[string]interface{}{} if req.ApproveStatus > 0 { where["approve_status"] = req.ApproveStatus } if req.SpecialConf > 0 { where["special_conf & ? > 0 "] = req.SpecialConf } if req.BaseConf > 0 { where["base_conf & ? > 0 "] = req.BaseConf } if req.HouseholdUid > 0 { where["household_uid"] = req.HouseholdUid } if req.RoomCount > 0 { where["room_count"] = req.RoomCount } if req.HallCount > 0 { where["hall_count"] = req.HallCount } if req.WcCount > 0 { where["wc_count"] = req.WcCount } if req.StreetCode != "" { where["street_code"] = req.StreetCode } else if req.AreaCode != "" { where["area_code"] = req.AreaCode } else if req.CityCode != "" { where["city_code"] = req.CityCode } else if req.ProvinceCode != "" { where["province_code"] = req.ProvinceCode } if req.RentPriceLess > 0 { where["rent_price <="] = req.RentPriceLess * 100 } if req.RentPriceGreater > 0 { where["rent_price >"] = req.RentPriceGreater * 100 } if req.Contacter != "" { where["contacter"] = req.Contacter } if req.ContacterPhone != "" { where["contact_phone"] = req.ContacterPhone } reply.Page = req.Page reply.Total, err = p.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if reply.Total == 0 { return reply, nil } list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize)) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*pb_v1.HouseRentItem, len(list)) m := map[int64]bool{} uidM := map[int64]bool{} gardenIds := []int64{} uids := []int64{} for _, v := range list { if _, ok := m[v.GardenId]; !ok { m[v.GardenId] = true gardenIds = append(gardenIds, v.GardenId) } if _, ok := uidM[v.HouseholdUid]; !ok && v.HouseholdUid > 0 { uidM[v.HouseholdUid] = true uids = append(uids, v.HouseholdUid) } } userInfos, err := getUserInfo(uids, dbname) if err != nil { return nil, err } gardenInfos, err := getGardenInfos(gardenIds) if err != nil { return nil, errors.DataBaseError } for i, v := range list { item := &pb_v1.HouseRentItem{ HouseName: v.HouseName, HouseId: v.HouseId, Layer: v.Layer, HouseArea: v.HouseArea, Direction: v.Diretion, RoomCount: v.RoomCount, HallCount: v.HallCount, WcCount: v.WcCount, Decorating: v.Decorating, Contacter: v.Contacter, ContactPhone: v.ContactPhone, PayTimeType: v.PayTimeType, RentType: v.RentType, RoomType: v.RoomType, RoomArea: v.RoomArea, RentPrice: v.RentPrice, Desposit: v.Desposit, //InTime:0, ApproveStatus: v.ApproveStatus, ServicePrice: v.ServicePrice, IntermediaryPrice: v.IntermediaryPrice, //BaseConf:, //SpecialConf Desc: v.Desc, HousePic: strings.Split(v.HousePic, ";"), CertPic: strings.Split(v.CertPic, ";"), //HasLift: GardenId: v.GardenId, Id: v.ID, GardenName: v.GardenName, Province: gardenInfos[v.GardenId].Province, City: gardenInfos[v.GardenId].City, Street: gardenInfos[v.GardenId].Street, Area: gardenInfos[v.GardenId].Area, Lat: v.Lat, Lnt: v.Lnt, Addr: gardenInfos[v.GardenId].GardenAddr, InTime: v.InTime, GardenDesc: gardenInfos[v.GardenId].GardenDesc, HouseholdUid: v.HouseholdUid, RealName: userInfos[v.HouseholdUid].RealName, Phone: userInfos[v.HouseholdUid].Phone, } if v.HasLift == 1 { item.HasLift = true } //bconf := BaseConfToBool(v.BaseConf) item.BaseConf = v.BaseConf //sconf := SpecialConfToBool(v.SpecialConf) item.SpecialConf = v.SpecialConf if v.HousePic == "" { item.HousePic = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.RentObj} } reply.List[i] = item } return reply, nil }