123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package park_space
- 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"
- "property-garden/impl/v1/charge_utils"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- )
- 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 getHouseholdHouseIds(uid int64, dbname string) ([]int64, error) {
- where := map[string]interface{}{
- "uid": uid,
- }
- h := dbmodel.NewHouseApprovedGarden(dbname)
- hlist, err := h.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- hids := make([]int64, len(hlist))
- for i, v := range hlist {
- hids[i] = v.HouseId
- }
- return hids, nil
- }
- func GetVehicleInfos(ids []int64, dbname string) (map[int64]dbmodel.TVehicle, error) {
- ret := map[int64]dbmodel.TVehicle{}
- if len(ids) == 0 {
- return ret, nil
- }
- p := dbmodel.NewVehicle(dbname)
- where := map[string]interface{}{
- "id in": ids,
- }
- 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 ParkSpaceList(ctx context.Context, req *pb_v1.ParkSpaceListRequest) (reply *pb_v1.ParkSpaceListReply, err error) {
- reply = &pb_v1.ParkSpaceListReply{}
- // 捕获各个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"))
- }
- }
- }()
- if req.GardenId < 1 {
- return nil, errors.ParamsError
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- p := dbmodel.NewSpacePark(dbname)
- where := map[string]interface{}{
- //"garden_id":req.GardenId,
- }
- if req.SpaceType > 0 {
- where["space_type"] = req.SpaceType
- }
- if req.ParkId > 0 {
- where["park_id"] = req.ParkId
- }
- if req.SpaceNumber != "" {
- where["space_number"] = req.SpaceNumber
- }
- if req.HouseId > 0 {
- where["house_id"] = req.HouseId
- }
- or := map[string]interface{}{}
- if req.BindType == BindTypeRent {
- or["bind_status"] = SpaceBindStatusEmpty
- if req.BindSpaceId > 0 {
- or["id"] = req.BindSpaceId
- }
- }
- // 查找业主购买的车位
- if req.BindType == BindTypeSell {
- if req.HouseholdUid < 0 {
- return nil, status.Error(10003, "搜索已售车位,业主uid不能为空")
- }
- hids, err := getHouseholdHouseIds(req.HouseholdUid, dbname)
- if err != nil {
- return nil, err
- }
- if len(hids) == 0 {
- return nil, status.Error(10003, "未找到该业主房屋")
- }
- where["house_id in"] = hids
- }
- reply.Page = req.Page
- reply.Total, err = p.Count(database.DB(), where, or)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := p.List(database.DB(), where, or, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = make([]*pb_v1.ParkSpaceItem, len(list))
- houseIds := []int64{}
- vehicleIds := []int64{}
- for _, v := range list {
- if v.HouseId > 0 {
- houseIds = append(houseIds, v.HouseId)
- }
- if v.VehicleId > 0 {
- vehicleIds = append(vehicleIds, v.VehicleId)
- }
- }
- houseInfoM, err := charge_utils.GetHouseInfos(houseIds, dbname)
- if err != nil {
- return nil, err
- }
- vehicleInfoM, err := GetVehicleInfos(vehicleIds, dbname)
- if err != nil {
- return nil, err
- }
- for i, v := range list {
- reply.List[i] = &pb_v1.ParkSpaceItem{
- Id: v.ID,
- SpaceNumber: v.SpaceNumber,
- SpaceType: v.SpaceType,
- SpaceArea: v.SpaceArea,
- ParkId: v.ParkId,
- ParkType: v.ParkType,
- ParkNumber: v.ParkNumber,
- ParkName: v.ParkName,
- Comment: v.Comment,
- HouseId: v.HouseId,
- Status: v.Status,
- HouseName: fmt.Sprintf("%v-%v-%v", houseInfoM[v.HouseId].BuildingNumber, houseInfoM[v.HouseId].UnitNumber, houseInfoM[v.HouseId].HouseNumber),
- CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
- }
- if v.Status == SpaceStatusRent && v.VehicleId > 0 {
- reply.List[i].PlateNo = vehicleInfoM[v.VehicleId].PlateNo
- }
- }
- return reply, nil
- }
|