repair_statistic_set.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "time"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. const (
  20. HandleTypeRepair = 1
  21. HandleTypeSuggestion = 2
  22. HandleTypeHousehold = 3
  23. )
  24. var StatisticDesc = map[int]string{
  25. HandleTypeRepair:"报事报修",
  26. HandleTypeSuggestion:"投诉建议",
  27. HandleTypeHousehold:"住户审核",
  28. }
  29. func checkRepairStatisticSetParam(req *pb_v1.RepairStatisticSetRequest) error {
  30. switch {
  31. case req.GardenId == 0:
  32. return status.Error(10003, "小区不能为空")
  33. case req.HandleType == 0:
  34. return status.Error(10003, "类型不能为空")
  35. }
  36. return nil
  37. }
  38. func RepairStatisticSetWithDb(req *pb_v1.RepairStatisticSetRequest, db *gorm.DB) error {
  39. now := time.Now()
  40. dbname := utils.GetGardenDbName(req.GardenId)
  41. where := map[string]interface{}{
  42. "handle_type":req.HandleType,
  43. }
  44. values := map[string]interface{}{}
  45. if req.TotalIncrease > 0 {
  46. values["total"]= gorm.Expr("total + ?", req.TotalIncrease)
  47. } else if req.TotalIncrease < 0{
  48. values["total"] = gorm.Expr("total - ?", 0-req.TotalIncrease)
  49. }
  50. if req.FinishIncrease > 0 {
  51. values["finish"]= gorm.Expr("finish + ?", req.FinishIncrease)
  52. } else if req.FinishIncrease < 0{
  53. values["finish"] = gorm.Expr("finish - ?", 0-req.FinishIncrease)
  54. }
  55. if len(values) == 0 {
  56. return nil
  57. }
  58. p := dbmodel.NewStatisticRepair(dbname)
  59. affected, err := p.UpdateAffected(db, where, values)
  60. if err != nil {
  61. return errors.DataBaseError
  62. }
  63. if affected > 0 {
  64. return nil
  65. }
  66. p.HandleType = req.HandleType
  67. p.CreatedAt = now
  68. p.UpdatedAt = now
  69. p.Total = req.TotalIncrease
  70. p.Finish = req.FinishIncrease
  71. err = p.Insert(db)
  72. if err != nil {
  73. return errors.DataBaseError
  74. }
  75. return nil
  76. }
  77. func RepairStatisticSet(ctx context.Context, req *pb_v1.RepairStatisticSetRequest) (reply *pb_v1.RepairStatisticSetReply, err error) {
  78. reply = &pb_v1.RepairStatisticSetReply{}
  79. // 捕获各个task中的异常并返回给调用者
  80. defer func() {
  81. if r := recover(); r != nil {
  82. err = fmt.Errorf("%+v", r)
  83. e := &status.Status{}
  84. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  85. logger.Error("err",
  86. zap.String("system_err", err.Error()),
  87. zap.Stack("stacktrace"))
  88. }
  89. }
  90. }()
  91. // 参数检查
  92. err = checkRepairStatisticSetParam(req)
  93. if err != nil {
  94. return nil, err
  95. }
  96. err = RepairStatisticSetWithDb(req, database.DB())
  97. return reply, err
  98. }