// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package garden 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-system/errors" dbmodel "property-system/model" pb_v1 "property-system/pb/v1" ) func checkGardenRentSellCountParam(req *pb_v1.GardenRentSellCountRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "请选择要更改的小区") } return nil } // func GardenRentSellCount(ctx context.Context, req *pb_v1.GardenRentSellCountRequest) (reply *pb_v1.GardenRentSellCountReply, err error) { reply = &pb_v1.GardenRentSellCountReply{} // 捕获各个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 = checkGardenRentSellCountParam(req) if err != nil { return nil, err } if req.SellIncrease == 0 && req.RentIncrease == 0 { return reply, nil } p := &dbmodel.TGardenApproved{} where := map[string]interface{}{ "id": req.GardenId, } err = p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if p.ID == 0 { return nil, errors.GardenNotExist } garden := dbmodel.TGardenApproved{} values := map[string]interface{}{} if req.RentIncrease > 0 { values["house_rent_count"] = gorm.Expr("house_rent_count + ?", req.RentIncrease) } else if req.RentIncrease < 0 { values["house_rent_count"] = gorm.Expr("house_rent_count - ?", -req.RentIncrease) } if req.SellIncrease > 0 { values["sell_rent_count"] = gorm.Expr("sell_rent_count + ?", req.SellIncrease) } else if req.SellIncrease < 0 { values["sell_rent_count"] = gorm.Expr("sell_rent_count - ?", req.SellIncrease) } where = map[string]interface{}{ "id": req.GardenId, } err = garden.Update(database.DB(), where, values) if err != nil { return nil, errors.DataBaseError } return reply, nil }