// 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-household/errors" dbmodel "property-household/model" "property-household/parser" "property-household/pb" pb_v1 "property-household/pb/v1" "strings" ) func checkHouseRentListParam(req *pb_v1.HouseRentListRequest) error { if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } return nil } func houseRentListByGarden(ctx context.Context, req *pb_v1.HouseRentListRequest) (reply *pb_v1.HouseRentListReply, err error) { mreq := pb_v1.GardenHouseRentListRequest{ GardenId: req.GardenId, ProvinceCode: req.ProvinceCode, CityCode: req.CityCode, AreaCode: req.AreaCode, StreetCode: req.StreetCode, RoomCount: req.RoomCount, HallCount: req.HallCount, WcCount: req.WcCount, HouseholdUid: req.HouseholdUid, RentPriceGreater: req.RentPriceGreater, RentPriceLess: req.RentPriceLess, ApproveStatus: req.ApproveStatus, Page: req.Page, PageSize: req.PageSize, BaseConf: req.BaseConf, SpecialConf: req.SpecialConf, } mreply, err := pb.Garden.GardenHouseRentList(context.Background(), &mreq) if err != nil { return nil, err } reply = &pb_v1.HouseRentListReply{ Page: mreply.Page, Total: mreply.Total, List: mreply.List, } return reply, nil } 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 HouseRentList(ctx context.Context, req *pb_v1.HouseRentListRequest) (reply *pb_v1.HouseRentListReply, err error) { reply = &pb_v1.HouseRentListReply{} // 捕获各个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 = checkHouseRentListParam(req) if err != nil { return nil, err } if req.GardenId > 0 { return houseRentListByGarden(ctx, req) } p := dbmodel.THouseRent{} where := map[string]interface{}{} if len(req.GardenIds) > 0 { where["garden_id in"] = req.GardenIds } 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 { if req.RoomCount > 4 { where["room_count >"] = 4 } else { where["room_count"] = req.RoomCount } } if req.HallCount > 0 { if req.HallCount > 4 { where["hall_count >"] = 4 } else { 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 } where["approve_status !="] = 4 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.ListByJoin(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{} gardenIds := []int64{} for _, v := range list { if _, ok := m[v.GardenId]; !ok { m[v.GardenId] = true gardenIds = append(gardenIds, v.GardenId) } } gardenInfos, err := getGardenInfos(gardenIds) if err != nil { return nil, errors.DataBaseError } for i, v := range list { item := &pb_v1.HouseRentItem{ HouseName: v.HouseName, 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, HouseId: v.HouseId, InTime: v.InTime, GardenDesc: gardenInfos[v.GardenId].GardenDesc, } 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 }