// 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" 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" ) func checkVehicleUpdateParam(req *pb_v1.VehicleUpdateRequest) 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.Id == 0: return status.Error(10003, "id不能为空") 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 checkSpace(req *pb_v1.VehicleUpdateRequest, oldVehicle *dbmodel.TVehicle, targetSpace *dbmodel.TParkSpace, dbname string) error { // 目标车位已绑定车辆 if targetSpace.VehicleId > 0 && req.SpaceId != oldVehicle.SpaceId { return status.Error(10003, "车位已绑定车辆") } // 目标车位未出售,车辆绑定类型不能为出售 if req.BindType == BindTypeSell && targetSpace.HouseId == 0 { return status.Error(10003, "出售类型请先在车位配置中绑定房屋") } if targetSpace.HouseId > 0 { // 目标车位已出售,不能绑定出租类型 if req.BindType != BindTypeSell { return status.Error(10003, "车位已出售,不能出租") } householdHasThisHouse, err := checkHouseholdHouse(req.HouseholdUid, targetSpace.HouseId, dbname) if err != nil { return err } // 目标车位已出售,车辆业主没有入驻车位所绑定的房屋 if !householdHasThisHouse { return status.Error(10003, "车位已绑定房屋,该用户没有入驻该房屋") } } return nil } func VehicleUpdate(ctx context.Context, req *pb_v1.VehicleUpdateRequest) (reply *pb_v1.VehicleUpdateReply, err error) { reply = &pb_v1.VehicleUpdateReply{} // 捕获各个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 = checkVehicleUpdateParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) // 获取旧数据 old := dbmodel.NewVehicle(dbname) where := map[string]interface{}{ "id": req.Id, } err = old.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if old.ID == 0 { return nil, errors.ErrRecordNotFound } reply.Origin = &pb_v1.VehicleUpdateRequest{ GardenId: req.GardenId, Id: req.Id, PlateNo: old.PlateNo, VehicleType: old.VehicleType, Vin: old.Vin, EngineNo: old.EngineNo, Brand: old.Brand, Color: old.Color, SpaceId: old.SpaceId, HouseholdUid: old.HouseholdUid, BindType: old.BindType, Displacement: old.Displacement, DisplacementUnit: old.DisplacementUnit, Seat: old.Seat, } now := time.Now() bindType := req.BindType space := dbmodel.NewParkSpace(dbname) parkId := old.ParkId // 绑定新车位,判断合法性 if req.SpaceId > 0 && req.SpaceId != old.SpaceId || req.HouseholdUid != old.HouseholdUid { 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 err = checkSpace(req, old, space, dbname); err != nil { return nil, err } parkId = space.ParkId } if req.SpaceId == 0 { bindType = BindTypeNormal } // 更新数据 veh := dbmodel.NewVehicle(dbname) where = map[string]interface{}{ "id": req.Id, } values := map[string]interface{}{ "plate_no": req.PlateNo, "vehicle_type": req.VehicleType, "vin": req.Vin, "engine_no": req.EngineNo, "brand": req.Brand, "color": req.Color, "space_id": req.SpaceId, "park_id": parkId, "household_uid": req.HouseholdUid, "bind_type": bindType, "displacement": req.Displacement, "displacement_unit": req.DisplacementUnit, "seat": req.Seat, "updated_at": now, } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() err = veh.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } // 更新新车位数据 if space.ParkId > 0 && space.VehicleId != req.Id { where := map[string]interface{}{ "id": req.SpaceId, } values := map[string]interface{}{ "vehicle_id": req.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 } } // 更新旧车位数据 if old.SpaceId > 0 && old.SpaceId != req.SpaceId { oldSpace := dbmodel.NewParkSpace(dbname) where := map[string]interface{}{ "id": old.SpaceId, } err = oldSpace.Find(db, where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if oldSpace.ID == 0 { db.Commit() return reply, nil } values := map[string]interface{}{ "vehicle_id": 0, } if oldSpace.HouseId == 0 { values["status"] = park_space.SpaceStatusEmpty values["bind_status"] = park_space.SpaceBindStatusEmpty } err = oldSpace.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } } db.Commit() return reply, nil }