obj_statistic_set.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package statistic
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "gorm.io/gorm"
  10. "property-garden/errors"
  11. dbmodel "property-garden/model"
  12. "property-garden/pb"
  13. pb_v1 "property-garden/pb/v1"
  14. "property-garden/utils"
  15. "time"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. const (
  21. ObjTypeHouse = 1
  22. ObjTypeSpace = 2
  23. ObjTypeVehicle = 3
  24. ObjTypeUser = 4
  25. )
  26. func checkObjStatisticSetParam(req *pb_v1.ObjStatisticSetRequest) error {
  27. switch {
  28. case req.GardenId == 0:
  29. return status.Error(10003, "小区不能为空")
  30. case req.ObjType == 0:
  31. return status.Error(10003, "类型不能为空")
  32. }
  33. return nil
  34. }
  35. func ObjStatisticSetWithDb(req *pb_v1.ObjStatisticSetRequest, db *gorm.DB) error {
  36. now := time.Now()
  37. dbname := utils.GetGardenDbName(req.GardenId)
  38. where := map[string]interface{}{
  39. "obj_type": req.ObjType,
  40. }
  41. values := map[string]interface{}{}
  42. if req.TotalIncrease > 0 {
  43. values["total"] = gorm.Expr("total + ?", req.TotalIncrease)
  44. } else if req.TotalIncrease < 0 {
  45. values["total"] = gorm.Expr("total - ?", 0-req.TotalIncrease)
  46. }
  47. if len(values) == 0 {
  48. return nil
  49. }
  50. p := dbmodel.NewStatisticObj(dbname)
  51. affected, err := p.UpdateAffected(db, where, values)
  52. if err != nil {
  53. return errors.DataBaseError
  54. }
  55. if affected > 0 {
  56. if req.ObjType != ObjTypeVehicle {
  57. mreq := pb_v1.CompanyObjStatisticSetRequest{GardenId: req.GardenId, ObjType: req.ObjType, TotalIncrease: req.TotalIncrease}
  58. _, err := pb.Company.CompanyObjStatisticSet(context.Background(), &mreq)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. p.ObjType = req.ObjType
  66. p.CreatedAt = now
  67. p.UpdatedAt = now
  68. p.Total = req.TotalIncrease
  69. err = p.Insert(db)
  70. if err != nil {
  71. return errors.DataBaseError
  72. }
  73. if req.ObjType != ObjTypeVehicle {
  74. mreq := pb_v1.CompanyObjStatisticSetRequest{GardenId: req.GardenId, ObjType: req.ObjType, TotalIncrease: req.TotalIncrease}
  75. _, err := pb.Company.CompanyObjStatisticSet(context.Background(), &mreq)
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. func ObjStatisticSet(ctx context.Context, req *pb_v1.ObjStatisticSetRequest) (reply *pb_v1.ObjStatisticSetReply, err error) {
  83. reply = &pb_v1.ObjStatisticSetReply{}
  84. // 捕获各个task中的异常并返回给调用者
  85. defer func() {
  86. if r := recover(); r != nil {
  87. err = fmt.Errorf("%+v", r)
  88. e := &status.Status{}
  89. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  90. logger.Error("err",
  91. zap.String("system_err", err.Error()),
  92. zap.Stack("stacktrace"))
  93. }
  94. }
  95. }()
  96. // 参数检查
  97. err = checkObjStatisticSetParam(req)
  98. if err != nil {
  99. return nil, err
  100. }
  101. db := database.DB().Begin()
  102. err = ObjStatisticSetWithDb(req, db)
  103. if err != nil {
  104. db.Rollback()
  105. return nil, err
  106. }
  107. db.Commit()
  108. return reply, err
  109. }