123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package park
- 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"
- )
- /*
- func isParkBindCharge(id int64, dbname string) (bool, error) {
- p := dbmodel.NewChargeSpace(dbname)
- where := map[string]interface{}{
- "park_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 ParkDel(ctx context.Context, req *pb_v1.ParkDelRequest) (reply *pb_v1.ParkDelReply, err error) {
- reply = &pb_v1.ParkDelReply{}
- // 捕获各个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.NewPark(dbname)
- where := map[string]interface{}{
- "id":req.Id,
- //"garden_id":req.GardenId,
- }
- old := dbmodel.NewPark(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
- }
- // todo
- //isBind, err := isParkBindCharge(req.Id, dbname)
- //if err != nil {
- // return nil, err
- //}
- //if isBind {
- // return nil, status.Error(10003, "该区域有车位已绑定费用,请先解除绑定")
- //}
- reply.Origin = &pb_v1.ParkUpdateRequest{
- Id:req.Id,
- ParkType:old.ParkType,
- ParkNumber:old.ParkNumber,
- ParkName:old.ParkName,
- Comment:old.Comment,
- GardenId:req.GardenId,
- }
- db := database.DB().Begin()
- err = p.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- space := dbmodel.NewParkSpace(dbname)
- where = map[string]interface{}{
- "park_id":req.Id,
- }
- list, _ := space.List(database.DB(), where, nil, -1, -1)
- ids := make([]int64, len(list))
- for i, v := range list {
- if v.VehicleId > 0 {
- return nil, status.Error(10003, "有车辆绑定车位,请先解除绑定")
- }
- ids[i] = v.ID
- }
- if len(ids) > 0 {
- isBind, err := charge_utils.IsObjsBindCharge(ids, charge_utils.ObjTypeSpace, dbname)
- if err != nil {
- return nil, err
- }
- if isBind {
- return nil, status.Error(10003, "有车位已绑定费用,请先解除绑定")
- }
- }
- err = space.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- db.Commit()
- return reply, nil
- }
|