// 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" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" "time" ) const ( BindTypeNormal = 1 BindTypeSell = 2 BindTypeRent = 3 ) func checkParkSpaceUpdateParam(req *pb_v1.ParkSpaceUpdateRequest) error { switch { case req.ParkId < 1: return status.Error(10003, "停车场不能为空") case req.SpaceNumber == "": return status.Error(10003, "车位编号不能为空") case req.SpaceType < 1: return status.Error(10003, "车位类型不能为空") case req.Id < 1: return status.Error(10003, "id 不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.SpaceArea == 0: return status.Error(10003, "车位面积不能为空") } return nil } func updateChargeBindArea(spaceId int64, area float64, db *gorm.DB, dbname string) error { p := dbmodel.NewChargeBind(dbname) where := map[string]interface{}{ "obj_id":spaceId, "obj_type":charge_utils.ObjTypeSpace, "charge_basis":charge_utils.ChargeBasisSpaceArea, } value := map[string]interface{}{ "obj_area":area, } err := p.Update(db, where, value) if err != nil { return errors.DataBaseError } return nil } // func ParkSpaceUpdate(ctx context.Context, req *pb_v1.ParkSpaceUpdateRequest) (reply *pb_v1.ParkSpaceUpdateReply, err error) { reply = &pb_v1.ParkSpaceUpdateReply{} // 捕获各个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 = checkParkSpaceUpdateParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) park := dbmodel.NewPark(dbname) where := map[string]interface{}{ "id":req.ParkId, //"garden_id":req.GardenId, } err = park.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound{ return nil, errors.DataBaseError } if park.ID == 0 { return nil, errors.ErrRecordNotFound } now := time.Now() p := dbmodel.NewParkSpace(dbname) where = map[string]interface{}{ "id":req.Id, //"garden_id":req.GardenId, } values := map[string]interface{}{ "space_number":req.SpaceNumber, "space_type":req.SpaceType, "space_area":req.SpaceArea, "park_id":req.ParkId, "comment":req.Comment, "house_id":req.HouseId, "updated_at":now, } 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 } if req.HouseId > 0 { values["status"] = SpaceStatusSell values["bind_status"] = SpaceBindStatusBind } else if old.VehicleId == 0 { values["status"] = SpaceStatusEmpty values["bind_status"] = SpaceBindStatusEmpty } if old.ParkId != req.ParkId || old.SpaceNumber != req.SpaceNumber || (old.HouseId > 0 && req.HouseId != old.HouseId){ isBind, err := charge_utils.IsObjBindCharge(req.Id, charge_utils.ObjTypeSpace, dbname) if err != nil { return nil, err } if isBind { return nil, status.Error(10003, "该车位已绑定费用,请先解除绑定") } } 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() defer func() { if err != nil { db.Rollback() } }() err = p.Update(db, where, values) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "该车位已存在") } return nil, errors.DataBaseError } if req.SpaceArea != old.SpaceArea { err = updateChargeBindArea(req.Id, req.SpaceArea, db, dbname) if err != nil { return nil, err } } if req.HouseId > 0 && req.HouseId != old.HouseId && old.VehicleId > 0 { // 检查新房屋id与车辆所属用户的房屋是否匹配 veh := dbmodel.NewVehicle(dbname) filter := map[string]interface{}{ "id":old.VehicleId, } err = veh.Find(database.DB(), filter) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if veh.ID > 0 { if veh.BindType == BindTypeRent { return nil, status.Error(10003, fmt.Sprintf("该车位已出租给车辆%s,无法绑定房屋", veh.PlateNo)) } household := dbmodel.NewHouseApprovedGarden(dbname) filter = map[string]interface{}{ "uid":veh.HouseholdUid, "house_id":req.HouseId, } count, err := household.Count(db, where, nil) if err != nil { return nil, errors.DataBaseError } if count == 0 { return nil, status.Error(10003, "该车位以出售方式绑定车辆, 车辆所属业主未申请该房屋") } } } db.Commit() return reply, nil }