// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package vehicle import ( "context" "encoding/json" "fmt" "property-garden/errors" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-garden/parser" ) func checkVehicleListParam(req *pb_v1.VehicleListRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") } if req.PageSize == 0 { req.PageSize = 10 } if req.Page == 0 { req.Page = 1 } return nil } func getHouseholdInfos(uids []int64, dbname string) (map[int64]dbmodel.THouseApprovedGarden, error) { p := dbmodel.NewHouseApprovedGarden(dbname) ret := map[int64]dbmodel.THouseApprovedGarden{} if len(uids) == 0 { return ret, nil } where := map[string]interface{}{ "uid in": uids, } list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } for _, v := range list { ret[v.Uid] = v } return ret, nil } func getSpaceInfos(spaceIds []int64, dbname string) (map[int64]dbmodel.TSpacePark, error) { p := dbmodel.NewSpacePark(dbname) ret := map[int64]dbmodel.TSpacePark{} if len(spaceIds) == 0 { return ret, nil } where := map[string]interface{}{ "id in": spaceIds, } list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } for _, v := range list { ret[v.ID] = v } return ret, nil } func VehicleList(ctx context.Context, req *pb_v1.VehicleListRequest) (reply *pb_v1.VehicleListReply, err error) { reply = &pb_v1.VehicleListReply{} // 捕获各个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 = checkVehicleListParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) p := dbmodel.NewVehicle(dbname) where := map[string]interface{}{ //"garden_id":req.GardenId, } if req.VehicleType > 0 { where["vehicle_type"] = req.VehicleType } if req.PlateNo != "" { where["plate_no"] = req.PlateNo } if req.BindType > 0 { where["bind_type"] = req.BindType } if req.HouseholdUid > 0 { where["household_uid"] = req.HouseholdUid } 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 } householdUids := []int64{} spaceIds := []int64{} for _, v := range list { if v.HouseholdUid > 0 { householdUids = append(householdUids, v.HouseholdUid) } if v.SpaceId > 0 { spaceIds = append(spaceIds, v.SpaceId) } } householdInfoM, err := getHouseholdInfos(householdUids, dbname) if err != nil { return nil, err } spackeInfoM, err := getSpaceInfos(spaceIds, dbname) if err != nil { return nil, err } reply.List = make([]*pb_v1.VehicleItem, len(list)) for i, v := range list { item := &pb_v1.VehicleItem{ PlateNo: v.PlateNo, VehicleType: v.VehicleType, Vin: v.Vin, EngineNo: v.EngineNo, Brand: v.Brand, Color: v.Color, SpaceId: v.SpaceId, HouseholdUid: v.HouseholdUid, BindType: v.BindType, Displacement: v.Displacement, DisplacementUnit: v.DisplacementUnit, Seat: v.Seat, Id: v.ID, HouseholdPhone: householdInfoM[v.HouseholdUid].Phone, HouseholdName: householdInfoM[v.HouseholdUid].Name, ParkId: v.ParkId, ParkNumber: spackeInfoM[v.SpaceId].ParkNumber, ParkName: spackeInfoM[v.SpaceId].ParkName, SpaceNumber: spackeInfoM[v.SpaceId].SpaceNumber, } if utils.BrandMap[v.Brand] { if v.Brand != "其他" { item.BrandPic = parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.BrandBucket + "/" + v.Brand + ".png" } } reply.List[i] = item } return reply, nil }