obj_statistic_set.go 2.7 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-company/errors"
  11. dbmodel "property-company/model"
  12. "property-company/pb"
  13. pb_v1 "property-company/pb/v1"
  14. "time"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. const (
  20. ObjTypeHouse = 1
  21. ObjTypeSpace = 2
  22. ObjTypeGarden = 3
  23. ObjTypeUser = 4
  24. )
  25. func checkCompanyObjStatisticSetParam(req *pb_v1.CompanyObjStatisticSetRequest) error {
  26. switch {
  27. case req.GardenId == 0 && req.Cid == 0:
  28. return status.Error(10003, "小区和公司不能同时为空")
  29. case req.ObjType == 0:
  30. return status.Error(10003, "类型不能为空")
  31. }
  32. return nil
  33. }
  34. func getCidByGarden(gardenId int64) (int64, error) {
  35. mreq := pb_v1.GardenInfosRequest{Ids: []int64{gardenId}}
  36. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  37. if err != nil {
  38. return 0, err
  39. }
  40. if len(mreply.List) == 0 {
  41. return 0, errors.ErrRecordNotFound
  42. }
  43. return mreply.List[0].Cid, nil
  44. }
  45. func CompanyObjStatisticSetWithDb(req *pb_v1.CompanyObjStatisticSetRequest, db *gorm.DB) error {
  46. now := time.Now()
  47. if req.Cid == 0 {
  48. cid, err := getCidByGarden(req.GardenId)
  49. if err != nil {
  50. return err
  51. }
  52. req.Cid = cid
  53. }
  54. where := map[string]interface{}{
  55. "cid": req.Cid,
  56. "obj_type": req.ObjType,
  57. }
  58. values := map[string]interface{}{}
  59. if req.TotalIncrease > 0 {
  60. values["total"] = gorm.Expr("total + ?", req.TotalIncrease)
  61. } else if req.TotalIncrease < 0 {
  62. values["total"] = gorm.Expr("total - ?", 0-req.TotalIncrease)
  63. }
  64. if len(values) == 0 {
  65. return nil
  66. }
  67. p := dbmodel.TStatisticObj{}
  68. affected, err := p.UpdateAffected(db, where, values)
  69. if err != nil {
  70. return errors.DataBaseError
  71. }
  72. if affected > 0 {
  73. return nil
  74. }
  75. p.ObjType = req.ObjType
  76. p.CreatedAt = now
  77. p.UpdatedAt = now
  78. p.Cid = req.Cid
  79. p.Total = req.TotalIncrease
  80. err = p.Insert(db)
  81. if err != nil {
  82. return errors.DataBaseError
  83. }
  84. return nil
  85. }
  86. func CompanyObjStatisticSet(ctx context.Context, req *pb_v1.CompanyObjStatisticSetRequest) (reply *pb_v1.CompanyObjStatisticSetReply, err error) {
  87. reply = &pb_v1.CompanyObjStatisticSetReply{}
  88. // 捕获各个task中的异常并返回给调用者
  89. defer func() {
  90. if r := recover(); r != nil {
  91. err = fmt.Errorf("%+v", r)
  92. e := &status.Status{}
  93. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  94. logger.Error("err",
  95. zap.String("system_err", err.Error()),
  96. zap.Stack("stacktrace"))
  97. }
  98. }
  99. }()
  100. // 参数检查
  101. err = checkCompanyObjStatisticSetParam(req)
  102. if err != nil {
  103. return nil, err
  104. }
  105. err = CompanyObjStatisticSetWithDb(req, database.DB())
  106. return reply, err
  107. }