123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package vehicle
- import (
- "context"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "property-garden/errors"
- "property-garden/impl/v1/park_space"
- "property-garden/impl/v1/statistic"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- const (
- BindTypeNormal = 1
- BindTypeSell = 2
- BindTypeRent = 3
- )
- func checkVehicleAddParam(req *pb_v1.VehicleAddRequest) error {
- if req.SpaceId == 0 {
- //req.BindType = BindTypeNormal
- }
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.PlateNo == "":
- return status.Error(10003, "车牌号不能为空")
- case req.Displacement < 1:
- //return status.Error(10003, "排量不能为空")
- case req.DisplacementUnit == "":
- //return status.Error(10003, "排量单位不能为空")
- case req.Seat < 1:
- //return status.Error(10003, "座位数不能为空")
- case req.BindType < 1:
- return status.Error(10003, "绑定类型不能为空")
- case req.SpaceId > 0 && req.BindType == BindTypeNormal:
- return status.Error(10003, "绑定类型错误")
- case req.HouseholdUid == 0:
- return status.Error(10003, "未绑定住户")
- case req.BindType == BindTypeSell && req.SpaceId == 0:
- return status.Error(10003, "出售类型请绑定车位")
- case req.BindType == BindTypeRent && req.SpaceId == 0:
- return status.Error(10003, "出租类型请绑定车位")
- }
- return nil
- }
- func checkHouseholdHouse(uid int64, houseId int64, dbname string) (bool, error) {
- p := dbmodel.NewHouseApprovedGarden(dbname)
- where := map[string]interface{}{
- "uid": uid,
- "house_id": houseId,
- }
- count, err := p.Count(database.DB(), where, nil)
- if err != nil {
- return false, errors.DataBaseError
- }
- if count > 0 {
- return true, nil
- }
- return false, nil
- }
- func VehicleAdd(ctx context.Context, req *pb_v1.VehicleAddRequest) (reply *pb_v1.VehicleAddReply, err error) {
- reply = &pb_v1.VehicleAddReply{}
- // 捕获各个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 = checkVehicleAddParam(req)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- dbname := utils.GetGardenDbName(req.GardenId)
- bindType := req.BindType
- space := dbmodel.NewParkSpace(dbname)
- if req.SpaceId > 0 {
- where := map[string]interface{}{
- "id": req.SpaceId,
- }
- err = space.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if space.ID == 0 {
- return nil, status.Error(10003, "车位不存在")
- }
- if space.VehicleId > 0 {
- return nil, status.Error(10003, "车位已绑定车辆")
- }
- if req.BindType == BindTypeSell && space.HouseId == 0 {
- return nil, status.Error(10003, "出售类型请先在车位配置中绑定房屋")
- }
- if space.HouseId > 0 {
- if req.BindType != BindTypeSell {
- return nil, status.Error(10003, "车位已出售,不能出租")
- }
- householdHasThisHouse, err := checkHouseholdHouse(req.HouseholdUid, space.HouseId, dbname)
- if err != nil {
- return nil, err
- }
- if !householdHasThisHouse {
- return nil, status.Error(10003, "车位已绑定房屋,该用户没有入驻该房屋")
- }
- }
- }
- if req.SpaceId == 0 {
- bindType = BindTypeNormal
- }
- veh := dbmodel.TVehicle{
- PlateNo: req.PlateNo,
- VehicleType: req.VehicleType,
- Vin: req.Vin,
- EngineNo: req.EngineNo,
- Brand: req.Brand,
- Color: req.Color,
- SpaceId: req.SpaceId,
- ParkId: space.ParkId,
- HouseholdUid: req.HouseholdUid,
- BindType: bindType,
- Displacement: req.Displacement,
- DisplacementUnit: req.DisplacementUnit,
- Seat: req.Seat,
- CreatedAt: now,
- UpdatedAt: now,
- }
- db := database.DB().Begin()
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- veh.SetTable(dbname)
- err = veh.Insert(db)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if space.ID > 0 {
- where := map[string]interface{}{
- "id": req.SpaceId,
- }
- values := map[string]interface{}{
- "vehicle_id": veh.ID,
- }
- if bindType == BindTypeSell || bindType == BindTypeRent {
- values["status"] = bindType
- values["bind_status"] = park_space.SpaceBindStatusBind
- }
- err = space.Update(db, where, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- }
- mreq := pb_v1.ObjStatisticSetRequest{
- ObjType: statistic.ObjTypeVehicle,
- TotalIncrease: int64(1),
- GardenId: req.GardenId,
- }
- err = statistic.ObjStatisticSetWithDb(&mreq, db)
- if err != nil {
- return nil, err
- }
- db.Commit()
- reply.Id = veh.ID
- return reply, nil
- }
|