123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- // 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
- }
|