123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // 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
- }
|