123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package vehicle
- 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/park_space"
- "property-garden/impl/v1/statistic"
- dbmodel "property-garden/model"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- )
- func checkVehicleDelParam(req *pb_v1.VehicleDelRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Id < 1:
- return status.Error(10003, "id 不能为空")
- }
- return nil
- }
- func VehicleDel(ctx context.Context, req *pb_v1.VehicleDelRequest) (reply *pb_v1.VehicleDelReply, err error) {
- reply = &pb_v1.VehicleDelReply{}
- // 捕获各个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 = checkVehicleDelParam(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,
- }
- // 更新数据
- veh := dbmodel.NewVehicle(dbname)
- db := database.DB().Begin()
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- err = veh.Delete(db, where)
- 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
- }
- // 更新旧车位数据
- if old.SpaceId > 0 {
- oldSpace := dbmodel.NewParkSpace(dbname)
- where := map[string]interface{}{
- "id":old.SpaceId,
- "vehicle_id":req.Id,
- }
- 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
- }
|