// 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" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/charge_utils" "property-garden/impl/v1/statistic" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" ) func getVehicleInfo(vehicleId int64, dbname string) (dbmodel.TVehicle, error) { p := dbmodel.NewVehicle(dbname) where := map[string]interface{}{ "id":vehicleId, //"garden_id":req.GardenId, } err := p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return *p, errors.DataBaseError } return *p, nil } /* func isSpaceBindCharge(id int64, dbname string) (bool, error) { p := dbmodel.NewChargeSpace(dbname) where := map[string]interface{}{ "space_id":id, } 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 ParkSpaceDel(ctx context.Context, req *pb_v1.ParkSpaceDelRequest) (reply *pb_v1.ParkSpaceDelReply, err error) { reply = &pb_v1.ParkSpaceDelReply{} // 捕获各个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.Id < 1 || req.GardenId < 1 { return nil, errors.ParamsError } dbname := utils.GetGardenDbName(req.GardenId) p := dbmodel.NewParkSpace(dbname) where := map[string]interface{}{ "id":req.Id, //"garden_id":req.GardenId, } // 原数据 old := dbmodel.NewParkSpace(dbname) err = old.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return nil, errors.DataBaseError } if old.ID == 0 { return nil, errors.ErrRecordNotFound } isBind, err := charge_utils.IsObjBindCharge(req.Id, charge_utils.ObjTypeSpace, dbname) if err != nil { return nil, err } if isBind { return nil, status.Error(10003, "该车位已绑定费用,请先解除绑定") } if old.VehicleId > 0 { vinfo, err := getVehicleInfo(old.VehicleId, dbname) if err != nil { return nil, err } if vinfo.ID > 0 { return nil, status.Error(10003, fmt.Sprintf("请先在车辆%s中解除对该车位的绑定", vinfo.PlateNo)) } } reply.Origin = &pb_v1.ParkSpaceUpdateRequest{ Id:req.Id, ParkId:old.ParkId, Comment:old.Comment, GardenId:req.GardenId, SpaceArea:old.SpaceArea, SpaceType:old.SpaceType, SpaceNumber:old.SpaceNumber, } // 删除车位 db := database.DB().Begin() err = p.Delete(db, where) if err != nil { db.Rollback() return nil, errors.DataBaseError } // 更新停车场车位数 park := dbmodel.NewPark(dbname) where = map[string]interface{}{ "id":old.ParkId, } values := map[string]interface{}{ "space_count":gorm.Expr("space_count - ?", 1), } err = park.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } mreq := pb_v1.ObjStatisticSetRequest{ ObjType:statistic.ObjTypeSpace, TotalIncrease:int64(-1), GardenId:req.GardenId, } err = statistic.ObjStatisticSetWithDb(&mreq, db) if err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }