update.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package property_package
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkPropertyPackageUpdateParam(req *pb_v1.PropertyPackageUpdateRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case propertyPackageTypeM[req.PackageType] == "":
  24. return status.Error(10003, "赠送类型错误")
  25. case req.PackageType == PropertyPackageTypeMonth && req.GiveMonths < 1:
  26. return status.Error(10003, "赠送月数不能为空")
  27. case req.PackageType == PropertyPackageTypeText && req.GiveContent == "":
  28. return status.Error(10003, "赠送文本不能为空")
  29. case req.PayMonths < 1:
  30. return status.Error(10003, "缴费月数不能为空")
  31. case req.Id < 1:
  32. return status.Error(10003, "id 不能为空")
  33. }
  34. return nil
  35. }
  36. func PropertyPackageUpdate(ctx context.Context, req *pb_v1.PropertyPackageUpdateRequest) (reply *pb_v1.PropertyPackageUpdateReply, err error) {
  37. reply = &pb_v1.PropertyPackageUpdateReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. // 参数检查
  51. err = checkPropertyPackageUpdateParam(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. dbname := utils.GetGardenDbName(req.GardenId)
  56. if req.PackageType == PropertyPackageTypeMonth {
  57. p := dbmodel.NewPropertyPackage(dbname)
  58. where := map[string]interface{}{
  59. "package_type":PropertyPackageTypeMonth,
  60. "give_months":req.GiveMonths,
  61. "pay_months":req.PayMonths,
  62. "id !=":req.Id,
  63. }
  64. err = p.Find(database.DB(), where)
  65. if err != nil && err != gorm.ErrRecordNotFound{
  66. return nil, errors.DataBaseError
  67. }
  68. if p.ID > 0 {
  69. if p.Enable == 2 {
  70. return nil, status.Error(10003, "已存在相同套餐, 请启用")
  71. }
  72. return nil, status.Error(10003, "已存在相同套餐")
  73. }
  74. }
  75. // 获取旧数据
  76. old := dbmodel.NewPropertyPackage(dbname)
  77. where := map[string]interface{}{
  78. "id":req.Id,
  79. }
  80. err = old.Find(database.DB(), where)
  81. if err != nil && err != gorm.ErrRecordNotFound {
  82. return nil, errors.DataBaseError
  83. }
  84. if old.ID == 0 {
  85. return nil, errors.ErrRecordNotFound
  86. }
  87. reply.Origin = &pb_v1.PropertyPackageUpdateRequest{
  88. GardenId:req.GardenId,
  89. Id:req.Id,
  90. GiveMonths:old.GiveMonths,
  91. GiveContent:old.GiveContent,
  92. PayMonths:old.PayMonths,
  93. PackageType:old.PackageType,
  94. Enable:false,
  95. }
  96. if old.Enable == 1 {
  97. reply.Origin.Enable = true
  98. }
  99. // 更新新数据
  100. now := time.Now()
  101. propertyPackage := dbmodel.NewPropertyPackage(dbname)
  102. enable := 1
  103. if !req.Enable {
  104. enable = 2
  105. }
  106. values := map[string]interface{}{
  107. "give_months":req.GiveMonths,
  108. "give_content":req.GiveContent,
  109. "package_type":req.PackageType,
  110. "enable":enable,
  111. "pay_months":req.PayMonths,
  112. "updated_at":now,
  113. }
  114. where = map[string]interface{}{
  115. "id":req.Id,
  116. }
  117. err = propertyPackage.Update(database.DB(), where, values)
  118. if err != nil {
  119. return nil, errors.DataBaseError
  120. }
  121. return reply, nil
  122. }