// 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" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/statistic" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) const ( SpaceStatusEmpty = 1 SpaceStatusSell = 2 SpaceStatusRent = 3 ) const ( SpaceBindStatusEmpty = 1 SpaceBindStatusBind = 2 ) func checkParkSpaceAddParam(req *pb_v1.ParkSpaceAddRequest) 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.GardenId < 1: return status.Error(10003, "小区不能为空") case req.SpaceArea == 0: return status.Error(10003, "车位面积不能为空") } return nil } // func ParkSpaceAdd(ctx context.Context, req *pb_v1.ParkSpaceAddRequest) (reply *pb_v1.ParkSpaceAddReply, err error) { reply = &pb_v1.ParkSpaceAddReply{} // 捕获各个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 = checkParkSpaceAddParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) fmt.Printf("xxxxxxx:%v,%v\n", dbname, 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.TParkSpace{ ParkId:req.ParkId, SpaceType:req.SpaceType, SpaceNumber:req.SpaceNumber, SpaceArea:req.SpaceArea, Comment:req.Comment, UpdatedAt:now, CreatedAt:now, Status:SpaceStatusEmpty, BindStatus:SpaceBindStatusEmpty, HouseId:req.HouseId, } if req.HouseId > 0 { p.Status = SpaceStatusSell p.BindStatus = SpaceBindStatusBind } p.SetTable(dbname) db := database.DB().Begin() err = p.Insert(db) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { db.Rollback() return nil, status.Error(10003, "该车位已存在") } db.Rollback() return nil, errors.DataBaseError } // 更新车位数 park = dbmodel.NewPark(dbname) where = map[string]interface{}{ "id":req.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 } reply.Id = p.ID db.Commit() return reply, nil }