decrease.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package garden
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "property-company/errors"
  14. dbmodel "property-company/model"
  15. pb_v1 "property-company/pb/v1"
  16. )
  17. func checkCompanyGardenDecreaseParam(req *pb_v1.CompanyGardenDecreaseRequest) error {
  18. switch {
  19. case req.Cid < 0:
  20. return status.Error(10003, "物业公司不能为空")
  21. }
  22. return nil
  23. }
  24. //
  25. func CompanyGardenDecrease(ctx context.Context, req *pb_v1.CompanyGardenDecreaseRequest) (reply *pb_v1.CompanyGardenDecreaseReply, err error) {
  26. reply = &pb_v1.CompanyGardenDecreaseReply{}
  27. // 捕获各个task中的异常并返回给调用者
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("%+v", r)
  31. e := &status.Status{}
  32. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  33. logger.Error("err",
  34. zap.String("system_err", err.Error()),
  35. zap.Stack("stacktrace"))
  36. }
  37. }
  38. }()
  39. // 检查参数
  40. err = checkCompanyGardenDecreaseParam(req)
  41. if err != nil {
  42. return nil, err
  43. }
  44. // 检查公司
  45. company := dbmodel.TCompany{}
  46. where := map[string]interface{}{
  47. "id":req.Cid,
  48. }
  49. values := map[string]interface{}{
  50. "garden_count":gorm.Expr("garden_count - ?", 1),
  51. }
  52. err = company.Update(database.DB(), where, values)
  53. if err != nil {
  54. return nil, errors.DataBaseError
  55. }
  56. return reply, nil
  57. }