free_garden.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package company
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "gorm.io/gorm"
  10. "property-company/errors"
  11. dbmodel "property-company/model"
  12. pb_v1 "property-company/pb/v1"
  13. "time"
  14. "git.getensh.com/common/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. //
  19. func CompanyChangeFreeGarden(ctx context.Context, req *pb_v1.CompanyChangeFreeGardenRequest) (reply *pb_v1.CompanyChangeFreeGardenReply, err error) {
  20. reply = &pb_v1.CompanyChangeFreeGardenReply{}
  21. // 捕获各个task中的异常并返回给调用者
  22. defer func() {
  23. if r := recover(); r != nil {
  24. err = fmt.Errorf("%+v", r)
  25. e := &status.Status{}
  26. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  27. logger.Error("err",
  28. zap.String("system_err", err.Error()),
  29. zap.Stack("stacktrace"))
  30. }
  31. }
  32. }()
  33. if req.Id < 0 || req.Count == 0{
  34. return nil, errors.ParamsError
  35. }
  36. company := &dbmodel.TCompany{}
  37. where := map[string]interface{}{
  38. "id":req.Id,
  39. }
  40. err = company.Find(database.DB(), where)
  41. if err != nil && err != gorm.ErrRecordNotFound{
  42. return nil, errors.DataBaseError
  43. }
  44. if company.ID == 0 {
  45. return nil, errors.ErrRecordNotFound
  46. }
  47. reply.Origin = &pb_v1.CompanyChangeFreeGardenRequest{
  48. Id:company.ID,
  49. Count:int32(company.FreeGardenCount),
  50. }
  51. now := time.Now()
  52. p := &dbmodel.TCompany{}
  53. values := map[string]interface{}{
  54. "updated_at":now,
  55. "free_garden_count":req.Count,
  56. }
  57. err = p.Update(database.DB(), where, values)
  58. if err != nil {
  59. return nil, errors.DataBaseError
  60. }
  61. return reply, nil
  62. }